Python User Manual<3>Loop & Judgment Conditions & Input and Output

 

Table of contents

 

1,for loop

2. while loop

 1. Exit the loop

1. Use variables as semaphores

2,break & continue

3 Warm reminders 

 3. if judgment


1,for loop

list1=[1,2,3,4,5]
for i in list1:
print(list)

2. Create a list of values

1,range()

a=range(1,6)
print(a)

 range(1, 6)

 range (start, end, step)

2,list()

a=range(1,6)
b=list(a)
print(b)

 [1, 2, 3, 4, 5]

2. while loop

while conditional_test:
    do sth

 When condition_test is True, infinite loop.

 1. Exit the loop

1. Use variables as semaphores

#自动骂人机
flag=1
while flag:
    a=input("fxck you\n")
    if a=="i love you":
        flag=0

2,break & continue

Exit loop & skip this loop

#自动骂人机
flag=1
while flag:
    a=input("fxck you\n")
    if a=="i love you":
        break
    elif a=="fxck you"
        continue
    print("you stinky shit")

3 Warm reminders 

Use ctrl+c to exit when looping infinitely

 3. if judgment

if conditional_test:
    do something
elif comditional_test:
    do sth
else:
    do sth

The empty list as a judgment condition defaults to 0

Guess you like

Origin blog.csdn.net/weixin_60787500/article/details/127740798