Click the blue word "python tutorial" to follow us!
What is an operator?
This chapter mainly explains the operators of Python. Take a simple example 4 +5 = 9. In the example, 4 and 5 are called operands, and "+" is called operator.
The Python language supports the following types of operators:
Arithmetic Operator
Comparison (relational) operators
Assignment operator
Logical Operators
Bit operator
Member operator
Identity operator
Operator precedence
Next, let us learn Python operators one by one.
Python arithmetic operators
The following hypothetical variables: a=10, b=20:
The following example demonstrates the operation of all arithmetic operators in Python:
Instance
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 21 b = 10 c = 0 c = a + b print "1-the value of c is:", c c = a-b print "2-the value of c is:", c c = a * b print "3-the value of c is:", c c = a/b print "4-the value of c is:", c c = a% b print "5-the value of c is:", c # Modify variables a, b, c a = 2 b = 3 c = a**b print "6-the value of c is:", c a = 10 b = 5 c = a//b print "7-the value of c is:", c ''' In the learning process, if you don’t know anything, you can add my python learning exchange button qun, 934109170 There are good learning tutorials, development tools and e-books in the group. Share with you the current talent needs of python companies and how to learn python from a zero basis, and what to learn. '''
The output of the above example:
Note: In Python 2.x, dividing an integer by an integer can only get an integer. If you want to get the fractional part, just change one of the numbers to a floating point number.
>>> 1/2 0 >>> 1.0/2 0.5 >>> 1/float(2) 0.5
The following assumes that the variable a is 10 and the variable b is 20:
The following example demonstrates the operation of all comparison operators in Python:
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 21 b = 10 c = 0 if a == b: print "1-a is equal to b" else: print "1-a is not equal to b" if a != b: print "2-a is not equal to b" else: print "2-a is equal to b" if a <> b: print "3-a is not equal to b" else: print "3-a is equal to b" if a <b: print "4-a is less than b" else: print "4-a is greater than or equal to b" if a> b: print "5-a is greater than b" else: print "5-a is less than or equal to b" # Modify the values of variables a and b a = 5 b = 20 if a <= b: print "6-a is less than or equal to b" else: print "6-a is greater than b" if b >= a: print "7-b is greater than or equal to a" else: print "7-b is less than a"
The output of the above example:
The following assumes that the variable a is 10 and the variable b is 20:
The following example demonstrates the operation of all assignment operators in Python:
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 21 b = 10 c = 0 c = a + b print "1-the value of c is:", c c += a print "2-the value of c is:", c c *= a print "3-the value of c is:", c c/= a print "4-the value of c is:", c c = 2 c %= a print "5-the value of c is:", c c **= a print "6-the value of c is:", c c//= a print "7-the value of c is:", c
The output of the above example:
Bitwise operators treat numbers as binary to perform calculations. The bitwise algorithm in Python is as follows:
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 ----------------- a&b = 0000 1100 a|b = 0011 1101 a^b = 0011 0001 ~a = 1100 0011
The following example demonstrates the operation of all bitwise operators in Python:
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100 print "1-the value of c is:", c c = a | b; # 61 = 0011 1101 print "2-the value of c is:", c c = a ^ b; # 49 = 0011 0001 print "3-the value of c is:", c c = ~a; # -61 = 1100 0011 print "4-the value of c is:", c c = a << 2; # 240 = 1111 0000 print "5-the value of c is:", c c = a >> 2; # 15 = 0000 1111 print "6-the value of c is:", c
The output of the above example:
Python language supports logical operators. The following assumes that the variable a is 10 and b is 20:
The output of the above example:
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 10 b = 20 if a and b: print "1-variables a and b are both true" else: print "1-One of the variables a and b is not true" if a or b: print "2-Both variables a and b are true, or one of the variables is true" else: print "2-Variables a and b are not true" # Modify the value of variable a a = 0 if a and b: print "3-variables a and b are both true" else: print "3-One of the variables a and b is not true" if a or b: print "4-Both variables a and b are true, or one of the variables is true" else: print "4-variables a and b are not true" if not( a and b ): print "5-Both variables a and b are false, or one of the variables is false" else: print "5-variables a and b are both true"
The output of the above example:
In addition to some of the above operators, Python also supports member operators. The test instance contains a series of members, including strings, lists, or tuples.
The following example demonstrates the operation of all member operators in Python:
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 10 b = 20 list = [1, 2, 3, 4, 5 ]; if (a in list ): print "1-variable a is in the given list" else: print "1-variable a is not in the given list" if (b not in list ): print "2-variable b is not in the given list" else: print "2-variable b is in the given list" # Modify the value of variable a a = 2 if (a in list ): print "3-variable a is in the given list in list" else: print "3-variable a is not in the given list"
The output of the above example:
Identity operator is used to compare the storage units of two objects
Note: The id() function is used to obtain the memory address of the object.
The following example demonstrates the operation of all identity operators in Python:
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 20 b = 20 if (a is b ): print "1-a and b have the same logo" else: print "1-a and b do not have the same logo" if (a is not b ): print "2-a and b do not have the same logo" else: print "2-a and b have the same logo" # Modify the value of variable b b = 30 if (a is b ): print "3-a and b have the same logo" else: print "3-a and b do not have the same logo" if (a is not b ): print "4-a and b do not have the same logo" else: print "4-a and b have the same logo"
The output of the above example:
The difference between is and ==:
is is used to determine whether the two variable reference objects are the same (the same memory space), == is used to determine whether the value of the reference variable is equal.
>>> a = [1, 2, 3] >>> b = a >>> b is a True >>> b == a True >>> b = a[:] >>> b is a False >>> b == a True
The following table lists all operators from highest to lowest precedence:
The following example demonstrates the operation of all operator precedence in Python:
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c/d #( 30 * 15)/5 print "(a + b) * c/d The result of the operation is: ", e e = ((a + b) * c)/d # (30 * 15)/5 print "((a + b) * c)/d The result of the operation is: ", e e = (a + b) * (c/d); # (30) * (15/5) print "(a + b) * (c/d) The result of the operation is: ", e e = a + (b * c)/d; # 20 + (150/5) print "a + (b * c)/d The result of the operation is:", e
The output of the above example:
Precautions
01
Students who are interested in Python development technology, welcome to join the communication group below to learn together and discuss with each other.
02
If you don’t understand in the process of learning python, you can join my python zero-base system to learn and exchange Qiuqiu qun: 934109170, to share with you the current talent needs of Python companies and how to learn Python from a zero-base, and what to learn. Related learning video materials and development tools are shared
Alright! The article is shared with the readers here
Finally, if you find it helpful, remember to follow, forward, and favorite