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

版权声明
本文为[MG_ Lee]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/10/20211013171735419B.html

  1. L'application de Seed en python
  2. Python functional programming series 008: Testability
  3. [must see for getting started with Python] the difference and connection between cookie and session in Python!
  4. Python Xiaobai from scratch pyqt5 project actual combat (4) basic controls
  5. Python Xiaobai starts the pyqt5 project from scratch (3) connection between signal and slot
  6. Echarts ne peut pas afficher le HTML en PDF en utilisant le pdfkit de Python
  7. 一只Python 小white 的日常提问(づ ●─● )づ
  8. 2021 tutoriel complet d'automatisation des tests d'interface python [matériel d'apprentissage joint]
  9. Décrivez ce que les connaissances pertinentes jouent dans votre travail en utilisant arduino ou Python, y compris les bibliothèques pertinentes, en conjonction avec votre travail quotidien.
  10. Une question quotidienne d'un petit morceau de Python (づ● - ●)
  11. Python中字典问题请求解惑
  12. 一只Python 小white 的日常提問(づ ●─● )づ
  13. 在python中的问题,请问如何解决
  14. Only 10 questions are needed to easily master Matplotlib graphics processing | Python skill tree
  15. 在python中的問題,請問如何解决
  16. Comment résoudre le problème en python
  17. Demande de résolution de problèmes de dictionnaire en python
  18. 使用python,在一个命名为.txt文本文档写入n m乘法表。
  19. En utilisant Python, écrivez une table de multiplication n m dans un document texte nommé.txt.
  20. 使用python,在一個命名為.txt文本文檔寫入n m乘法錶。
  21. Python,前缀后缀相同时合并
  22. 关于#python#的问题:python
  23. 關於#python#的問題:python
  24. Python,前綴後綴相同時合並
  25. Questions sur # # Python #: Python
  26. Python, préfixe et suffixe combinés en même temps
  27. python manage.py shell无法运行,
  28. python manage.py shell無法運行,
  29. Le shell Python manage.py ne fonctionne pas,
  30. python中使用vscode Import 'matplotlib.pyplot' could not be resolved from source 问题
  31. [Chapter 11 of the full version] Python advanced crawler practice - system master Po anti climbing skills challenge high salary
  32. L'utilisation de vscode Import 'matplotlib.pyplot' en python ne peut pas être résolue à partir du problème source
  33. Python fusionne les deux listes et supprime les éléments dupliqués lors de la fusion
  34. [JS Reverse AES Reverse Encryption] python crawler combat, les jours sont de plus en plus décisifs
  35. 30 jeux Python. Je peux jouer à la pêche au travail pendant une journée.
  36. J'a i collecté un nouveau hit de liste en python, donc c'est un secret que quelqu'un d'autre peut devenir un magnat des médias!
  37. J'a i utilisé Python pour ramper à travers 5000 belles photos de papier peint, un jour oublié Premier amour!
  38. [Python planting system] the best green plant for your girlfriend. Girls love it when they see it! Attachment: should be able to feed - right??!
  39. [Python love guide] two small programs for sweetness burst table are released! Afraid you can't find someone?
  40. J'ai utilisé Python pour ramper 1000 lettres d'amour pour aider mon colocataire à exprimer les fleurs de classe, mais les inverser et les inverser... C'est le secret ultime des fleurs de classe!
  41. J'ai utilisé Python pour me connecter à la plus grande plate - forme de jeu au monde, et à quel point le cryptage steam est intelligent [code source inclus]
  42. python中列表转为矩阵后无法进行矩阵的乘法运算
  43. Python crawler Development and Learning full tutoriel 2nd Edition, banggan 100000 words [recommended Collection]
  44. Python crawler haut de gamme: microstore confus anti - décryptage
  45. La multiplication de la matrice ne peut pas être effectuée lorsque la liste est convertie en matrice en python
  46. Introduction to operators in python (Part 1)
  47. Are the dictionaries in Python ordered
  48. Introduction to dictionaries in Python
  49. List introduction in Python
  50. pandas比较两个dataframe特定数据列的数值是否相同并给出差值:使用np.where函数
  51. Python使用matplotlib绘制透明背景的可视化图像并保存透明背景的可视化结果(transparent background)
  52. Python self study notes -- basic grammar
  53. Python utilise matplotlib pour dessiner une image visuelle de l'arrière - plan transparent et enregistrer les résultats visuels de l'arrière - plan transparent
  54. Pandas compare les valeurs de deux colonnes de données spécifiques à dataframe et donne des valeurs de voyage: en utilisant la fonction np.where
  55. Comment configurer une application ASGI Django avec Postgres, nginx et uvicorn sur Ubuntu 20.04
  56. What are the advantages of Python and how to get started quickly
  57. Python self study notes -- basic data types
  58. Python code reading (Chapter 14): List Union
  59. Analyse statistique de la fonction de données des essais aléatoires Python
  60. Alien invasion project in Python application -- Aliens (Part 2)