Notes
A single line comment in Python begins with #, for example:
# This is a comment. print("Hello, World!")
Multi-line comments are enclosed with three single quotation marks'', or three double quotation marks', for example:
''' This is a multi-line comment with three single quotes This is a multi-line comment with three single quotes This is a multi-line comment with three single quotes ''' print("Hello, World!")
""" This is a multi-line comment with three double quotes This is a multi-line comment with three double quotes This is a multi-line comment with three double quotes """ print("Hello, World!")
operator
Arithmetic operator
Compare (relational) operators
Assignment Operators
Logical Operator
Bitwise Operators
member operator
Identity Operator
Operator priority
Python arithmetic operator
+ Plus
- subtraction
* multiplying
/ Division
% Modeling
* power
// take and divide
a = 21 b = 10 c = 0 c = a + b print("1 - c The value is:", c) c = a - b print("2 - c The value is:", c) c = a * b print("3 - c The value is:", c) c = a / b print("4 - c The value is:", c) c = a % b print("5 - c The value is:", c) # Modify variables a, b, c a = 2 b = 3 c = a ** b print("6 - c The value is:", c) a = 10 b = 5 c = a // b print("7 - c The value is:", c)
The output of the above example is as follows:
The value of 1 - c is 31
The value of 2 - c is 11
The value of 3 - c is 210
The value of 4 - c is: 2.1
The value of 5 - c is: 1
The value of 6 - c is 8
The value of 7 - c is: 2
Python comparison operator
== Equivalent to - Compare Object Equivalence
!= Not equal - Compare two objects to see if they are not equal
> Is greater than - Return x greater than y
<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. Note that these variable names are capitalized.
>= greater than or equal to - Returns whether x is greater than or equal to y.
<= less than or equal to - Returns whether x is less than or equal to y.
a = 21 b = 10 c = 0 if ( a == b ): print ("1 - a Be equal to b") else: print ("1 - a Not equal to b") if ( a != b ): print ("2 - a Not equal to b") else: print ("2 - a Be equal to b") if ( a < b ): print ("3 - a less than b") else: print ("3 - a Greater than or equal to b") if ( a > b ): print ("4 - a greater than b") else: print ("4 - a Less than or equal to b") # Modify the values of variables a and b a = 5; b = 20; if ( a <= b ): print ("5 - a Less than or equal to b") else: print ("5 - a greater than b") if ( b >= a ): print ("6 - b Greater than or equal to a") else: print ("6 - b less than a")
The output of the above example is as follows:
1 - a is not equal to b
2 - a is not equal to b
3 - a greater than or equal to b
4 - a is greater than b
5 - a is less than or equal to b
6 - b greater than or equal to a
Python assignment operator
= A simple assignment operator c = a + b assigns the result of a + b to C
+= The addition assignment operator c += a is equivalent to c = c + a
-= The subtraction assignment operator c -= a is equivalent to c = c - a
*= Multiplication assignment operator c*= A is equivalent to C = c* a
/= The division assignment operator c/= A is equivalent to c = c / a
%= The modular assignment operator c% = a is equivalent to C = c% = a
**= Power assignment operator c**= A is equivalent to C = c** a
//= The integer division assignment operator c /= A is equivalent to c = c // a
a = 21 b = 10 c = 0 c = a + b print ("1 - c The value is:", c) c += a print ("2 - c The value is:", c) c *= a print ("3 - c The value is:", c) c /= a print ("4 - c The value is:", c) c = 2 c %= a print ("5 - c The value is:", c) c **= a print ("6 - c The value is:", c) c //= a print ("7 - c The value is:", c)
The output of the above example is as follows:
The value of 1 - c is 31
The value of 2 - c is 52
The value of 3 - c is: 1092
The value of 4 - c is 52.0
The value of 5 - c is: 2
The value of 6 - c is 2097 152
7 - c: 99864
Python logical operator
x and y Boolean "and" - If x is False, x and y returns False, otherwise it returns the calculated value of y. (a and b) Return 20.
Or x or y Boolean "or" - If x is True, it returns the value of x, otherwise it returns the calculated value of y. (a or b) Return 10.
not x Boolean "non" - If x is True, return False. If x is False, it returns True. not(a and b) returns to False
a = 10 b = 20 if ( a and b ): print ("1 - variable a and b All for true") else: print ("1 - variable a and b There is an inaction. true") if ( a or b ): print ("2 - variable a and b All for true,Or one of the variables is true") else: print ("2 - variable a and b None of them can do it. true") # Modify the value of variable a a = 0 if ( a and b ): print ("3 - variable a and b All for true") else: print ("3 - variable a and b There is an inaction. true") if ( a or b ): print ("4 - variable a and b All for true,Or one of the variables is true") else: print ("4 - variable a and b None of them can do it. true") if not( a and b ): print ("5 - variable a and b All for false,Or one of the variables is false") else: print ("5 - variable a and b All for true")
The output of the above example is as follows:
1 - Variables a and b are true
2 - Variables a and b are true, or one of them is true
3 - Variables a and b have one not true
4 - Variables a and b are true, or one of them is true
5 - Variables a and b are false, or one of them is false
Python member operator
In. Returns True if a value is found in the specified sequence, or False. X is in the y sequence, if x returns True in the y sequence.
Not in returns True if no value is found in the specified sequence, or False. X is not in the y sequence, if x is not in the y sequence, return True.
a = 10 b = 20 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print ("1 - variable a In a given list list in") else: print ("1 - variable a Not in the given list list in") if ( b not in list ): print ("2 - variable b Not in the given list list in") else: print ("2 - variable b In a given list list in") # Modify the value of variable a a = 2 if ( a in list ): print ("3 - variable a In a given list list in") else: print ("3 - variable a Not in the given list list in")
The output of the above example is as follows:
1 - Variable a is not in the list in the given list
2 - Variable b is not in a given list
3 - Variable a in a given list
Python identity operator
Is to determine whether two identifiers are referenced from an object, x is y, similar to id(x) == id(y), if the same object is referenced, return True, otherwise return False
It is not to determine whether two identifiers are referenced from different objects, similar to ID (a)!= ID (b). If the reference is not the same object, the result True is returned, otherwise False is returned.
a = 20 b = 20 if ( a is b ): print ("1 - a and b Have the same logo") else: print ("1 - a and b No identical identification") if ( id(a) == id(b) ): print ("2 - a and b Have the same logo") else: print ("2 - a and b No identical identification") # 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 No identical identification") if ( a is not b ): print ("4 - a and b No identical identification") else: print ("4 - a and b Have the same logo")
The output of the above example is as follows:
1 - a and b have identical identities
2 - a and b have identical identities
3 - a and b do not have the same identification
4 - a and b do not have the same identification
Python Operator Priority
The following table lists all operators from the highest to the lowest priority:
** Index (highest priority)
~+- Bit-by-bit flip, one dollar plus and minus (the last two methods are called +@and-@)
*/%// Multiplication, division, modularization and division
+- Addition and subtraction
"< < Right Shift, Left Shift Operator
& bit `AND'
^| Bit Operator
<= < >= comparison operator
<>==!= equal to operator
=%=/=//=-=+=*= **= assignment operator
Is not an identity operator
in not in member operator
not and or logical operators
a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print ("(a + b) * c / d The results are as follows:", e) e = ((a + b) * c) / d # (30 * 15 ) / 5 print ("((a + b) * c) / d The results are as follows:", e) e = (a + b) * (c / d); # (30) * (15/5) print ("(a + b) * (c / d) The results are as follows:", e) e = a + (b * c) / d; # 20 + (150/5) print ("a + (b * c) / d The results are as follows:", e)
The output of the above example is as follows:
(a + b) * c / d operation results are: 90.0
((a + b) * c) / d operation results are: 90.0
(a + b) * (c / d) operation results are: 90.0
The result of a + (b * c) / d operation is: 50.0