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:
- Arithmetic operator
- Compare (relational) operators
- The assignment operator
- Logical operator
- An operator
- Member operator
- Identity operator
- Ternary operator
Arithmetic element operator
The arithmetic element operators in Python are basically the same as those in OC or Swift.
**
: power, returnx
they
Power. Such as:10 * * 20
Said,10
the20
To the power.//
: Takes the exact division and returns the integer part of the quotient. For example,9/2
The output4
.
Note the following when using:
-
/
The result is a floating point number, even if both numbers are integers:
>>> 9/3
3.0
>>> 10/3
3.3333333333333335
Copy the code
-
//
To take a full division, taking only the integral part, and discarding the remainder:
>>> 10//3 3 >>> -10// 4Copy the code
-
%
Remainder operation
>>> 10%3 1 >>> -10%3 2 # round downCopy the code
-
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! =
. (python3
In the<>
Has been deprecated)
Points to note:
-
= =
Comparison value (int
withstr
Not directly comparable) :
>>> a = 1 >>> b = 2 >>> a ! = b True >>> a <> b File "<stdin>", line 1 a <> b ^ SyntaxError: invalid syntaxCopy the code
- Strings are compared to strings
ASCII
Value:
>>> '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
- 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:
-
python
Does not supporta++
Grammar:
>>> a++ # python does not support a++ syntax File "<stdin>", line 1 a++ ^ SyntaxError: invalid syntaxCopy the code
-
+ =
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 b
Both forTrue
When the result isTrue
a or b
One of them is zeroTrue
The result is zeroTrue