while循环
while true: (死循环)
x = 'youpinketang'
while x:
print (x, end = '')
x=x[1: ]
break
从循环中跳出
continue
跳到循环头部进行下一次循环
while x:
x-=1
if x%2 !=0:
continue
print(x,end='')
while True:
name = imput('请输入您的姓名:')
if name =='stop'
break
age = imput('请输入您的年龄:')
print('您好:{},您的年龄是:{},欢迎学习'。format(name,age))
pass
占位符,对代码的不确定
else
for 循环:
found = False
for x in range (1,5)
if x ==6:
found = True
print('已经找到了',x)
break
if not found:
print ('没找到')
for x in range(1,5):
if x ==6:
print('有这个数字',x)
break
else:
print('没找到')
for x in [1,2,3,4]:
print (x, end =' ')
sum = 0
for x in [1,2,3,4,5]
sum+=x
print(s um)
只遍历字典表的键
for key in emp:
print('{} =>{}'.format (key, emp.get(key,'未找到')))
or
for key in emp.keys():
print(key)
只遍历值
for key in emp.values():
print(key)
values不是列表,是可使用for,while循环的可迭代的试图结构,为了减少对内存的占用
找交集
result =[]
for x in s1:
if x in s2:
result.append(x)
print (result)
l = [x for x in s1 if x in s2]
print(l)
range()
for x in range (1,101,2)
enumerate():可获得现在在循环的是第几项
s='youpinketang'
for idx, item in enumerate(s):
print('{}){}'.format(idx,item))