day 07 Process Control

Soul three questions:

what is? Why should there be? how to use?

A branch structure

1.1 What is the branch structure

That is, to perform different sub-code based on true and false conditions

1.2 Why have a branch structure

When we need to do different things depending on the conditions, such as: rain today, I take an umbrella, it does not rain today, I do not take an umbrella

1.3 how to use the branch structure

1.3.1 if grammar

if 条件1:   # 如果条件1的结果为True,就依次执行:代码1、代码2,......
   代码1
    代码2
    ......
elif 条件2: # 如果条件2的结果为True,就依次执行:代码3、代码4,......
   代码3
    代码4
    ......
else:     # 其它情况,就依次执行:代码7、代码8,......
    代码7
    代码8
    ......
# 注意:
# 1、python用相同缩进(4个空格表示一个缩进)来标识一组代码块,同一组代码会自上而下依次运行
# 2、条件可以是任意表达式,但执行结果必须为布尔类型
# 2.1在if判断中所有的数据类型也都会自动转换成布尔类型
# 2.2、None,0,空(空字符串,空列表,空字典等)三种情况下转换成的布尔值为False
# 2.3、其余均为True 

Next, use a nested if to demonstrate advanced usage, when we need a condition as a prerequisite, we need more if scenarios to represent different nesting

#我们需要对90分以上的男生和女生做不同奖励,对80分以下的男生女生做不同惩罚
result = int(input("请输入你的成绩"))
sex = input("请输入你的性别")
if result>90:
    if sex == "boy":
        print("棒棒糖")
    else:
        print("冰激凌")
else:
    if sex == "boy":
        print("eat shit")
    else:
        print("get scolding")

Extended Content: copy depth

When we need two of the same list of content to be modified without affecting the operation of another list

Shallow copy: the original list is the memory address of the first layer without distinction a copy directly to the new list.

In this case, we modify immutable type in the original list, a new list will not change

Modify the original list of variable types, a new list will change accordingly

a = [
    'aa','bb',[1111]
]
b=a.copy()
print(id(a))
print(id(b))
>>>1729418126024
>>>1729419377288
a[2][0]=123
print(a)
print(b)
>>>['aa', 'bb', [123]]
>>>['aa', 'bb', [123]]

Deep copy: immutable type shallow copy of copy mode and as

Variable type, will open up a new memory space in memory

a = [
    'aa','bb',[111]
]
import copy
b = copy.deepcopy(a)
print(id(a))
print(id(b))
>>>2172376474312
>>>2172379870920
a[2][0]=222
print(a)
print(b)
>>>['aa', 'bb', [222]]
>>>['aa', 'bb', [111]]

Conclusion, if only read-only operations, use shallow copy, if you want to write, with a deep copy

The two-cycle structure

2.1while cycle

Basic operation: Output 1-4

i= 1
while i<5:#i<5是while执行子代码的条件,在执行完一次循环后回到开头重新进行判断
    print(i)
    i+=1

Advanced Operation: determine the user login operation

username = "hz"
password = "123"
tag = True
while tag:
    name = input("请输入你的用户名:")
    word = input("请输入你的密码:")
    if name==username and word == password:
        print("登录成功")
        tag = False
    else:
        print("账号或密码错误")
    print("————end————")

Two ways to terminate the cycle

#第一种,直接把循环条件改为假,在执行完本层循环后结束循环
while tag=True
    tag=False
    print("1")
#第二种,break,在break后面的代码不执行,直接跳出循环
while True
    break
    print("1")

Skip present cycle

i = 0
while i<5:
    i += 1
    if i == 2:
        continue #跳出本次循环,执行下一次循环
    print(i)

while used in conjunction with else

i = 0
while i<5:
    i += 1
    if i == 2:
        continue
    print(i)
else:
    print("hz is dsg")
#执行else代码的条件:while循环结束后且没有被break打断的情况

Guess you like

Origin www.cnblogs.com/hz2lxt/p/12449529.html