Arithmetic operator
+, -,*, / floating point division, % division,* * exponentiation, // full division.
These operators are used on variables of type Number to perform mathematical operations on the variables. + and * can also be used for String, List, and Tuple operations. + merges the contents of two variables. * is the result of repeating the value of a variable a specified number of times.
An operator
& is1The result is1| a for1The result is1^ The result when inconsistent is1~ transpose to the left, low fill0>> Right shift, negative high complement1, positive high order complement0
Copy the code
&, | ^ ~, < <, > >. These operators can only be used for integer operations, which convert integers into their binary counterparts for bitwise operations.
Relational operator
= = equal! = Not equal to > Greater than < less than >= Greater than or equal to <= Less than or equal toCopy the code
Used to compare the relationship between two variables of the same type, returning True or False.
Logical operator
-
x and y
- If x is False, the result is False
- If x is a False value that is not False, x is returned
- If x is True or True, the value of y is returned
-
x or y
- Returns x if x is True or a True value
- If x is False or a False value, y is returned
-
not x
- Return False if x is True or True
- Returns True if x is False or a False value
Note: bool 0, “, [], {}, (), None is false, all others are considered true.
The assignment operator
=
+=
-=
*=
/=
%=
**=
//=
Copy the code
Used to assign a specific value to a variable, except that = is a direct assignment, all other assignments are incremental. Incrementally assigned variables must be directly assigned before the incrementally assigned operator can be used.
Where += and *= can be used on String, List, and Tuple data types.
Code Exercise 1
-
Operators that manipulate numbers
-
Arithmetic operator
-
The assignment operator
-
Relational operator
-
Logical operator
-
An operation
-
Priority: ()+ arithmetic operations >> bitwise operations >> Relational operations >> Logical operators
1.Arithmetic operators + plus - minus * multiply/decimal division // integer division % mod ** Exponential priority: exponent >> multiply, divide, mod >> Add, subtract A =100/4 # 25.0 float
a=100//4 #25 int
a=10//4 #2 int 3.1415925 -> 3.14
print(a)
print(type(a))
print(3.1415926*100//1.0/100) expression: data (variables) and operators are syntactically combined a=11%4 # 2 (business)... 3 (remainder)
a=5支那2 # 5 ^ 2 = 25
print(a) # 3
a=3+2*2支那3 # 19
print(a)
2.Assignment += -= /= //= %= *= **= A =2
a+=4 # is the same thing as a is equal to a plus 4
a**=4
print(a) # 16
3.The result of the relational operator operation is Boolean > > < < < == equal! = unequal >= greater than or equal to <= less than or equal to b =10> =6
print(b)
print(type(b))
b = 10>6+10
b=(10>6) +10 # True=1 False=0
print(b)
4.The logical operator that operates on a value of a Boolean typeandAnd for theTrueAs a result,True, it isFalse
orOr forFalseAs a result,FalseIt isTrue
notThe notTrue->False False->True
b = 10>6 and False
b = 10>6 or False
b = 10<6 or False
b = not 10<6
print(b)
5.Bit operation integer -> binary & bit and are both1As a result,1It is0| or are for0As a result,0It is1~ a invert1->0 0->1Different or the same result is0The different outcomes are1
b = 4 & 7 # 100 & 111
b = 4|7 #
b = ~4
b = 5+2&6
b = 5>2&6
print(b)
Copy the code