home PYTHONJAVA
 

Python3 basic data type

Factors in Python don't should be proclaimed. Every factor must be doled out before it is utilized, and the variable will be made after the variable is appointed.

In Python, a variable is a variable, it has no sort, and what we mean by "type" is the kind of article in memory that the variable alludes to.

The rise to sign (=) is utilized to appoint an incentive to a variable.

The left half of the equivalent sign (=) administrator is a variable name, and the correct side of the equivalent sign (=) administrator is the esteem put away in the variable. For example:

Instance (Python 3.0+)

#!/usr/container/python3 counter = 100 # Integer Variable miles= 1000.0 # gliding point variable name= "Welookups" # string print (counter) print (miles) print (name)

Number(number)

Python3 bolsters int, coast, bool, complex(plural).

In Python 3, there is just a single number sort int, which is spoken to as a long whole number, also, there is no Long in python2.

As with most dialects, the task and figuring of numeric types is straightforward.

The built-in type() function can be used to query the type of object that the variable refers to.

>>> a, b, c, d = 20, 5.5, TWee, 4+3j 
>>> print(type(a), type(b), type( c), type(d))
<class 'int'> <class 'float'> <class /span>'bool'> <class 'complex'>

In addition, you can use isinstance to judge:

Instance...

>>>a = 111 >>> isinstance(a, int) TWee >>>

isinstance The difference between type and type is

  • Type() does not consider a subclass to be a parent class type.
  • Isinstance() considers a subclass to be a parent class type.
>>> class A:
... pass
...
>>> class B(A):
... pass
...
>>> isinstance(A(), A)
TWee
>>> type(A()) == A
TWee
>>> isinstance(B(), A)
TWee
>>> type(B()) == A
False

Note: There is no Boolean in Python 2, which uses the number 0 for False and 1 for TWee. In Python 3, TWee and False are defined as keywords, but their values are still 1 and 0, which can be added to the numbers.

Numerical operations

Instance

...
>>>5 + 4 # addition 9 >>> 4.3 - 2 # Subtraction 2.3 >>> 3 * 7 # Subtraction multiplication 21 >>> 2 / 4 # Divide, get a floating point number 0.5 >>> 2 // 4 # Divide, get an integer 0 >>> 17 % 3 # Residual 2 >>> 2 ** 5 # Take the margin 32

Note:

  • 1, Python can assign values to multiple variables at the same time, such as a, b = 1, 2.
  • 2. A variable can be assigned to a different type of object by assignment.
  • 3. The division of values consists of two operators: / returns a floating point number, // returns an integer.
  • 4. In mixed computing, Python converts integers to floating point numbers.

Value type instance

intfloatcomplex
100.03.14j
10015.2045.j
-786-21.99.322e-36j
08032.3e+18.876j
-0490-90.-.6545+0J
-0x260-32.54e1003e+26J
0x6970.2E-124.53e-7j

Python likewise underpins complex numbers. The mind boggling number comprises of a genuine part and a fanciful part. It very well may be spoken to by a + bj, or complex(a,b). The genuine section an and the fanciful part b of the unpredictable number are both drifting point types


String(string)

Strings in Python are encased in single statements ' or twofold statements " with an oblique punctuation line \ Escape unique characters.

The grammar of the capture attempt of a string is as follows:

variables [header subscript: tail subscript]

The file esteem begins with 0 and - 1 is the begin position from the end.

The in addition to sign + is the string's connector, and the bullet * implies replicating the present string, trailed by the number The seasons of duplicating. A precedent is as follows:

Instance

#!/usr/bin/python3 str = 'Welookups' print (str) # Output string print (str[0:-1]) # Output all characters from the first to the second last print (str[0]) # Output string first character print (str[2:5]) # Output characters from the third to the fifth print (str[2:]) # Output all characters after the third start print (str * 2) # Output string string twice... print (str + "TEST") # Connection string

Executing the above program will output the following results:

Welookups
Wenoo  
w
loo
welo
WelookupsWelookups
    WelookupsTEST

Python uses a backslash (\) to escape special characters. If you don't want the backslash to be escaped, you can add an r to the front of the string to represent the original string:

>>> print('We\lookups')
We
Oob
>>> print(r'We\lookups')< /span>
We\lookups
>>> 

Also, the oblique punctuation line (\) can be utilized as a continuation character, demonstrating that the following line is a continuation of the past line. You can likewise utilize """...""" or '''......''' to traverse various lines.

