First, think about a problem: python Why should type? Open up space to store content
Data is divided into
- Integer numbers; int (integer)
- float (float); numbers with decimal
- str (string); use '' or "" Any text cited
- bool (Boolean); Only True and False
The basic data structure
- list (list) # []
- tuple (Ganso) # ()
- dict (dictionary) # {}
- set (set) #set ()
Numbers, strings, tuples are immutable;
Lists, dictionaries, collections are variable types
Note: Strictly speaking, the sequence (collection lists element dictionary) is a data structure, and the numerical value is a null string Boolean data type
Number (Digital)
- Python3 support int, float, bool, complex (plural)
Type >>> (. 3 ) < class ' int ' > >>> type (2.2 ) < class ' a float ' > >>> type (True) < class ' BOOL ' > >>> type (+ 2J. 1 ) < class ' Complex ' > >>> # scientific notation 1E10 10,000,000,000.0 >>>
# can not interoperate between different types of data, an example:
>>> a =. 1
>>> B = " gloryroad "
>>>a+b
Traceback (most recent call last):
File "<stdin> " , Line. 1, in <Module1>
TypeError: Unsupported the operand type (S) for +: ' int ' and ' STR '
# type conversion
. int (x) to x is converted to an integer
a float (x) to x conversion to a float.
complex (x) converts x to a complex, the real part of x, the imaginary part of 0. the
complex (x, y) x and y converted to a complex number, the real part of x, the imaginary part of y.x and y are numeric expressions.
If you must operate to relate to convert the data type of the time, data type conversion, you only need to type the data as a function name can be illustrated:
>>> age='18'
>>> type(age)
<class 'str'>
>>> int(age)+10
28
>>> str(100)
'100'
bool Boolean True and False
to sum up:
- "" 0 [] () {} # nonempty representative Fasle
- True representatives of non-empty
>>> 4>3 True >>> 4<3 False >>> bool([]) False >>> bool(()) False >>> bool({}) False >>> bool(0) False >>> bool("") False
Isinstance herein can also be used to determine:
>>> a=100
>>> isinstance(a,int)
True
The difference between the type and isinstance?
Numerical operations: +, -, *, /,%, @ **
>>> 5 + 4 # adder 9 >>> 4.3 - 2 # subtractor 2.3 >>> 3 * 7 # multiplier 21 >>> 10/8 # division, to obtain a floating-point number 1.25 >>> 10 @ 8 # take integer 1 >>> 10% 8 # modulo 2 >>> 2 ** 5 # exponentiation 32
>>> Import Math
>>> Math.sqrt (. 4 ) prescribing #
2.0
>>> math.pow (2,4) # power of
16.0
>>> Math.PI #Pi
3.141592653589793
>>> divmod (9,4) # simultaneously rounding and modulo
(2, 1)
* Divmod () function is the remainder and divisor calculation result combined return tuple (a // b, a% b) contains a quotient and a remainder of
Comparison operations: ==, =,>, <,> =, <=!
21 is A = >>> >>> B = 10 >>> C = 0 >>> ... IF (A == B): ... Print ( " . 1 - A is equal to B " ) ... the else : ... Print ( " . 1 - A not equal to B " ) ... . 1 - A not equal to B >>> IF (A =! B): ... Print ( " 2 - A not equal to B " ) .. . the else : ... Print ( "2 - a is equal to B ") ... 2 - A = b
Assignment operator: =, + =, - =, * =, / =,% = @ =,% =
21 is A = >>> >>> B = 10 >>> C = 0 >>> C = A + B >>> Print ( " . 1 - C values: " , C) . 1 - C values: 31 is >>> = C + a >>> Print ( " 2 - C values: " , C) 2 - C value: 52 is >>> = C * a >>> Print ( " . 3 - to C value: " , C) . 3 - C value: 1092 >>> C / = a >>> Print ( " . 4 - C values: ", C) . 4 - C value: 52.0
Bit computing:
& | Bitwise AND operator: two values involved in operations, if the corresponding two bits are 1, then the 1-bit result is 0 otherwise | (A & b) output 12, a binary interpretation: 00001100 |
| | Or bitwise operators: two long as the corresponding binary bit is a 1, it is a result bit. | (A | b) output 61, a binary interpretation: 00111101 |
^ | Bitwise exclusive OR operator: When two different corresponding binary, the result is a | (A ^ b) output 49, a binary interpretation: 00110001 |
~ | Bitwise operators: for each binary data bit inverse, i.e., the 1 to 0, the 0 to 1. similarly ~ x -x-1 | (~ A) -61 output, binary interpreter: 11000011, in the form of a complement to unsigned binary number. |
<< | Mobile operators left: each binary operand to the left a number of bits of all, the number of bits of a mobile "<<" specifies the right, discarding the upper, lower 0s. | output a << 2 240, the binary interpretation: 11110000 |
>> | Right Mobile Operators: The various binary operand is ">>" All right a number of bits left, ">>" the number of bits specified right | a >> 2 15 output, binary interpreter: 0000 1111 |
Logical operations: and, or, Not
Exercise:
1 can be output within the 1000. At the same time the number 2,3,5 divisible.
2. The output of the 1000 can be any of a number divisible by 3, 5 in. Each line of output 10 as the number.
3. To determine whether a year is a leap year
1. >>> for i in range(1,1000): ... if i%2==0 and i%3==0 and i%5==0: ... print(i) ... 2.
>>> count = 0
>>> for i in range(1,10001):
... if i % 2 ==0 or i % 3 ==0 or i % 5 ==0:
... print(i,end=" ")
... count+=1
... if count%10==0:
... print("")
3.
>>> year = int (input ( "Please enter a year:")) Please enter a year: 2000 >>> >>> IF (! == 0 and 4% year year% 100 = 0) or 400% year 0 ==: ... Print ( "% S is a leap year"% year) ... 2000 is a leap year
Members of the operation: in, not in
Identity operation: it is, is not
is | is two identifiers are determined from a reference to an object is not | the y-IS the X- , similar to the above mentioned id (the X-) == the above mentioned id (the y-) , if the reference is to the same object returns True, otherwise False |
is not | is determined is not the identifier is not referenced from two different objects | IS Not Y X , similar to the ID (A)! = ID (B) . If the reference is not the same object the results of True is returned, otherwise it returns False. |
NOTE: ID () function is used to obtain the memory address of the object.
In addition to the five types alone would write:
>>> print(type('road'))
<class 'str'>
>>> print(type([1,2,3]))
<class 'list'>
>>> print(type((1,2,3)))
<class 'tuple'>
>>> print(type({"a":1,"b":2}))
<class 'dict'>
>>> print(type(set([3,4])))
<class 'set'>