The following hypothetical variables :a = 10 ,b = 20
Operator
describe
example
+ Add ( Add two objects )a+b The output is 30- reduce ( Get a negative number or a number minus another number )a-b The output is -10* ride ( Multiply two numbers or return a string repeated several times )a*b The output is 200/ except (x Divide y)b/a The output is 2% modulus ( Returns the remainder of the Division )b%a The output is 0** power ( return x Of y The next power )a**b by 10 Of 20 Power // Divide and conquer ( Returns the integer part of the quotient - Rounding down )9//2 The output is 4 -9//4 The output is -5
Here is Python Code :
a = 21
b = 10
c = 0
c = a + b
print("1 - c The value of is :", c)
c = a - b
print("2 - c The value of is :", c)
c = a * b
print("3 - c The value of is :", c)
c = a / b
print("4 - c The value of is :", c)
c = a % b
print("5 - c The value of is :", c)
# Modify variables a 、b 、c
a = 2
b = 3
c = a**b
print("6 - c The value of is :", c)
a = 10
b = 5
c = a//b
print("7 - c The value of is :", c)
Running results :
Two 、Python Comparison operator
The following hypothetical variables :a = 10 ,b = 20
Operator
describe
example
== be equal to ( Compare two objects for equality )(a == b) return False!= It's not equal to ( Compare two objects for inequality )(a != b) return True> Greater than ( return x Is it greater than y)(a > b) return False< Less than ( return x Is less than y)(a < b) return True>= Greater than or equal to ( return x Greater than or equal to y)(a >= b) return False<= Less than or equal to ( return x Less than or equal to y)(a <= b) return True
All comparison operators return 1 Said really , return 0 Said the false . This is different from special variables True and False Equivalent ( Note the case of these variable names ).
Here is Python Code :
a = 21
b = 10
c = 0
if ( a == b ):
print ("1 - a be equal to b")
else:
print ("1 - a It's not equal to b")
if ( a != b ):
print ("2 - a It's 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 variables a and b Value
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")
Running results :
3、 ... and 、Python Assignment operator
The following hypothetical variables :a = 10 ,b = 20
Operator
describe
example
= Simple assignment operators c = a + b take a + b The result of the operation is assigned to c+= Addition assignment operator c += a Equivalent to c = c + a-= Subtraction assignment operator c -= a Equivalent to c = c - a*= Multiplication assignment operator c *= a Equivalent to c = c * a/= Division assignment operator c /= a Equivalent to c = c / a%= Modulo assignment operator c %= a Equivalent to c = c % a**= Power assignment operator c **= a Equivalent to c = c ** a//= Rounding assignment operators c //= a Equivalent to c = c // a
Here is Python Code :
a = 21
b = 10
c = 0
c = a + b
print ("1 - c The value of is :", c)
c += a
print ("2 - c The value of is :", c)
c *= a
print ("3 - c The value of is :", c)
c /= a
print ("4 - c The value of is :", c)
c = 2
c %= a
print ("5 - c The value of is :", c)
c **= a
print ("6 - c The value of is :", c)
c //= a
print ("7 - c The value of is :", c)
Running results :
Four 、Python An operator
Bitwise operators It calculates numbers as binary .
a = 60,b = 13 Its binary format is as follows : a = 0011 1100 b = 0000 1101
Operator
describe
example
& Bitwise and operator : Two values involved in the operation , If both corresponding bits are 1, The result of this bit is 1, Otherwise 0(a & b) The output is 12 , Binary interpretation :0000 1100| bitwise or operator : Two values involved in the operation , As long as one of the two corresponding binary digits is 1, The result bit is 1(a | b) The output is 61 , Binary interpretation :0011 1101^ bitwise exclusive or operator : When two corresponding bits are different , The result is 1(a ^ b) The output is 49 , Binary interpretation :0011 0001~ Bitwise negation operator : Invert each binary bit of data , Namely the 1 Turn into 0, hold 0 Turn into 1. ~x Be similar to -x-1(~a) The output is -61 , Binary interpretation :1100 0011<< Left move operator : All the binary bits of the operand are shifted to the left by several bits , from “<<” The number on the right specifies the number of digits to move , High level discard , Low complement 0a << 2 The output is 240 , Binary interpretation :1111 0000>> Move right operator : hold “>>” Each binary of the left operand is shifted several bits to the right ,“>>” The number on the right specifies the number of digits to move a >> 2 The output is 15 , Binary interpretation :0000 1111
Here is Python Code :
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0
c = a & b; # 12 = 0000 1100
print ("1 - c The value of is :", c)
c = a | b; # 61 = 0011 1101
print ("2 - c The value of is :", c)
c = a ^ b; # 49 = 0011 0001
print ("3 - c The value of is :", c)
c = ~a; # -61 = 1100 0011
print ("4 - c The value of is :", c)
c = a << 2; # 240 = 1111 0000
print ("5 - c The value of is :", c)
c = a >> 2; # 15 = 0000 1111
print ("6 - c The value of is :", c)
Running results :
5、 ... and 、Python Logical operators
The following hypothetical variables :a = 10 ,b = 20
Operator
Logical expression
describe
example
andx and y Boolean “ And ”( If x by False ,x and y return False , Otherwise return to y Calculated value )(a and b) return 20orx or y Boolean “ or ”( If x yes True , Then return to x Value , Otherwise return to y Calculated value )(a or b) return 10notnot x Boolean “ Not ”( If x by True , Then return to False . If x by False , Then return to True )not(a and b) return False
Here is Python Code :
a = 10
b = 20
if ( a and b ):
print ("1 - Variable a and b All for true")
else:
print ("1 - Variable a and b One didn't do it 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 Not for true")
# Modify variables a Value
a = 0
if ( a and b ):
print ("3 - Variable a and b All for true")
else:
print ("3 - Variable a and b One didn't do it 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 Not for 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")
Running results :
6、 ... and 、Python member operator
Operator
describe
example
in Returns if a value is found in the specified sequence True , Otherwise return to Falsex In the sequence y in , If x stay y Return in sequence Truenot in If no value is found in the specified sequence, return True , Otherwise return to Falsex Not in the sequence y in , If x be not in y Return in sequence True
Here is Python Code :
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
print ("1 - Variable a In the 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 the given list list in ")
# Modify variables a Value
a = 2
if ( a in list ):
print ("3 - Variable a In the given list list in ")
else:
print ("3 - Variable a Not in the given list list in ")
Running results :
7、 ... and 、Python Identity operator
Identity operator Storage unit used to compare two objects
Operator
describe
example
isis Is to determine whether two identifiers are referenced from an object x is y , similar id(x) == id(y) , If you are referring to the same object , Then return to True , Otherwise return to Falseis notis not Is to determine whether two identifiers are referenced from different objects x is not y , similar id(x) != id(y) , If you are not referring to the same object , Then return to True , Otherwise return to False
Here is Python Code :
a = 20
b = 20
if ( a is b ):
print ("1 - a and b Have the same logo ")
else:
print ("1 - a and b There is no identical logo ")
if ( id(a) == id(b) ):
print ("2 - a and b Have the same logo ")
else:
print ("2 - a and b There is no identical logo ")
# Modify variables b Value
b = 30
if ( a is b ):
print ("3 - a and b Have the same logo ")
else:
print ("3 - a and b There is no identical logo ")
if ( a is not b ):
print ("4 - a and b There is no identical logo ")
else:
print ("4 - a and b Have the same logo ")
Running results :
8、 ... and 、Python Operator precedence
Operator
describe
** Index ( Highest priority )~ + - Flip by bit , One yuan plus sign and minus sign ( The last two methods are called [email protected] and [email protected])* / % // ride 、 except 、 Modulo and integers + - Add Subtraction >> << Move right 、 Shift left operator & position ’AND’^ | An operator <= < > >= Comparison operator <> == != Equals operator = %= /= //= -= += *= **= Assignment operator is is not Identity operator in not in member operator not and or Logical operators