• By Han Xinzi @Showmeai
  • Tutorial address: www.showmeai.tech/tutorials/5…
  • This paper addresses: www.showmeai.tech/article-det…
  • Statement: All rights reserved, please contact the platform and the author and indicate the source

1. The Python operator

Operators are used to perform operations on variables and values. Let’s take a simple example: 5 +6 = 11. In this example, 5 and 6 are called operands, and “+” is the operator.

The Python language supports the following types of operators:

  • Arithmetic operator
  • Compare (relational) operators
  • The assignment operator
  • Logical operator
  • An operator
  • Member operator
  • Identity operator
  • Operator priority

2.Python arithmetic operators

Assume the following variables: A =10, b=20:

The operator describe The instance
+ Add – Add two objects A + b Output 30
Subtract – to get a negative number or subtract something from something else A-b The output is -10
* Multiply – Multiply two numbers together or return a string that has been repeated several times The output of a * b is 200
/ Over minus x over y B/a Output 2
% Modulo – Returns the remainder of the division B % a Displays 0
** Power – Returns x to the y power A **b is 10 to the 20th power, and the output is 1000000000000000000000000
// Take an exact division – return the integer part of the quotient (Take down the whole) >>> 9//2 4 >>> -9//2 -5

The following code demonstrates the operation of all of Python’s arithmetic operators (code can be run in an online Python3 environment) :

a = 30
b = 10
c = 0
 
c = a + b
print("After the first operation, c is:", c)
 
c = a - b
print("After the second operation, c is:", c)
 
c = a * b
print("After the third operation, c is:", c)
 
c = a / b
print("The value of c after the fourth operation is:", c)
 
c = a % b
print("The value of c after the fifth operation is:", c)
 
Change variables A, B, and c
a = 2
b = 3
c = a**b 
print("The value of c after the sixth operation is:", c)
 
a = 10
b = 5
c = a//b 
print("The value of c after the seventh operation is:", c)
Copy the code

Output from the above code:

After the first operation, the value of c is 40 after the second operation, the value of C is 20 after the third operation, the value of C is 300 after the fourth operation, the value of C is 3.0 after the fifth operation, the value of C is 0 after the sixth operation, the value of C is 8 after the seventh operation, the value of C is 2Copy the code

3.Python comparison operators

Assume that variable A is 10 and variable B is 20:

