1 # 1、循环的语法与基本使用 2 """ 3 print(1) 4 while 条件: 5 代码1 6 代码2 7 代码3 8 print(3) 9 """ 10 # 11 # count = 0 12 # while count < 5: # 5 < 5 13 # print(count) # 0,1,2,3,4 14 # 15 # print('*代码----->') 16 17 # 2、死循环与效率问题 18 # count = 0 19 # while count < 5: # 5 < 5 20 # print(count) # 0,1,2,3,4 21 # 22 # while True: 23 # name = input('your name >>>> ') 24 # print(name) 25 26 # 纯计算无io的死讯会导致致命的效率问题 27 # while True: 28 # 1+1 29 30 # while 1: 31 # print('xxxx') 32 33 # 3、循环的应用 34 username = 'egon' 35 password = '123' 36 37 # 两个问题: 38 # 1、重复代码 39 # 2、输对了应该不用再重复 40 # while True: 41 # inp_name=input('请输入您的账号:') 42 # inp_pwd=input('请输入您的密码:') 43 # 44 # if inp_name == username and inp_pwd == password: 45 # print('登录成功') 46 # else: 47 # print('账号名或密码错误') 48 49 50 # 4、退出循环的两种方式 51 # 方式一:将条件改为False,等到下次循环判断条件时才会生效 52 tag = True 53 while tag: 54 inp_name = input('请输入您的账号:') 55 inp_pwd = input('请输入您的密码:') 56 57 if inp_name == username and inp_pwd == password: 58 print('登录成功') 59 tag = False # 之后的代码还会运行,下次循环判断条件时才生效 60 else: 61 print('账号名或密码错误') 62 63 print('====end====') 64 65 # 方式二:break,只要运行到break就会立刻终止本层循环 66 while True: 67 inp_name = input('请输入您的账号:') 68 inp_pwd = input('请输入您的密码:') 69 70 if inp_name == username and inp_pwd == password: 71 print('登录成功') 72 break # 立刻终止本层循环 73 else: 74 print('账号名或密码错误') 75 76 # print('====end====') 77 78 # 7、while循环嵌套与结束 79 ''' 80 tag=True 81 while tag: 82 while tag: 83 while tag: 84 tag=False 85 86 87 # 每一层都必须配一个break 88 while True: 89 while True: 90 while True: 91 break 92 break 93 break 94 ''' 95 ## break的方式 96 # while True: 97 # inp_name=input('请输入您的账号:') 98 # inp_pwd=input('请输入您的密码:') 99 # 100 # if inp_name == username and inp_pwd == password: 101 # print('登录成功') 102 # while True: 103 # cmd=input("输入命令>: ") 104 # if cmd == 'q': 105 # break 106 # print('命令{x}正在运行'.format(x=cmd)) 107 # break # 立刻终止本层循环 108 # else: 109 # print('账号名或密码错误') 110 # 111 # # print('====end====') 112 113 # # 改变条件的方式 114 # tag=True 115 # while tag: 116 # inp_name=input('请输入您的账号:') 117 # inp_pwd=input('请输入您的密码:') 118 # 119 # if inp_name == username and inp_pwd == password: 120 # print('登录成功') 121 # while tag: 122 # cmd=input("输入命令>: ") 123 # if cmd == 'q': 124 # tag=False 125 # else: 126 # print('命令{x}正在运行'.format(x=cmd)) 127 # else: 128 # print('账号名或密码错误') 129 130 131 # 8、while +continue:结束本次循环,直接进入下一次 132 # 强调:在continue之后添加同级代码毫无意义,因为永远无法运行 133 # count=0 134 # while count < 6: 135 # if count == 4: 136 # count+=1 137 # continue 138 # # count+=1 # 错误 139 # print(count) 140 # count+=1 141 142 # 9、while +else:针对break 143 # count=0 144 # while count < 6: 145 # if count == 4: 146 # count+=1 147 # continue 148 # print(count) 149 # count+=1 150 # else: 151 # print('else包含的代码会在while循环结束后,并且while循环是在没有被break打断的情况下正常结束的,才会运行') 152 153 # count=0 154 # while count < 6: 155 # if count == 4: 156 # break 157 # print(count) 158 # count+=1 159 # else: 160 # print('======>') 161 162 163 # 应用案列: 164 # 版本1: 165 # count=0 166 # tag=True 167 # while tag: 168 # if count == 3: 169 # print('输错三次退出') 170 # break 171 # inp_name=input('请输入您的账号:') 172 # inp_pwd=input('请输入您的密码:') 173 # 174 # if inp_name == username and inp_pwd == password: 175 # print('登录成功') 176 # while tag: 177 # cmd=input("输入命令>: ") 178 # if cmd == 'q': 179 # tag=False 180 # else: 181 # print('命令{x}正在运行'.format(x=cmd)) 182 # else: 183 # print('账号名或密码错误') 184 # count+=1 185 186 # 版本2:优化 187 count = 0 188 while count < 3: 189 inp_name = input('请输入您的账号:') 190 inp_pwd = input('请输入您的密码:') 191 192 if inp_name == username and inp_pwd == password: 193 print('登录成功') 194 while True: 195 cmd = input("输入命令>: ") 196 if cmd == 'q': # 整个程序结束,退出所有while循环 197 break 198 else: 199 print('命令{x}正在运行'.format(x=cmd)) 200 break 201 else: 202 print('账号名或密码错误') 203 count += 1 204 else: 205 print('输错3次,退出')
1 ''' 2 1、什么是for循环 3 循环就是重复做某件事,for循环是python提供第二种循环机制 4 5 2、为何要有for循环 6 理论上for循环能做的事情,while循环都可以做 7 之所以要有for循环,是因为for循环在循环取值(遍历取值)比while循环更简洁 8 9 3、如何用for循环 10 语法: 11 for 变量名 in 可迭代对象:# 可迭代对象可以是:列表、字典、字符串、元组、集合 12 代码1 13 代码2 14 代码3 15 ... 16 ''' 17 # 一:for基本使用之循环取值 18 # 案例1:列表循环取值 19 # 简单版 20 # l = ['alex_dsb', 'lxx_dsb', 'egon_nb'] 21 # for x in l: # x='lxx_dsb' 22 # print(x) 23 24 # 复杂版: 25 # l = ['alex_dsb', 'lxx_dsb', 'egon_nb'] 26 # i=0 27 # while i < 3: 28 # print(l[i]) 29 # i+=1 30 31 32 # 案例2:字典循环取值 33 # 简单版 34 # dic={'k1':111,'k2':2222,'k3':333} 35 # for k in dic: 36 # print(k,dic[k]) 37 38 # 复杂版:while循环可以遍历字典,太麻烦了 39 40 # 案例3:字符串循环取值 41 # 简单版 42 # msg="you can you up,no can no bb" 43 # for x in msg: 44 # print(x) 45 46 # 复杂版:while循环可以遍历字典,太麻烦了 47 48 49 # 二:总结for循环与while循环的异同 50 # 1、相同之处:都是循环,for循环可以干的事,while循环也可以干 51 # 2、不同之处: 52 # while循环称之为条件循环,循环次数取决于条件何时变为假 53 # for循环称之为"取值循环",循环次数取决in后包含的值的个数 54 # for x in [1,2,3]: 55 # print('===>') 56 # print('8888') 57 58 59 # 三:for循环控制循环次数:range() 60 # in后直接放一个数据类型来控制循环次数有局限性: 61 # 当循环次数过多时,数据类型包含值的格式需要伴随着增加 62 # for x in 'a c': 63 # inp_name=input('please input your name: ') 64 # inp_pwd=input('please input your password: ') 65 # 66 67 # range功能介绍 68 ''' 69 >>> range(10) 70 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 71 >>> 72 >>> range(1,9) # 1...8 73 [1, 2, 3, 4, 5, 6, 7, 8] 74 >>> 75 >>> range(1,9,1) # 1 2 3 4 5 6 7 8 76 [1, 2, 3, 4, 5, 6, 7, 8] 77 >>> range(1,9,2) # 1 3 5 7 78 [1, 3, 5, 7] 79 ''' 80 # for i in range(30): 81 # print('===>') 82 83 84 # for+break: 同while循环一样 85 # for+else:同while循环一样 86 # username='egon' 87 # password='123' 88 # for i in range(3): 89 # inp_name = input('请输入您的账号:') 90 # inp_pwd = input('请输入您的密码:') 91 # 92 # if inp_name == username and inp_pwd == password: 93 # print('登录成功') 94 # break 95 # else: 96 # print('输错账号密码次数过多') 97 98 99 # 四:range补充知识(了解) 100 # 1、for搭配range,可以按照索引取值,但是麻烦,所以不推荐 101 # l=['aaa','bbb','ccc'] # len(l) 102 # for i in range(len(l)): 103 # print(i,l[i]) 104 # 105 # for x in l: 106 # print(l) 107 # 2、range()在python3里得到的是一只"会下蛋的老母鸡" 108 109 110 # 五:for+continue 111 # for i in range(6): # 0 1 2 3 4 5 112 # if i == 4: 113 # continue 114 # print(i) 115 116 # 六:for循环嵌套:外层循环循环一次,内层循环需要完整的循环完毕 117 # for i in range(3): 118 # print('外层循环-->', i) 119 # for j in range(5): 120 # print('内层-->', j) 121 122 # 补充:终止for循环只有break一种方案 123 124 # print('hello %s' % 'egon') 125 # 1、print之逗号的使用 126 # print('hello','world','egon') 127 # 2、换行符 128 # print('hello\n') 129 # print('world') 130 # 3、print值end参数的使用 131 # print('hello\n',end='') 132 # print('word') 133 print('hello',end='*') 134 print('world',end='*')
1 ''' 2 1、什么是for循环 3 循环就是重复做某件事,for循环是python提供第二种循环机制 4 5 2、为何要有for循环 6 理论上for循环能做的事情,while循环都可以做 7 之所以要有for循环,是因为for循环在循环取值(遍历取值)比while循环更简洁 8 9 3、如何用for循环 10 语法: 11 for 变量名 in 可迭代对象:# 可迭代对象可以是:列表、字典、字符串、元组、集合 12 代码1 13 代码2 14 代码3 15 ... 16 ''' 17 # 一:for基本使用之循环取值 18 # 案例1:列表循环取值 19 # 简单版 20 # l = ['alex_dsb', 'lxx_dsb', 'egon_nb'] 21 # for x in l: # x='lxx_dsb' 22 # print(x) 23 24 # 复杂版: 25 # l = ['alex_dsb', 'lxx_dsb', 'egon_nb'] 26 # i=0 27 # while i < 3: 28 # print(l[i]) 29 # i+=1 30 31 32 # 案例2:字典循环取值 33 # 简单版 34 # dic={'k1':111,'k2':2222,'k3':333} 35 # for k in dic: 36 # print(k,dic[k]) 37 38 # 复杂版:while循环可以遍历字典,太麻烦了 39 40 # 案例3:字符串循环取值 41 # 简单版 42 # msg="you can you up,no can no bb" 43 # for x in msg: 44 # print(x) 45 46 # 复杂版:while循环可以遍历字典,太麻烦了 47 48 49 # 二:总结for循环与while循环的异同 50 # 1、相同之处:都是循环,for循环可以干的事,while循环也可以干 51 # 2、不同之处: 52 # while循环称之为条件循环,循环次数取决于条件何时变为假 53 # for循环称之为"取值循环",循环次数取决in后包含的值的个数 54 # for x in [1,2,3]: 55 # print('===>') 56 # print('8888') 57 58 59 # 三:for循环控制循环次数:range() 60 # in后直接放一个数据类型来控制循环次数有局限性: 61 # 当循环次数过多时,数据类型包含值的格式需要伴随着增加 62 # for x in 'a c': 63 # inp_name=input('please input your name: ') 64 # inp_pwd=input('please input your password: ') 65 # 66 67 # range功能介绍 68 ''' 69 >>> range(10) 70 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 71 >>> 72 >>> range(1,9) # 1...8 73 [1, 2, 3, 4, 5, 6, 7, 8] 74 >>> 75 >>> range(1,9,1) # 1 2 3 4 5 6 7 8 76 [1, 2, 3, 4, 5, 6, 7, 8] 77 >>> range(1,9,2) # 1 3 5 7 78 [1, 3, 5, 7] 79 ''' 80 # for i in range(30): 81 # print('===>') 82 83 84 # for+break: 同while循环一样 85 # for+else:同while循环一样 86 # username='egon' 87 # password='123' 88 # for i in range(3): 89 # inp_name = input('请输入您的账号:') 90 # inp_pwd = input('请输入您的密码:') 91 # 92 # if inp_name == username and inp_pwd == password: 93 # print('登录成功') 94 # break 95 # else: 96 # print('输错账号密码次数过多') 97 98 99 # 四:range补充知识(了解) 100 # 1、for搭配range,可以按照索引取值,但是麻烦,所以不推荐 101 # l=['aaa','bbb','ccc'] # len(l) 102 # for i in range(len(l)): 103 # print(i,l[i]) 104 # 105 # for x in l: 106 # print(l) 107 # 2、range()在python3里得到的是一只"会下蛋的老母鸡" 108 109 110 # 五:for+continue 111 # for i in range(6): # 0 1 2 3 4 5 112 # if i == 4: 113 # continue 114 # print(i) 115 116 # 六:for循环嵌套:外层循环循环一次,内层循环需要完整的循环完毕 117 # for i in range(3): 118 # print('外层循环-->', i) 119 # for j in range(5): 120 # print('内层-->', j) 121 122 # 补充:终止for循环只有break一种方案 123 124 # print('hello %s' % 'egon') 125 # 1、print之逗号的使用 126 # print('hello','world','egon') 127 # 2、换行符 128 # print('hello\n') 129 # print('world') 130 # 3、print值end参数的使用 131 # print('hello\n',end='') 132 # print('word') 133 print('hello',end='*') 134 print('world',end='*')