Table of contents
1. Use variables as semaphores
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