The operator describe The instance
= = Equal – Whether the objects are equal Return False.
! = Not equal – Compares whether two objects are not equal (a ! = b) returns true.
<> Not equal – Compares whether two objects are not equal.Python3 is obsolete. (a <> b) returns true. This operator is similar to! =.
> Greater than – Returns whether x is greater than y A > b returns False.
< Less than – Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. This is equivalent to the special variables True and False respectively. (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 or equal to – Returns whether x is less than or equal to y. (a <= b) returns true.

The following code demonstrates the operation of all Python comparison operators (the code can be run in an online Python3 environment) :

a = 30
b = 10
c = 0
 
if  a == b :
   print("a 等于 b")
else:
   print("A is not equal to B")
 
ifa ! = b :print("A is not equal to B")
else:
   print("a 等于 b")
 
if  a < b :
   print("A less than b" )
else:
   print("A is greater than or equal to B")
 
if  a > b :
   print("A greater than b")
else:
   print("A is less than or equal to B")
 
Change the values of variables A and b
a = 5
b = 20
if  a <= b :
   print("A is less than or equal to B")
else:
   print("A greater than b")
 
if  b >= a :
   print("B is greater than or equal to A")
else:
   print(Less than a "b")
Copy the code

The output of the above example is as follows:

A is not equal to b a is not equal to b A is greater than or equal to b a is less than or equal to b b is greater than or equal to aCopy the code

4.Python assignment operator

Assume that variable A is 10 and variable B is 20:

The operator describe The instance
= A simple assignment operator C = a + b Assigns the result of a + b to c
+ = The addition assignment operator C plus a is the same thing as c is equal to c plus a
– = The subtraction assignment operator C minus a is the same thing as c minus a
* = Multiplication assignment operator C times a is the same thing as c times a
/ = The division assignment operator C/a is the same thing as c = c/a
% = Take the modulo assignment operator C %= a is the same thing as c = c % a
* * = Power assignment operator C **= a is the same thing as c = c ** A
/ / = Takes the divisible assignment operator C //= a is equivalent to c = c // a

The following code demonstrates all of Python’s assignment operators (code can be run in an online Python3 environment) :

a = 30
b = 10
c = 0
 
c = a + b
print("After the first operation, c is:", c)
 
c += a
print("After the second operation, c is:", c )
 
c *= a
print("After the third operation, c is:", c )
 
c /= a 
print("The value of c after the fourth operation is:", c )
 
c = 2
c %= a
print("The value of c after the fifth operation is:", c)
 
c **= a
print("The value of c after the sixth operation is:", c)
 
c //= a
print("The value of c after the seventh operation is:", c)
Copy the code

Output from the above code:

After the first operation, the value of C is 40. After the second operation, the value of C is 70. After the third operation, the value of C is 2100. After the fourth operation, the value of C is 70.0.  35791394Copy the code

5.Python bitwise operators

Bitwise operators evaluate numbers as if they were binary. Bitwise operations in Python are as follows:

In the following table, variable A is 60 and variable B is 13. 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
Copy the code
The operator describe The instance
& Bitwise and operator: Two values involved in the operation, the result of which is 1 if both corresponding bits are 1, and 0 otherwise (a & b) Output 12, binary interpretation: 0000 1100
| Bitwise or operator: if either of the two corresponding binary digits is 1, the result bit is 1. (a | b) output 61, binary interpretation: 0011 1101
^ Bitwise xor operator: When two corresponding binary bits differ, the result is 1 (a ^ b) 49, binary interpretation: 0011 0001
~ Bitwise invert operator: Invert each binary bit of data, that is, changing 1 to 0 and 0 to 1.~xSimilar to the-x-1 (~a) Output result -61, binary interpretation: 1100 0011, in the form of a signed binary number’s complement.
<< Left shift operator: all the binary bits of an operand move to the left by a number of bits<<The number on the right specifies the number of digits to move. The highest digit is discarded and the lowest digit is filled with zeros. A << 2 output result 240, binary interpretation: 1111 0000
>> Right shift operator: to move all the binary digits of the operand to the left of “>>” several right,>>The number on the right specifies the number of digits to move A >> 2 Output 15, binary interpretation: 0000 1111

The following code demonstrates the operation of all Python bit-operators (code can be run in an online PYTHon3 environment) :

a = 60            # 60 = 0011 1100 
b = 13            # 13 = 0000 1101 
c = 0
 
c = a & b;        # 12 = 0000 1100
print("After the first operation, c is:", c)
 
c = a | b;        # 61 = 0011 1101 
print("After the second operation, c is:", c)
 
c = a ^ b;        # 49 = 0011 0001
print("After the third operation, c is:", c)
 
c = ~a;           # -61 = 1100 0011
print("The value of c after the fourth operation is:", c)
 
c = a << 2;       # 240 = 1111 0000
print("The value of c after the fifth operation is:", c)
 
c = a >> 2;       # 15 = 0000 1111
print("The value of c after the sixth operation is:", c)
Copy the code

Output from the above code:

After the first operation, the value of c is: 12 after the second operation, the value of C is: 61 after the third operation, the value of C is: 49 after the fourth operation, the value of C is: -61 After the fifth operation, the value of C is: 240 after the sixth operation, the value of C is: 15Copy the code

6.Python logical operators

The Python language supports logical operators, assuming variables A are 10 and b is 20:

The operator Logical expression describe The instance
and x and y Boolean “and” – x and y returns False if x is False, otherwise it returns the computed value of y. A and b return 20.
or x or y Boolean “or” – it returns the calculated value of x if x is non-zero, otherwise it returns the calculated value of y. A or b returns 10.
not not x Boolean “not” – returns False if x is True. If x is False, it returns True. Not (a and b) returns False

The following code demonstrates the operation of all of Python’s logical operators (code can be run in an online Python3 environment) :

a = 10
b = 20
 
if  a and b :
   print("1. Variables A and B are true")
else:
   print("1. Neither variable A nor variable B is true")
 
if  a or b :
   print("2. Both variables a and B are true, or one of them is true")
else:
   print("2. Variables A and B are not true")
 
Change the value of variable A
a = 0
if  a and b :
   print("3. Variables A and B are true")
else:
   print("3. Neither variable A nor variable B is true")
 
if  a or b :
   print("4. Both variables A and B are true, or one of them 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 them is false")
else:
   print("5. Variables A and B are true")
Copy the code

Output from the above code:

1. Both a and B are true 2. Both a and B are true, or one of the variables is true 3. Either of the variables a and B are not true 4. Either of the variables A and B are true, or one of them is true 5. The variables a and b are both false, or one of them is falseCopy the code

7.Python member operators

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 operator describe The instance
in Returns True if a value is found in the specified sequence, False otherwise. X is in the y sequence and returns True if x is in the y sequence.
not in Returns True if no value was found in the specified sequence, False otherwise. X is not in the y sequence, and returns True if x is not in the y sequence.

The following code demonstrates the operation of all Python member operators (code can be run in an online Python3 environment) :

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")
 
Change the value of variable A
a = 2
if ( a in list) :print("3. Variable A is in the given list")
else:
   print("3. Variable A is not in the given list")
Copy the code

Output from the above code:

2. Variable B is not in the given list. 3. The variable a is in the given list listCopy the code

8.Python identity operators

The identity operator is used to compare the storage units of two objects

The operator describe The instance
is Is determines whether two identifiers refer to an object x is y, similar toid(x) == id(y)Returns True if the same object is referenced, False otherwise
is not Is not determines whether two identifiers refer to different objects x is not y, similar toid(x) ! = id(y). Returns True if not the same object is referenced, False otherwise.

Note: The id() function is used to get the memory address of an object.

The following code demonstrates the operation of all of Python’s identity operators (code can be run in an online Python3 environment) :

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")
 
Change 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 identification")
 
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")
Copy the code

The output of the above example is as follows:

A and B have the same identification. 2. A and B have the same identification. 3Copy the code

Is differs from == : IS is used to determine whether two variable reference objects are the same (same block of memory space), and == is used to determine whether the values of reference variables are the same.

>>> a = [1.2.3]
>>> b = a
>>> b is a 
True
>>> b == a
True
>>> b = a[:]
>>> b is a
False
>>> b == a
True
Copy the code

9.Python operator precedence

The following table lists all operators from highest to lowest priority:

The operator describe
** Index (highest priority)
~ + – Bitwise flip, unary plus and minus (the last two methods are called +@ and -@)
% * / / / Multiply, divide, take modulus and take exact division
+ – Addition subtraction
>> << Shift right, shift left operator
& A ‘AND’
^ | An operator
< = < > > = Comparison operator
< > = =! = Equal operator
= %= /= //= -= += *= **= The assignment operator
is is not Identity operator
in not in Member operator
not and or Logical operator

The following code demonstrates the operation of all Python operator priorities (the code can be run in an online Python3 environment) :

a = 20
b = 10
c = 15
d = 5
e = 0
 
e = (a + b) * c / d       So this is equal to 30 times 15 over 5
print((a + b) * c/d,  e)
 
e = ((a + b) * c) / d     So this is equal to 30 times 15 over 5
print((a + b) * c)/d,  e)
 
