Python中的判断、循环 if...else,while
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 则是用于退出循环