python使用手册<3>循环&判断条件&输入输出

 

目录

 

1,for循环

2, while循环

 1,退出循环

1,使用变量作为信号灯

2,break & continue

3温馨提示 

 3,if判定


1,for循环

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

2,创建数值列表

1,range()

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

 range(1, 6)

 range(起始,终止,步长)

2,list()

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

 [1, 2, 3, 4, 5]

2, while循环

while conditional_test:
    do sth

 当condition_test为True时,无限循环。

 1,退出循环

1,使用变量作为信号灯

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

2,break & continue

退出循环&跳过本次循环

#自动骂人机
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温馨提示 

无限循环时用ctrl+c退出

 3,if判定

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

空列表作为判断条件默认为0

猜你喜欢

转载自blog.csdn.net/weixin_60787500/article/details/127740798