本文为我学习python的笔记,因本人的编程技术才刚刚入门,所以笔记中可能会有一些错误,希望大家能友好的帮我指正出来
本文文字内容均为原创,请勿私自转载
图片来自互联网,侵删
条件循环
while 条件语句:
执行语句
eg:
a=0
while a<=10000:
print(a)
a=a+1
PS:a=a+1 == a+=1
使用循环遍历列表中的元素
list1=[1,2,3,4,5,6,7,8,9]
i=0
while i<9:
print(list1[i])
i+=1
循环加条件判断
eg:
list1=[1,2,3,4,5,6,7]
i=0
while i<7:
if list1[i]<5:
print('%d<5'%list1[i])
elif list1[i]==5:
print('%d==5'%list1[i])
else:
print('%d>5'%list1[i])
i+=1
使用break退出循环
eg:
n=0
while n<10:
if n==5:
break
n+=1
print(n)
使用continue跳过循环
eg:
n=0
while n<10:
n+=1
if n%2==0:
continue
print(n)
break跳过整个循环,continue跳过本次循环
else否则
while嵌套
eg:
a=1
while a<=9:
b=1
while b<=a:
print(' {}*{}={} '.format(a,b,a*b),end='')
b+=1
print("")
a+=1
for循环-->迭代循环
for i in object:
执行语句
在列表中遍历取值
eg:
list1=[1,2,3,4,5,6]
for i in list1: #i是变量,代表的是列表中的元素
print(i)
在字典中遍历取值
eg:
dict1=dict(a=1,b=2,c=3,d=4)
for i in dict1:
print(i)
range #范围
eg:
for i in range(10):
print(i)
break
退出循环
eg:
for i in range(10):
if i==6:
break
print(i)
continue
跳过当前循环
eg:
for i in range(10):
if i==6:
continue
print(i)
else
否则
eg:
for i in range(10):
if i==6:
continue
print(i)
else:
print("The end")
dir
显示文件列表(迭代器)
for循环嵌套
eg:
print("Start")
for a in range(1,10):
for b in range(1,a+1):
print(' {}*{}={} '.format(b,a,a*b),end='')
b+=1
print("")
a+=1
else:
print("The End")
数据分组
eg:
a=[]
b=[]
for i in range(1,101):
if i%2==1:
a.append(i)
else:
b.append(i)
print(a)
print(b)
登录系统
s=1
import time
for c in range(1,6):
if c==5:
continue
else:
s=10*c
time.sleep(s)
for i in range(1,4):
a=input("请输入用户名")
b=input("请输入密码")
if a=="2233" and b=="000000":
break
else:
print("账号或密码错误,再错误%d次后自动锁定账户"%(3-i))
continue
if i==3:
print("账户已锁定,请%d秒后重试"%s)
a=b=0
continue
else:
break
if c==5 and i==3:
print("你号没了")
else:
print("登录成功")