网络编程
位置:首页>> 网络编程>> Python编程>> 浅谈Python基础—判断和循环

浅谈Python基础—判断和循环

作者:格格_gloria  发布时间:2021-04-21 03:53:46 

标签:Python,判断,循环

判断

缩进代替大括号。

冒号(:)后换号缩进。

if


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

浅谈Python基础—判断和循环

if-elif-else


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

浅谈Python基础—判断和循环

列表


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

浅谈Python基础—判断和循环

字典


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

浅谈Python基础—判断和循环

循环

while

数值


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

浅谈Python基础—判断和循环

集合


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

浅谈Python基础—判断和循环

for

集合


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

浅谈Python基础—判断和循环

列表


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基础—判断和循环

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基础—判断和循环

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基础—判断和循环详解整合网站的支持!

来源:https://www.cnblogs.com/gloria-zhang/p/10572125.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com