1. for循环和break语句
for i in range(3): # i的取值 [0,1,2,3) print(i)
以下代码输出1 到 101的所有奇数
for i in range(1,101,2): # 2是步长 print("loop:",i)
30 到40之间的值不打印
for i in range(1,101,2): # 2是步长 if i<30 or i>40: print("loop:",i)
简单的登录案例
userName = "Kevin" password = "123456" # 有5次输错密码的机会 for i in range(5): userName1 = input("Please Enter Your UserName:") password1 = input("Please Enter Your Password:") if userName == userName1 and password == password1: print("Congratulations %s ! Login Success" %userName) break; # 跳出,中断 else: print("UserName Or Password Is Error!")
5次登录失败的时候打印提示语句
userName = "Kevin" password = "123456" flag = True # 有5次输错密码的机会 for i in range(5): userName1 = input("Please Enter Your UserName:") password1 = input("Please Enter Your Password:") if userName == userName1 and password == password1: print("Congratulations %s ! Login Success" %userName) flag = False break; # 跳出,中断 else: print("UserName Or Password Is Error!") if flag: print("Are you shameless?")
或者 for循环后面跟着else语句
userName = "Kevin" password = "123456" # 有5次输错密码的机会 for i in range(5): userName1 = input("Please Enter Your UserName:") password1 = input("Please Enter Your Password:") if userName == userName1 and password == password1: print("Congratulations %s ! Login Success" %userName) break; # 跳出,中断 else: print("UserName Or Password Is Error!") else: print("Are you shameless?")
2. continue语句和while语句
while 循环实现5次输入密码和用户名
userName = "Kevin" password = "123456" # 有5次输错密码的机会 count = 0 while count < 5: userName1 = input("Please Enter Your UserName:") password1 = input("Please Enter Your Password:") if userName == userName1 and password == password1: print("Congratulations %s ! Login Success" %userName) break; # 跳出,中断 else: print("UserName Or Password Is Error!") count = count + 1 else: print("Are you shameless?")
当输入错误出现3次,由用户决定是否继续尝试
userName = "Kevin" password = "123456" # 有5次输错密码的机会 count = 0 while count < 5: userName1 = input("Please Enter Your UserName:") password1 = input("Please Enter Your Password:") if userName == userName1 and password == password1: print("Congratulations %s ! Login Success" %userName) break; # 跳出,中断 else: print("UserName Or Password Is Error!") count = count + 1 if count == 3: keepTry = input("Do your want to try more times ?[y/n]") if keepTry == "y": count = 0 else: print("Are you shameless?")
continue结束本次循环
for i in range(10): if i < 5: continue # 结束本次循环,进入下一次循环 print(i) # 打印的值是5到9
3. Python的数据结构
列表数据
a = ["张三","李四","王五","赵信","内瑟斯"] # 列表a,类似java中的数组
列表查询,获取列表的数据
a = ["张三","李四","王五","赵信","内瑟斯"] # 列表a,类似java中的数组 print(a[3]) # 列表下表从0开始,打印出 赵信 print(a[1:3]) # 打印出 ['李四', '王五'],出来的结果仍然是列表 print(a[1:]) # 打印出 ['李四', '王五', '赵信', '内瑟斯'] print(a[1:-1]) # 打印出 ['李四', '王五', '赵信']
print(a[1:-1:2])# 打印出 ['李四', '赵信']
print(a[3::-2]) # 打印出 ['赵信', '李四']
向列表中添加元素,append(self,object),insert(self,index,object)
b = ["张三","李四","王五","赵信","内瑟斯"] b.insert(5,"泰达米尔") print(b[5]) b.append("Boy Next Door") # 默认插入到列表最后一个位置 print(b[0:-1]) # ['张三', '李四', '王五', '赵信', '内瑟斯', '泰达米尔']
列表元素的修改
c = ["张三","李四","王五","赵信","内瑟斯"] c[2] = "亚特兰蒂斯" print(c) c[1:3] = ["纽约","华盛顿","莫斯科"] print(c) # 元素的删除 c.remove("纽约") print(c) # ['张三', '华盛顿', '莫斯科', '赵信', '内瑟斯'] c.pop(1) print(c) del c[0] print(c)
4. 列表的内置方法
a = ["2","2","3","44","33","2","3"].count("2") # 计算列表中元素出现的次数 print(a) a = ["张三","李四","王五","赵6","朱七"] b = ["2","2","4"] a.extend(b) # 在列表a后面添加列表b print(a) print(b) index = a.index("李四") # 获取 李四所在列表的索引值 print(index) b.reverse() # 列表反转 print(b) c = [1,2,3,4,45,8,5,9,100,23] c.sort() # 从小到大排序 print(c)