I. Data types common in Python
1. Integer: The operation is accurate at any time
2. Floating-point number: When the operation, there may be rounding
3. String: string is any text enclosed in single quotation mark ' or double quotation mark '
1) The same type can be used with the + number or, the number for splicing
2) Different types can only use, number splicing, can not use the + number
3) After casting, can be splicing, such as STR (a) int (a) this is the cast
4. Boolean: Boolean value only true, false two values, the result of the logical operation is a Boolean value
5. Value of None: null is a special value in Python, represented by none, none is not understood as 0, because 0 is meaningful, and none is a special empty object
- Note: There is no null in Python
- The empty string is different from the none value, and the empty string is ""
two. The operators commonly used in Python
| Arithmetic operators |
| Compare (relational) operators |
| Assignment operators |
| logical operators |
| Bitwise operators |
| Member operators |
| Identity operator |
1. Arithmetic Operators
As the following table, suppose A = ten, B = 20
| Operator |
Describe |
Instance |
| + |
Add: Two objects added |
A + B output result 30 |
| - |
Minus: One number minus another number |
-B Output Result-10 |
| * |
Multiply by: Two number multiplied or returns a string that repeats several times |
A * b output result 200 |
| / |
Except: x divided by Y |
B/A Output Results 2.0 |
| % |
Modulo: Returns the remainder of a division |
B% A output result 0 |
| ** |
Power: Returns the Y power of X |
A * * b is 10 of 20 output result 100000000000000000000 |
| // |
Divide: Returns the integer part of the result |
9//2 output result 4, 9.0//2.0 Output Result 4.0 |
Here, we should pay extra attention to, in Python3,/is True division, get at least one bit of the decimal, and in Python2/means rounding
10/5 results:2.0
Although it represents rounding, if any of the divisor and dividend is a floating-point number, the resulting result is also a floating-point number
5//35.0//+//3.05.0//3.0 results:11.01.01.0
2. Comparison Operators
As the following table, suppose A = ten, B = 20, the return value is a Boolean value
| Operator |
Describe |
Instance |
| == |
equals: Compares objects for equality |
(A = = b) returns False |
| != |
Not equal to: Compares two objects for unequal |
(A! = b) returns True |
| > |
Greater than: Returns whether x is greater than Y |
(A > B) return False |
| < |
Less than: Returns whether x is less than Y. All comparison operators return 1 for true, and return 0 for false. This is different with the special variable True and false are equivalent. Note that the capitalization of these variable names |
(A < b) returns true |
| >= |
Greater than or equal to: Returns whether x is greater than or equal to Y |
(a >= b) returns False |
| <= |
Less than equals: Returns whether x is less than or equal to Y |
(a <= b) returns True |
The comparison operator returns a Boolean value, and the assignment is different from = =, please note!
3. Assignment Operators
As the following table, suppose A = ten, B = 20
| Operator |
Describe |
Instance |
| = |
Simple assignment operator |
c = A + B assigns the result of the operation of A + B to c |
| += |
Addition assignment operator |
c + = A is equivalent to C = C + A |
| -= |
Subtraction assignment operator |
C-= A is equivalent to C = c-a |
| *= |
Multiplication assignment operator |
C *= A is equivalent to C = c * A |
| /= |
Division assignment operator |
C/= A is equivalent to C = c/a |
| %= |
Modulo assignment operator |
C%= A is equivalent to C = c% A |
| **= |
Power assignment operator |
c * * = A is equivalent to C = c * * A |
| //= |
Take the divisible assignment operator |
C//= A is equivalent to C = c//A |
+ = and-= are more commonly used operators
Data types and common operators in Python