Introduction to operators in python (Part 1)

Xiaobo test growth path 2021-10-29 04:10:09
introduction operators python

Let's introduce today Python Operator in , Operators mainly include the following categories :

  • Arithmetic operator
  • Compare ( Relationship ) Operator
  • Logical operators
  • Assignment operator
  • Ternary operator
  • An operator
  • member operator
  • Identity operator

Next , Introduce the specific operation of the operator in detail :

Arithmetic operator

Arithmetic operator an operator used to perform related operations on data of integer type and floating point type .

Common arithmetic operators and corresponding operations are described in the figure below :

If you will True/False When used for numerical operations , Will automatically switch to 1 and 0 To calculate .

Comparison operator

The comparison operator is mainly used to compare the size of two expressions , The return result of the comparison is True perhaps False.

Operator

name

Example

explain

>

Greater than

a>b

a Greater than b When to return to True, otherwise False

==

be equal to

a==b

a And b Return when equal True, otherwise False

<

Less than

a<b

a Less than b When to return to True, otherwise False

>=

Greater than or equal to

a>=b

a Greater than or equal to b When to return to True, otherwise False

<=

Less than or equal to

a<=b

a Less than b When to return to True, otherwise False

!=

It's not equal to

a!=b

a And b Return... If not equal True, otherwise False

Examples demonstrate :

Let's test you

 stay python in , What are the execution results of the following statements ?
print(1.0 == 1) ==>True
print(1 == True) ==>True
print([2,1]>[1]) ==>True
print(['1']>[1]]) ==> Operation error reporting 

Logical operators

Logical operators are used to operate on Boolean variables , The result is also Boolean .

  • and And (and The values on both sides are True when , return True)
  • or or (or As long as one of the values on both sides is True when , return True)
  • not Not ,( take true Turn into false,false Turn into true)

Please look at specific cases :

print(True and True) #True
print(True and False) #False
print(False and False) #False

about a or b Come on , If a It's true , Then the value is a, Otherwise b; about a and b Come on , If a It's true , Then the value is b, Otherwise a.

print(True or False) #True
print(False or True) #True
print(False or False) #False
print(not True) #False
print(not False) #True

The specific running effect is shown in the above script , It should be noted that , Logical operators like this , At run time , A similar “ A short circuit ” The design of the ,and and or The operation is in operation , If the result has been determined , You won't evaluate the following expression .

such as :True or False , Ahead True It has been determined that the final returned result will be True, You won't care about or Go back True still False. All in all :and All data should be True To return to True,or There is only one for True Then return to True.

Assignment operator

= Is the most common assignment operator , such as a=3, It means that 3 Is assigned to a variable a. Other common assignment operators are :+=、-+、*=、/=、%=、//=、**= etc. .

a+=b ==> a=a+b
a-=b ==> a=a-b
a*=b ==> a=a*b

Ternary operator

This is generally used in if Judgment conditions are common , such as , seek 2 Maximum number , The common expression is as follows :

if a>b:
max = a
else:
max = b

python Provides a simple way to write , The code looks much simpler :

max = a if a>b else b

python Pass through if else Condition judgment of , It can achieve something similar java Inside ?: Ternary operator .

java The usage inside is as follows :z = x>y ? x-y : x+y;

python Inside usage :value1 if Judge the condition else value2

A little more complicated :

a if a>b else c if c>d else d
Equivalent to :a if a>b else ( c if c>d else d )

member operator

  • in : Returns if a value is found in the specified sequence True, Otherwise return to False
  • not in : If the corresponding value is not found in the specified sequence number, return True, Return if found False

Member operators are still used more , For example, to view a certain key Is it in a dictionary , It can be used key in dict.keys() To judge

a = {"A": "1", "B": "2"}
print('A' in a.keys())
print('a' in 'abcdefg')

Identity operator

is、is not, Used to determine whether two identifiers refer to the same object

a1 = 10
b1 = 2
b1 += 8
print(a1 is b1) #True
s1 = 'abc'
s2 = '123abc'[3:]
print(s1 is s2) #False
a1 = 1
print(a1 is True) #False

This article is from WeChat official account. - Xiaobo's road to growth (libotest)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the [email protected] Delete .

