# I. conditions

## What are the conditions? What can be a condition? Why conditions?

### 2. Type of condition

#The first category: explicit Boolean

The condition can be: the comparison operator ා age = 18 ා print (age > 16) ා a Boolean value will be obtained after the condition judgment

The conditions can be: True, False ා is ﹐ beautiful = True ා print (is ﹐ beautiful)

#The second category: implicit Boolean value

All values can be used as conditions, including 0, None, and empty (empty string, empty list, and empty dictionary). The Boolean value represented is False,

The rest are True

### 3. Why to use conditions

Conditions help the program to execute instructions according to people's ideas and get the desired results. It can also improve the efficiency and value of code.

# 2, Logical operators: not, and, or

Note: (not at all)

### 1. Basic use of not, and, or

#not: it is to reverse the result of the condition immediately following
#ps:not and the condition immediately following is an indivisible whole

```>>> print(not 16 > 13)
False
>>> print(not True)
False
>>> print(not False)
True
>>> print(not 10)
False
>>> print(not 0)
True
>>> print(not None)
True
>>> print(not '')#'' Null value
True
>>> ```

#And: logical and, and are used to link left and right conditions. If both conditions are True at the same time, the final result will be True

```>>> print(True and 10 > 3)
True
>>> print(True and 10 > 3 and 10 and 0) # If the conditions are all true, the final result will be True
0
>>> print( 10 > 3 and 10 and 0 and 1 > 3 and 4 == 4 and 3 != 3)  # Lazy principle
0
>>> ```

#Or: logical or, or is used to link left and right conditions. If one of the two conditions is True, the final result will be True,
#If both conditions are False, the final result is False

```>>> print(3 > 2 or 0)
True
>>> print(3 > 4 or False or 3 != 2 or 3 > 2 or True) # Lazy principle
True
>>> ```

Three sentence summary:

not is: true becomes false, false becomes true

and is: all true is true, one false is false

or is: one true is true, all false is false

### 2. Priority not > and > or

# ps:
#If a single connection is just a string of and connections, or a single connection is just a string of or connections, it can be calculated according to the sequence from left to right (lazy principle)
#If it's mixed use, priority needs to be considered

```>>> res=3>4 and not 4>3 or 1==3 and 'x' == 'x' or 3 >3
>>> print(res)
False
>>> #       False                 False              False
>>> res=(3>4 and (not 4>3)) or (1==3 and 'x' == 'x') or 3 >3
>>> print(res)
False>>> #Standard expression, and connection as far as possible>>> res=3>4 and ((not 4>3) or 1==3) and ('x' == 'x' or 3 >3)>>> print(res)False>>> ```

# 3, Member operator

### 1. Member operator in

```>>> print("egon" in "hello egon") # Determine whether a string exists in a large string
True
>>> print("e" in "hello egon") # Determine whether a string exists in a large string
True
>>> print(111 in [111,222,33]) # Determine whether the element exists in the list
True
>>> # judge key Exists in dictionary or not
>>> print(111 in {"k1":111,'k2':222})
False
>>> print("k1" in {"k1":111,'k2':222})
True
>>> # not in
>>> print("egon" not in "hello egon") # Recommended use
False
>>> print(not "egon" in "hello egon") # The logic is the same as above, but the semantics is not clear, so it is not recommended
False
>>> ```

### 2. Identity operator is

```>>>
>>> #is # It's about whether the IDs are equal
>>> x=10
>>> y=23
>>> x is y
False
>>> ```

# 4, Process control

## 1. if condition judgment

Syntax 1: if

if conditions:

(four spaces) code 1

(four spaces) code 2

(four spaces) code 3

(the space before the code is the distinguishing mark of code level)

```# print(1)
# print(2)
# print(3)
# if condition:
#     Code 1
#     Code 2
#     Code 3
# print(4)
# print(5)```
```>>>
>>> age = 60
>>> is_beautiful = True
>>> if (age > 16 and age < 20 and is_beautiful):
print('I like it. Let's be together')
print('Other code.............')
>>> ```

Syntax 2: if else

if (condition):

(four spaces) code 1

else:

(four spaces) code 2

```if condition:    Code 1    Code 2    Code 3else:    Code 1    Code 2    Code 3# age = 60
# is_beautiful = True
# star = 'Horizontal seat'
#
# if age > 16 and age < 20 and is_beautiful and star == 'Horizontal seat':
#     print('I like it. Let's get together...')
# else:
#     print('How are you, Auntie? I'll play with you. I'll hide my kung fu and fame')
#
# print('Other code.............')```

Syntax 3: if---elif

if (condition):

(four spaces) code 1

elif (condition):

(four spaces) code 2

elif (condition):

(four spaces) code 2

-- elif (condition) - if the previous condition is not met, the command of elif (condition) and else will continue to be executed

```'''
Syntax 3:
if condition 1:
Code 1
Code 2
Code 3
elif condition 2:
Code 1
Code 2
Code 3
elif condition 3:
Code 1
Code 2
Code 3
'''
# score=73
# if score >= 90:
#print('excellent ')
# elif score >= 80 and score < 90:
#print('good ')
# elif score >= 70 and score < 80:
#print('normal ')

Improvement of Chinese Medicine
# score=int(score)
#
# if score >= 90:
#print('excellent ')
# elif score >= 80:
#print('good ')
# elif score >= 70:
#print('normal ')```
```'''
Syntax 3:
if condition 1:
Code 1
Code 2
Code 3
elif condition 2:
Code 1
Code 2
Code 3
elif condition 3:
Code 1
Code 2
Code 3
...
else:
Code 1
Code 2
Code 3
'''
# score=int(score)
#
# if score >= 90:
#print('excellent ')
# elif score >= 80:
#print('good ')
# elif score >= 70:
#print('normal ')
# else:
#
# print('=====>')```

Syntax 4: if nested if

```age = 17
is_beautiful = True
star = 'Horizontal seat'

if 16 < age < 20 and is_beautiful and star == 'Horizontal seat':
print('Start to say.....')
is_successful = True
if is_successful:
print('Two will live happily ever after...')
else:
print('Good aunt')

print('Other code.............')```

## 3,while

Tags: Python REST

Posted on Fri, 06 Mar 2020 02:29:49 -0800 by MFKR