# 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

==

be equal to

a==b

a And b Return when equal True, otherwise False

<

Less than

a<b

>=

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

!=

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]>) ==>True
print(['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```