判断

缩进代替大括号。

冒号(:)后换号缩进。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

if

 

test=100
if test>50:
    print('OK')
print('test')

Python基础——判断和循环 Python 第1张

 

if-elif-else

test=50
if test>200:
    print('200')
elif test<100:
    print('100')
else:
    print('100-200')

Python基础——判断和循环 Python 第2张

列表

 

test1=[123,456,789]
if 123 in test1:
    print('OK')

Python基础——判断和循环 Python 第3张

 

字典

 

test2={'hello':123,'world':456}
if 'hello' in test2:
    print('OK')

Python基础——判断和循环 Python 第4张

 

循环

while

数值

 

 

test=0
while test<10:
    print(test)
    test+=1

 

Python基础——判断和循环 Python 第5张

集合

 

test1=set(['hello','world'])
while test1:
    test2=test1.pop()
    print(test2)

 

Python基础——判断和循环 Python 第6张

 

for

集合

 

test3=set(['hello','world'])
for t in test3:#相当于foreach
    print(t)

 

Python基础——判断和循环 Python 第7张

列表

 

test4=[123,456,789]
for i in range(3):
    print(test4[i])

 

test5=[123,456,789,34,5435,26,2362,262,26,5]
for i in range(len(test5)):
    print(test5[i])

 

Python基础——判断和循环 Python 第8张

continue

 

test6=[11,12,13,14,15,16,17,18,19,20]
for i in test6:
    if i%2==0:
        print(i) 
    else:
        continue
    print(i)

 

Python基础——判断和循环 Python 第9张

break

 

test7=[12,13,14,15,16,17,18,19,20]
for i in test7:
    if i%2==0:
        print(i) 
    else:
        break
    print(i)

 

Python基础——判断和循环 Python 第10张

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