# 1, Basic operator

## 1. Arithmetic operator

```print(10+3) # 13
print(10-3) # 7
print(10*3) # 30
print(10/3) # 3.3333333333333335
print(10//3) \3 remove the floor
print(10%3)  # 1. Remainder
```

Understand that the (* *) + sign can not only add numbers and numbers, but also string and string list and list addition

```x = 'aaa'
y = 'bbb'
print(x + y) # aaabbb
print([1,2,3] + [4,5]) # [1, 2, 3, 4, 5]

x = 'aaa'*3
print(x) # aaaaaaaaa
```

## 2. Comparison operator

```print(10 == 10)
print(10 != 3)
print(10 > 3)
print(10 < 3)
print(10 >= 3)
print(10 <= 3)
```

Understand

```s1 = 'abced'
s2 = 'az'
print(s2>s1) # True
# The size of strings can be compared one by one with ASCII codes until the results are compared. For example, a and a cannot compare the next b<z

s1 = [123,123]
s2 = [111]
print(s1 <s2) False

l1 = [1, 'abc', 3.1]
# l2 = [1, 2222, 3.1]
l2 = [1, "abb", 3.1]
print(l1 > l2) True
```

## 3. Assignment operator

1 augmented assignment

```age = 18
age += 1  # age = age + 1
print(age)
age -= 2  # Subtraction assignment operator c-=a is equivalent to c=c-a
print(age)
age *= 2  # Multiplication assignment operator c*=a is equivalent to c=c*a
age /= 2  # The division assignment operator c/=a is equivalent to c=c/a
age %= 2  # Modulo assignment operator c%=a is equivalent to c=c% a
age **= 2  # Power assignment operator c**=a is equivalent to c=ca
age //=2 \c \a the integer division assignment operator c//=a is equivalent to c=c//a
```

2 Cross assignment

```x = 111
y = 222
x, y = y, x
print(x, y)
```

3 Chained assignment

```x = y = z = 10
print(id(x),id(y),id(z))
```

4 Decompression assignment

```salaries = [111,222,333,444,555]

m0,m1,m2,m3,m4=salaries
print(m0)  # 111
print(m1)  # 222
print(m2)  # 333
print(m3)  # 444
print(m4)  # 555

x, y, z = {'k1': 111, 'k2': 222, 'k3': 333}
dic = {'k1': 111, 'k2': 222, 'k3': 333}
x, y, z = dic
print(x, y, z)  # k1 k2 k3
print(dic[x], dic[y], dic[z])  # 111 222 333

x, y, z,a,b = "hello"
print(x)  # h
x,*_,a,b = "hello"
print(x,a,b)  # h l o
```

Note that the variable name and the number of values must correspond to each other one by one

```# m0,m1,m2,m3,m4,m5=salaries # An error is reported. One more variable name is not allowed
# m0,m1,m2,m3=salaries # An error is reported. You can't do without one variable name
```

The introduction of '' '': '' can help us take the values at both ends, but cannot take the middle value

```salaries = [111, 222, 333, 444, 555]
m0, m1, *_ = salaries
print(m0)  # 111
print(m1)  # 222
print(*_)  # 333

*_,x,y=salaries
print(x,y)  # 444 555
x,*_,y,z=salaries
print(x,y,z)  # 111 444 555
_,*middle,_=salaries
print(middle) # [222,333,444]
```

The values without corresponding relationship will be saved as a list and assigned to the following variable name, which is _, Other symbols x, *xxx are available
Generally use'\u' It means that we don't intend to use this thing

# 2, Logical operator

## condition

Anything that can get both True and False values can be used as a condition
1. Explicit Boolean value: on the surface, you can see true or false

```#(1) Result of comparison operation
print(10 > 3)
print(10 == 3)
#(2) The variable value is directly True or False
tag = True
```

2. Implicit Boolean value: it looks like a value on the surface and will be converted to True or False by the interpreter at the bottom
The Boolean values corresponding to 0, None and null (including empty list and empty dictionary) are False, and the Boolean values corresponding to other values are True

```# 10
# 3.1
# ""
# []
# {"k1":222}
# None
```

## Logical operator

Used to connect multiple conditions
(1)not condition: negates the result of the condition
(2) condition 1 and condition 2: connect the left and right conditions. Both conditions must be True before the final result is True
(3) condition 1 or condition 2: connect the left and right conditions. If one of the two conditions is True, the final result will be True
(4) priority: not > and > or, it is recommended to use brackets to identify priority

```#        False    or          True
res=(3 > 4 and 4 > 3) or (not (1 == 3 and 'x' == 'x')) or 3 > 3
print(res) # True

print(10 and 0)  # 0
print(10 and False)  # False
print(10 or False)  # 10
```

Tags: Python

Posted by danmon on Tue, 31 May 2022 19:07:39 +0530