This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021.

The Python language supports the following types of operators:

  1. Arithmetic operator
  2. Compare (relational) operators
  3. The assignment operator
  4. Logical operator
  5. An operator
  6. Member operator
  7. Identity operator
  8. Ternary operator

Arithmetic element operator

The arithmetic element operators in Python are basically the same as those in OC or Swift.

  • **: power, returnxtheyPower. Such as:10 * * 20Said,10the20To the power.
  • //: Takes the exact division and returns the integer part of the quotient. For example,9/2The output4.

Note the following when using:

  1.  /The result is a floating point number, even if both numbers are integers:
>>> 9/3     
3.0

>>> 10/3
3.3333333333333335
Copy the code
  1.  //To take a full division, taking only the integral part, and discarding the remainder:
>>> 10//3 3 >>> -10// 4Copy the code
  1.  %Remainder operation
>>> 10%3 1 >>> -10%3 2 # round downCopy the code
  1.  divmod()You get both the quotient and the remainder, and the return value is zerotuple (x//y, x%y).
> > > divmod (10, 3) (3, 1)Copy the code

⚠️ Note: Python also has some minor computational problems due to floating-point accuracy:

0.1 + 0.1 + 0.1 0.3 5.551115123125783 e-17Copy the code

You can use the Decimal module:

From decimal import Decimal Decimal('0.1')+Decimal('0.1')+Decimal('0.1')-Decimal('0.3') Decimal('0.0')Copy the code

Comparison operator

The comparison operators in Python are basically the same as those in OC or Swift.

  • <>: does not equal, compares two objects do not want to wait, similar! =. (python3In the<>Has been deprecated)

Points to note:

  1.  = =Comparison value (intwithstrNot directly comparable) :
>>> a = 1 >>> b = 2 >>> a ! = b True >>> a <> b File "<stdin>", line 1 a <> b ^ SyntaxError: invalid syntaxCopy the code
  1. Strings are compared to stringsASCIIValue:
>>> 'abc' < 'xyz'   # 97 98 99 < 120 121 122
True

>>> (3,2) < ('a','b')	
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
Copy the code
  1. Continuous comparison:
>>> 3 > 2 > 1	# 3 > 2 and 2 > 1
True
>>> 3 > 2 > 2	# 3 > 2 and 2 > 2
False
>>> (3 > 2) > 1	# True and True > 1
False
Copy the code

The assignment operator

The operator instructions example
= The assignment x = y
– = Assignment reduction x -= y, x = x – y
* = Take the assignment x *= y, x = x * y
/ = In addition to the assignment x /= y, x = x / y
% = Take the remainder and assign x %= y, x = x % y
* * = Power assignment x **= y, x = x ** y
/ / = Take an integer and assign x //= y, x = x // y
& = Bitwise and assignment x &= y, x = x & y
= Bitwise or assignment
^ = Xor assignment by bit x ^= y, x = x ^ y
< < = Left the assignment x <<= y, x = x << y
> > = Moves to the right assignment x >>= y, x = x >> y

Points to note:

  1.  pythonDoes not supporta++Grammar:
>>> a++ # python does not support a++ syntax File "<stdin>", line 1 a++ ^ SyntaxError: invalid syntaxCopy the code
  1.  + =You can’t break it down+ =:
>>> A + = 1 # line 1 a + = 1 ^ SyntaxError: invalid syntaxCopy the code

An operator

The arithmetical element operators in Python are basically the same as those in OC or Swift that we normally use.

Logical operator

Python supports logical operators, but does not support && and | |, instead of the English word of more human and or not (all lowercase) :

  • a and bBoth forTrueWhen the result isTrue
  • a or bOne of them is zeroTrueThe result is zeroTrue