Python supports the following types of operators:
Arithmetic operators are things like addition, subtraction, multiplication, division, and remainder.
The following uses actual code to demonstrate the use of all arithmetic operators in Python:
1. a =212.b =103.c =04.5.c = a + b
6. print("1 - a +The value of c after b is:", c)7.8.c = a - b
9. print("2 - a -The value of c after b is:", c)10.11.c = a * b
12. print("3 - a *The value of c after b is:", c)13.14.c = a / b
15. print("4 - a /The value of c after b is:", c)16.17.c = a % b
18. print("5 - a %The value of c after b is:", c)19.20.#Re-assign variables: a, b, c
21. a =222.b =323.c = a ** b
24. print("6 -2 to the power of 3 is:", c)25.26.a =1027.b =628.c = a // b29.print("7 -The integer part of 10 divided by 6 is:", c)
operation result:
The value of c after 1-a + b is: 31
The value of c after 2-a-b is: 11
3-the value of c after a * b: 210
4-The value of c after a / b is: 2.1
5-The value of c after a% b is: 1
6-2 to the power of 3 is: 8
The integer part of 7-10 divided by 6 is: 1
The comparison operator will have a boolean return value
The following uses actual code to demonstrate the use of all comparison operators in Python:
1. a =212.b =103.c =04.5.if(a == b):6.print("1 -a is equal to b")7.else:8.print("1 -a is not equal to b")9.10.if(a != b):11.print("2 -a is not equal to b")12.else:13.print("2 -a is equal to b")14.15.if(a < b):16.print("3 -a is less than b")17.else:18.print("3 -a is greater than or equal to b")19.20.if(a > b):21.print("4 -a is greater than b")22.else:23.print("4 -a is less than or equal to b")24.25.#Modify the values of variables a and b
26. a =5;27.b =20;28.if(a <= b):29.print("5 -a is less than or equal to b")30.else:31.print("5 -a is greater than b")32.33.if(b >= a):34.print("6 -b is greater than or equal to a")35.else:36.print("6 -b is less than a")
operation result:
1-a is not equal to b
2-a is not equal to b
3-a is greater than or equal to b
4-a is greater than b
5-a is less than or equal to b
6-b is greater than or equal to a
We have used the assignment operator a long time ago. As for the compound assignment operator, it combines arithmetic operators and performs an arithmetic operation while assigning.
The following uses actual code to demonstrate the use of all assignment operators in Python:
1. a =212.b =103.c =04.5.c = a + b
6. print("1 -The value of c is:", c)7.8.c += a
9. print("2 -The value of c is:", c)10.11.c *= a
12. print("3 -The value of c is:", c)13.14.c /= a
15. print("4 -The value of c is:", c)16.17.c =218.c %= a
19. print("5 -The value of c is:", c)20.21.c **= a
22. print("6 -The value of c is:", c)23.24.c //= a25.print("7 -The value of c is:", c)
operation result:
1-the value of c: 31
2-the value of c: 52
3-The value of c is: 1092
4-the value of c: 52.0
5-The value of c is: 2
6-the value of c: 2097152
7-the value of c: 99864
The smallest unit of a computer is a bit. Bitwise operators treat numbers as binary to perform calculations. In general, operators are not used much. Only some special cases will use in-place operators, as shown in the following figure:
In the following table, the variable a is 60 and b is 13 and the binary format is as follows:
a = 0011 1100
b = 0000 1101
E.g:
&: And, if both parties are 1, it will be 1, and if one is 0, it will be 0; for example:
0000 0111
0001 0111
result:
0000 0111
| : Or, if one party is 1, then it is 1, and both parties are 0 to be 0; for example:
0001 0101
1001 0111
result:
1001 0111
~ :None, negate, 0 negates 1 and 1 negates 0; for example
0000 0000 0000 0000 0000 0000 1001 0111
result:
1111 1111 1111 1111 1111 1111 0110 1000
^ : Exclusive OR, similar to not equal, if both sides are 1, then it is 0, whether both sides are 0 or 0,
If neither party is 1 nor 0 is 1; for example:
0001 0101
1001 0111
result:
1000 0010
">> ": Move right, move two digits to the right, complement 1 for high digits of negative numbers, and 0 for high digits of positive numbers; for example:
Positive number: 6 >> 2
0000 0110
result:
0000 0001
negative number:
1111 1111 1111 1111 1111 1111 1111 1010
result:
1111 1111 1111 1111 1111 1111 1111 1110
"<< ": Shift left, two digits to the left, both positive and negative numbers are the last digit with 0; for example:
Positive number: 6 << 2
0000 0110
result:
0001 1000
negative number:
1111 1111 1111 1111 1111 1111 1111 1010
result:
1111 1111 1111 1111 1111 1111 1110 1000
**Code example: **
1. a =60 # 60=001111002.b =13 # 13=000011013.c =04.5.c = a & b; # 12=000011006.print("1 -The value of c is:", c)7.8.c = a | b; # 61=001111019.print("2 -The value of c is:", c)10.11.c = a ^ b; # 49=0011000112.print("3 -The value of c is:", c)13.14.c =~a; # -61=1100001115.print("4 -The value of c is:", c)16.17.c = a <<2; # 240=1111000018.print("5 -The value of c is:", c)19.20.c = a >>2; # 15=0000111121.print("6 -The value of c is:", c)
operation result:
1-the value of c: 12
2-The value of c is: 61
3-The value of c is: 49
4-The value of c: -61
5-the value of c: 240
6-the value of c: 15
Python supports logical operators, and other languages use &&, ||,! To represent and or not. In Python, and, or, not are used to represent and or not
The following assumes that the variable a is 10 and b is 20
E.g:
and: and means at the same time: compare whether the values of both parties are true, as long as one of the values is false, the result is false. E.g:
A B result
true and false false
false and true false
true and true true
false and false false
When the and operator is used, the previous value is true, and the latter will continue to run to see if it is true. If the previous is false, the latter will not run again because the condition is no longer established.
or: Or, the meaning of yes or: compare the values of the two parties to see if one of them is true, as long as one of the values is true, the result is true. E.g:
A B result
true or false true
false or true true
true or true true
false or false false
When the or operator is used, the previous value is true, the latter will not run, because the condition has been established; if the previous is false, then continue to run to see if it is true or false.
not: No, it means negation: as long as the value is true, the result is false, and the result of false is true. It means the other way around. E.g:
A not A result
true not false
false not true
**Code example: **
1. a =102.b =203.4.if(a and b):5.print("1 -Variables a and b are both true")6.else:7.print("1 -One of the variables a and b is not true")8.9.if(a or b):10.print("2 -Variables a and b are both true,或其中一个变量为 true")11.else:12.print("2 -Variables a and b are not true")13.14.15.16.#Modify the value of variable a
17. a =018.if(a and b):19.print("3 -Variables a and b are both true")20.else:21.print("3 -One of the variables a and b is not true")22.23.if(a or b):24.print("4 -Variables a and b are both true,或其中一个变量为 true")25.else:26.print("4 -Variables a and b are not true")27.28.ifnot(a and b):29.print("5 -Variables a and b are both false, or one of the variables is false")30.else:31.print("5 -Variables a and b are both true")
operation result:
1-Both variables a and b are true
2-Variables a and b are both true, or one of the variables is true
3-One of the variables a and b is not true
4-Variables a and b are both true, or one of the variables is true
5-Variables a and b are both false, or one of the variables is false
In addition to some of the above operators, Python also supports member operators. Member operators are used to find whether a value is in an array (list) or is not in an array (list)
The following uses actual code to demonstrate the use of all member operators in Python:
1. a =102.b =203.list =[1,2,3,4,5];4.5.if(a in list):6.print("1 -The variable a is in the given list")7.else:8.print("1 -The variable a is not in the given list")9.10.if(b not in list):11.print("2 -The variable b is not in the given list")12.else:13.print("2 -The variable b is in the given list")14.15.#Modify the value of variable a
16. a =217.if(a in list):18.print("3 -The variable a is in the given list")19.else:20.print("3 -The variable a is not in the given list")
operation result:
1-Variable a is not in the given list
2-Variable b is not in the given list
3-The variable a is in the given list
The identity operator is used to compare whether the reference objects of two variables are the same or not the same
**Note: ** id() function is used to get the memory address of the object.
The following uses actual code to demonstrate the use of all identity operators in Python:
1. a =202.b =203.4.if(a is b):5.print("1 -a and b have the same reference object")6.else:7.print("1 -a and b do not have the same reference object")8.9.if(id(a)==id(b)):10.print("2 -a and b have the same reference object")11.else:12.print("2 -a and b do not have the same reference object")13.14.#Modify the value of variable b
15. b =3016.if(a is b):17.print("3 -a and b have the same reference object")18.else:19.print("3 -a and b do not have the same reference object")20.21.if(a is not b):22.print("4 -a and b do not have the same reference object")23.else:24.print("4 -a and b have the same reference object")
operation result:
1-a and b have the same reference object
2-a and b have the same reference object
3-a and b do not have the same reference object
4-a and b do not have the same reference object
The difference between is and ==:
is is used to determine whether two variable reference objects are the same, == is used to determine whether the value of the reference variable is equal.
1.>>> a =[1,2,3]2.>>> b = a
3.>>> b is a
4. True
5.>>> b == a
6. True
7.>>> b = a[:]8.>>> b is a
9. False10.>>> b == a
11. True
The following example demonstrates the operation of all operator precedence in Python:
1. a =202.b =103.c =154.d =55.e =06.7.e =(a + b)* c / d #(30*15)/58.print("(a + b) * c /d The result of the operation is:", e)9.10.e =((a + b)* c)/ d # (30*15)/511.print("((a + b) * c) /d The result of the operation is:", e)12.13.e =(a + b)*(c / d); # (30)*(15/5)14.print("(a + b) * (c / d)The result of the operation is:", e)15.16.e = a +(b * c)/ d; # 20+(150/5)17.print("a + (b * c) /d The result of the operation is:", e)
operation result:
(a + b) * c / d The result of the operation is: 90.0
((a + b) * c) / d The result of the operation is: 90.0
(a + b) * (c / d) The result of the operation is: 90.0
a + (b * c) / d The result of the operation is: 50.0
Recommended Posts