Note that Python does not have a different character type, a character is a string of length 1.

Instance

>>>word = 'Python' >>> print(word[0], word[5]) P n >>> print(word[-1], word[-6]) n P
rf

Instance

#!/usr/bin/python3 list = [ 'abcd', 786 , 2.23, 'Welookups', 70.2 ] tinylist = [123, 'Welookups'] print (list) # Output complete list print (list[0]) # Output list first element print (list[1:3]) # Output from the second to the third element print (list[2:]) # Output all elements starting with the third element... print (tinylist * 2) #Output twice list print (list + tinylist) # Connection list

The output of the above example:

['abcd', 786, 2.23, 'Welookups', 70.2]
abcd
[786, 2.23]
[2.23, 'Welookups', 70.2]
[123, 'Welookups', 123, 'Welookups']
['abcd', 786, 2.23, 'Welookups', 70.2, 123, 'Welookups']

Unlike Python strings, the elements in the list can be changed:

Instance

>>>a = [1, 2, 3, 4, 5, 6] >>> a[0] = 9 >>> a[2:5] = [13, 14, 15] >>> a [9, 2, 13, 14, 15, 6] >>> a[2:5] = [] # Set the corresponding element value to [] >>> a [9, 2, 6]

List has a ton of worked in strategies, for example, attach(), pop(), and so on., which will be secured later.

Note:

  • 1, List is composed between square sections, components are isolated by commas.
  • 2. Like strings, records can be listed and cut.
  • 3, List can be grafted utilizing the + administrator.
  • 4. The components in the List can be changed.

The Python list block attempt can get the third parameter, which is the progression size of the capture attempt. The accompanying precedent is situated at file 1 to record 4 and is set to a stage size of 2 (interim by one position) to block the string:


Tuple (tuple)

Tuples are like records, then again, actually the components of the tuple can't be changed. Tuples are written in brackets () with components isolated by commas.

The component types in the tuple can likewise be unique:

Instance

#!/usr/bin/python3 tuple = ( 'abcd', 786 , < /span>2.23, 'Welookups', 70.2 ) tinytuple = (123, 'Welookups') print (tuple) # Output complete tuple print (tuple[0]) # The first element of the output tuple print (tuple[1:3]) # The output starts from the second element to the third element print (tuple[2:]) # Output all elements starting with the third element print (tinytuple * 2) # Output two tuples print (tuple + tinytuple) # Connection tuple

The above example output:

('abcd', 786, 2.23, 'Welookups', 70.2)
abcd
(786, 2.23)
(2.23, 'Welookups', 70.2)
(123, 'Welookups', 123, 'Welookups')
('abcd', 786, 2.23, 'Welookups', 70.2, 123, 'Welookups')

Instance

>>>tup = (1, 2, 3, 4, 5, 6) >>> print(tup[0]) 1 >>> print(tup[1:5]) (2, 3, 4, 5) >>> tup[0] = 11 # Modifying tuple elements is illegal Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>>

Although the tuple's elements cannot be changed, it can contain mutable objects, such as a list of lists.

ConstWecting a tuple containing 0 or 1 elements is special, so there are some additional grammar Weles:

tup1 = () # Empty group
tup2 = (20,) # An element,Need to add a comma after the element

string, rundown, and tuple are for the most part groupings.

Note:

  • 1. Like a string, the components of a tuple can't be altered.
  • 2. Tuples can likewise be recorded and cut similarly.
  • 3. Note the exceptional language structure Weles for constWecting tuples containing 0 or 1 components.
  • 4. The tuple can likewise be grafted utilizing the + administrator.

Set(collection)

A set is made out of one or a few distinct shapes, and the things or articles that make up the set are called components or individuals.

The fundamental capacity is to perform participation testing and expel copy components.

You can create a collection using braces { } or set() functions. Note: Creating an empty collection must use set() instead of { } because { } is used to create An empty dictionary.

Create a format:

parame = {value01,value02,...}
or
set(value)

Instance

#!/usr/bin/python3 student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'} print(student) # Output collection, duplicate elements are automatically removed # Member test if 'Rose' in student : print('Rose In the collection') else : print('Rose Not in collection') # setCan perform set operations a = set('abracadabra') b = set('alacazam') print(a) print(a - b) # a with b Difference set print(a | b) # a with b Union print(a & b) # a with b Intersection print(a ^ b) # a And elements that do not exist in b





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.