e = (a + b) * (c / d);    # (30) * (15/5)
print((a + b) * (c/d),  e)
 
e = a + (b * c) / d;      # 20 + (150/5)
print("A + (b * c)/d",  e)
Copy the code

The output of the above example is as follows:

(a + b) * c/d: 90.0 ((a + b) * c)/d: 90.0 (a + b) * (c/d) : 90.0 a + (b * c)/d: 50.0Copy the code

Information and code download

This series of tutorials can be downloaded from Github on ShowMeAI, which can be run locally in Python. If you can use Google Colab, you can also use Google Colab.

The Python sketchbooks covered in this tutorial series can be downloaded at:

  • Python quick table

Expanded Reference materials

  • Python tutorial – Python3 documentation
  • Python Tutorial – Official website of Liao Xuefeng

ShowMeAI related articles recommended

  • Python is introduced
  • Python installation and environment configuration
  • Basic Python syntax
  • Basic Python data types
  • Python operator.
  • Python conditional control with if statements
  • Python loop statements
  • Python while loop
  • The python for loop
  • Python break statement
  • Python continue statement
  • Python pass statement
  • Python strings and operations
  • Python list
  • The python tuple
  • Python dictionary
  • Python set
  • Python functions
  • Python iterators and generators
  • Python data structures
  • Python module
  • Python file reading and writing
  • Python file and directory operations
  • Python error and exception handling
  • Object-oriented programming in Python
  • Python namespaces and scopes
  • Python time and date

ShowMeAI series tutorials recommended

  • Illustrated Python programming: From beginner to Master series of tutorials
  • Illustrated Data Analysis: From beginner to master series of tutorials
  • The mathematical Basics of AI: From beginner to Master series of tutorials
  • Illustrated Big Data Technology: From beginner to master