# Python self study notes -- operators

MG_ Lee 2021-10-29 05:26:15
python self study notes operators

#### Python Logical operators

Python Language supports logical operators , The following hypothetical variables a by 10, b by 20:

Operator Logical expression describe example
and x and y Boolean " And " - If x by False,x and y return x Value , Otherwise return to y Calculated value . (a and b) return 20.
or x or y Boolean " or " - If x yes True, It returns x Value , Otherwise it returns y Calculated value . (a or b) return 10.
not not x Boolean " Not " - If x by True, return False . If x by False, It returns True. not(a and b) return 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 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")```
1 - Variable a and b All for true
2 - Variable a and b All for true, Or one of the variables is true
3 - Variable a and b One didn't do it true
4 - Variable a and b All for true, Or one of the variables is true
5 - Variable a and b All for false, Or one of the variables is false

#### Python member operator

In addition to some of the above operators ,Python Member operators are also supported , The test case contains a series of members , Including strings , List or tuple .

Operator describe example
in Returns if a value is found in the specified sequence True, Otherwise return to False. x stay y In sequence , If x stay y Return in sequence True.
not in If no value is found in the specified sequence, return True, Otherwise return to False. x be not in y In sequence , If x be not in y Return in sequence True.
```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 ")```
1 - Variable a Not in the given list list in
2 - Variable b Not in the given list list in
3 - Variable a In the given list list in

#### Python Identity operator

Identity operators are used to compare the storage units of two objects

Operator describe example
is is Is to determine whether two identifiers are referenced from an object x is y, similar id(x) == id(y) , If it refers to the same object, it returns True, Otherwise return to False
is not is not Is to determine whether two identifiers are referenced from different objects x is not y , similar id(a) != id(b). If the reference is not the same object, return the result True, Otherwise return to False.

▲ notes ： id() Function to get the memory address of an object .

```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 ")```
1 - a and b Have the same logo
2 - a and b Have the same logo
3 - a and b There is no identical logo
4 - a and b There is no identical logo

▲is And == difference ：
is Used to determine whether two variable reference objects are the same , == Used to determine whether the values of reference variables are equal .

Reference resources ：Python3 Operator | Novice tutorial

https://pythonmana.com/2021/10/20211013171735419B.html