Original publication time : 2021-10-05

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[Xiaobo test growth path]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/10/20211013160901066E.html

  1. OpenCV - Python Real play (14) - face detection details (six lignes de code seulement pour apprendre 4 méthodes de détection de visage)
  2. 你好,python开发mes系统,能分享下吗,我最近也想搞这方面的
  3. 你好,python開發mes系統,能分享下嗎,我最近也想搞這方面的
  4. Bonjour, Python a développé mon système, pouvez - vous le partager?
  5. Introduction to tuples in Python
  6. Introduction to strings in python (Part 2)
  7. Introduction to strings in python (Part 1)
  8. python关于 if 的简单操作时,输出结果不是预期所要的结果 的问题
  9. python關於 if 的簡單操作時,輸出結果不是預期所要的結果 的問題
  10. Lorsque Python fonctionne simplement sur if, la sortie n'est pas le résultat attendu
  11. Python中字典问题请求解惑
  12. Python中字典問題請求解惑
  13. Demande de résolution de problèmes de dictionnaire en python
  14. Python中字典问题请求解惑
  15. Python technique 2: advanced usage of function parameters
  16. Demande de résolution de problèmes de dictionnaire en python
  17. Preliminary Knowledge - Python Core use Common Data Analysis Library (ⅱ)
  18. 关于python的代码问题,终端打印为什么会起飞
  19. En ce qui concerne les problèmes de code Python, pourquoi l'impression du terminal décolle - t - elle?
  20. Python中种子seed的运用问题
  21. L'application de Seed en python
  22. Python functional programming series 008: Testability
  23. [must see for getting started with Python] the difference and connection between cookie and session in Python!
  24. Python Xiaobai from scratch pyqt5 project actual combat (4) basic controls
  25. Python Xiaobai starts the pyqt5 project from scratch (3) connection between signal and slot
  26. Echarts ne peut pas afficher le HTML en PDF en utilisant le pdfkit de Python
  27. 一只Python 小white 的日常提问(づ ●─● )づ
  28. 2021 tutoriel complet d'automatisation des tests d'interface python [matériel d'apprentissage joint]
  29. 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.
  30. Une question quotidienne d'un petit morceau de Python (づ● - ●)
  31. Python中字典问题请求解惑
  32. 一只Python 小white 的日常提問(づ ●─● )づ
  33. 在python中的问题,请问如何解决
  34. Only 10 questions are needed to easily master Matplotlib graphics processing | Python skill tree
  35. 在python中的問題,請問如何解决
  36. Comment résoudre le problème en python
  37. Demande de résolution de problèmes de dictionnaire en python
  38. 使用python,在一个命名为.txt文本文档写入n m乘法表。
  39. En utilisant Python, écrivez une table de multiplication n m dans un document texte nommé.txt.
  40. 使用python,在一個命名為.txt文本文檔寫入n m乘法錶。
  41. Python,前缀后缀相同时合并
  42. 关于#python#的问题:python
  43. 關於#python#的問題:python
  44. Python,前綴後綴相同時合並
  45. Questions sur # # Python #: Python
  46. Python, préfixe et suffixe combinés en même temps
  47. python manage.py shell无法运行,
  48. python manage.py shell無法運行,
  49. Le shell Python manage.py ne fonctionne pas,
  50. python中使用vscode Import 'matplotlib.pyplot' could not be resolved from source 问题
  51. [Chapter 11 of the full version] Python advanced crawler practice - system master Po anti climbing skills challenge high salary
  52. L'utilisation de vscode Import 'matplotlib.pyplot' en python ne peut pas être résolue à partir du problème source
  53. Python fusionne les deux listes et supprime les éléments dupliqués lors de la fusion
  54. [JS Reverse AES Reverse Encryption] python crawler combat, les jours sont de plus en plus décisifs
  55. 30 jeux Python. Je peux jouer à la pêche au travail pendant une journée.
  56. 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!
  57. J'a i utilisé Python pour ramper à travers 5000 belles photos de papier peint, un jour oublié Premier amour!
  58. [Python planting system] the best green plant for your girlfriend. Girls love it when they see it! Attachment: should be able to feed - right??!
  59. [Python love guide] two small programs for sweetness burst table are released! Afraid you can't find someone?
  60. 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]