06循环

'''
while
'''
'''
while 布尔表达式:冒号不能省略
'''
'''
1+2+3+...+10
'''
i=1
sum1=0
while i<=10:
sum1+=i
i+=1
print(sum1)
'''
python 中的没有 i++ ,如果写了会报语法错误。

但是python 中有 --i,++i,+-i,-+i,他们不是实现-1操作的,仅仅是作为判断运算符号,类似数学中的负负得正

i = 2

print ++i //2

print -+i //-2

print +-i //-2

print --i //2

python 中没有 && ,!, || 这3个运算符,在逻辑表达式中写成这3个会抱逻辑错误的。要实现同样的功能,要写成 and,not,or

返回值 2 and 3 返回3

返回值 2 or 3 返回2

返回值 not 2 and 3 返回 False
'''
#三酷猫钓鱼记录查找,python3.6.3版本下执行
fish_record='鲫鱼5条、鲤鱼8条、鲢鱼7条、草鱼2条、黑鱼6条、乌龟1只'
print(len(fish_record))
record_len=len(fish_record)
i=0
while i<record_len:
if fish_record[i:i+2]=='乌龟':
if int(fish_record[i+2])/2==0:
print("找到乌龟了,是%d只,偶数"%(int(fish_record[i+2])))
else:
print("找到乌龟了,是%d只,奇数"%(int(fish_record[i+2])))
i+=5

'''
for
'''
'''
for v1 in 范围:


'''
s=0
for i in range(10):
print(i)
s+=i
else:
print(str(s)+ ' is a consequence')
'''
range()是python的内建范围函数;range(9)代表0、1、2、3、4、5、6、7、8这九个数字的集合
'''
for i in range(1,5,2):#在0,1,2,3,4中每隔一个数取一个数,2为步长
print(i)
'''
break:跳出所有循环
continue:跳出本次循环
'''

str1='Tom is a student'
for i in range(len(str1)):
print('for循环了%d次'%(i+1))
if str1[i:i+3]=='Tom':
print('OK')
#break
continue
print('ok2')

猜你喜欢

转载自www.cnblogs.com/wsxcode/p/12198674.html
今日推荐