Python基础——判断和循环

该博客主要介绍Python中的判断和循环相关知识。判断部分涉及if、if - elif - else语句,用缩进代替大括号,冒号后换行缩进;循环部分包含while和for循环,还提及了列表、字典、集合等数据结构,以及continue和break语句。

判断

缩进代替大括号。

冒号(:)后换号缩进。

if

 

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

 

if-elif-else

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

列表

 

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

 

字典

 

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

 

循环

while

数值

 

 

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

 

集合

 

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

 

 

for

集合

 

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

 

列表

 

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])

 

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)

 

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)

 

转载于:https://www.cnblogs.com/gloria-zhang/p/10572125.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值