标签:
while 循环
while 循环 while 条件: 如果条件是真的,就继续的循环 如果条件是假的,就停止循环
循环的意思就是让程序重复地执行某些语句,whiler循环就是循环结构的一种,当事先不知道循环该执行多少次,就要用到while循环
Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为
while 判断条件:
执行语句……
这块一定要注意缩进
执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。
当判断条件假false时,循环结束。
循环 1--10的整数 kaixin= 1 falag = True while falag: print(kaishi) if kaishi == 10: falag = False kaishi += 1 D:\python3.5\python.exe D:/untitled/www.py 1 2 3 4 5 6 7 8 9 10 Process finished with exit code 0
while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立,具体用法如下:
循环 1--10 不包括7的整数
a = 1
while True: 如果条件是真 往下执行
if a == 7: 如果条件不满足7时继续执行
a += 1 递增加一
continue 跳出本次循环
print(a)
if a == 10:
break #跳出循环 下面的代码不再执行
a += 1
D:\python3.5\python.exe D:/untitled/www.py
1
2
3
4
5
6
8
9
10
Process finished with exit code 0
另一种方法:
xin = 1
while xin < 11:
if xin == 3:
print (xin)
else:
print (xin)
xin += 1
D:\python3.5\python.exe D:/untitled/www.py
1
2
3
4
5
6
7
8
9
10
Process finished with exit code 0
在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
实现用户登录三次机会
q = 0 初始值
while q < 3: 如果 q 不满足 3时 就向下执行
user = input(‘username:‘) 请用户输入
pwd = input(‘password:‘) 请输入密码
if user == ‘xin‘and pwd == ‘123‘: 如果输入用户名和密码
print (‘yes‘)
break 跳出当前循环 下面的代码不再执行
else: 否则
print (‘rey again‘)
q += 1
D:\python3.5\python.exe D:/untitled/www.py
username:ewq
password:123
rey again
username:ewq
password:123
rey again
username:xin
password:123
yes
Process finished with exit code 0
1----100 的和
a = 0
q = 1
while q < 100:
a += q
q += 1
print(a)
D:\python3.5\python.exe D:/untitled/lianxi.py
4950
Process finished with exit code 0
1--100 的所有奇数
i = 1
while i < 100:
if i % 2 == 1:
print(i)
i +=1
D:\python3.5\python.exe D:/untitled/lianxi.py
1
3
5
7
...
...
...
95
97
99
Process finished with exit code 0
1---100 的 所有偶数
a = 0
while True:
print(a)
a += 2
if a > 100:
break
D:\python3.5\python.exe D:/untitled/lianxi.py
0
2
4
6
8
...
...
...
...
...
96
98
100
Process finished with exit code 0
循环 1----15 不包括7,10的整数
i = 0 # 最后的值 初始值循环的第一个值
while i<=15: #条件是真的就往下执行
i += 1 #当初始值不满足条件时就加一
if i == 7: #如果条件满足时
continue # 就停止循环 继续下面的代码
i += 1 #当初始值不满足条件时就加一
if i ==10: #如果条件满足时
continue #就停止循环 继续下面的代码
print(i)
D:\python3.5\python.exe D:/untitled/lianxi.py
1
2
3
4
5
6
8
9
11
12
13
14
15
16
Process finished with exit code 0
2-3+4.。。100 的和
q = 0 #初始值 # a = 2 #循环初始值 # while a<= 100: #条件是不是真的 # c = a % 2 #判断结果是不是哦数 # if c == 0: #判断是不是是偶数 # q += a #偶数就相加 # else: #否则就是奇数 # q -= a #奇数就相减 # a += 1 # 递增加1 # print(q) D:\python3.5\python.exe D:/untitled/lianxi.py -51 Process finished with exit code 0
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
字符串
q = ‘xin‘
for i in q:
print(i)
D:\python3.5\python.exe D:/untitled/lianxi.py
x
i
n
Process finished with exit code 0
列表
q = [1,2,3,4]
for i in q:
print(i)D:\python3.5\python.exe D:/untitled/lianxi.py
1
2
3
4
Process finished with exit code 0
另外一种执行循环的遍历方式是通过索引,如下实例:
li = [‘alex‘,‘rice‘,‘rain‘]
for i in range(len(li)):
print(i)
结果
0,1,2
1 -2 +3.。。99的和
i = 0
a = 1
for a in range(1,99):
if a % 2 == 1:
i += a
else:
i -= a
a += 1
print(i)
D:\python3.5\python.exe D:/untitled/lianxi.py
-49
Process finished with exit code 0
range() 范围的意思
enumrate
为可迭代的对象添加序号:
li = [‘alex‘,‘rice‘,‘rain‘]
for k,v in enumerate(li,100):
print(k,v)
D:\python3.5\python.exe D:/untitled/lianxi.py
100 alex
101 rice
102 rain
Process finished with exit code 0
标签:
原文地址:http://www.cnblogs.com/guokaixin/p/5469601.html