Python中的判断、循环 if...else,while

本文介绍了Python中的if...else语句及不同条件组合的使用方法,并通过具体实例展示了如何利用for循环和while循环遍历数组元素。此外,还介绍了循环中continue和break命令的作用。

if...else语句:

a=3; b=3; 
if a == b :
print(a,b)
elif a <= b :
print(str(a) + " is less than " + str(b))
else :
print(str(a) + " is greater than " + str(b))

###################################

n = 3
if (n >= 0 and n <= 8) or (n >= 12 and n <= 25): 
print("hhh")

##或者 

n = 3
if (n >= 0 and n <= 8) or (n >= 12 and n <= 25): print("hhh")

 

一个字符串也相当于是一个数组,通过for循环可以将每个字符输出

for循环,相当于foreach
c="cc";d="dd";e="ee";f="ff"
pa = [c,d,e,f]
for p in pa:
print("current p is:",p)

使用下标索引:
c="cc";d="dd";e="ee";f="ff"
pa = [c,d,e,f]
for index in range(len(pa)):
print("current p is:",pa[index])

或者:
c="cc";d="dd";e="ee";f="ff"
pa = [c,d,e,f]
for index in range(2,4):
print("current p is:",pa[index])


使用while循环:
c="cc";d="dd";e="ee";f="ff"
pa = [c,d,e,f]
i=0
while(i<len(pa)):
print("current p is:",pa[i])
i=i+1


while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环

转载于:https://www.cnblogs.com/dreamer-fish/p/3818408.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值