Let's introduce today Python Operator in , Operators mainly include the following categories :
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 .
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
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 .