Python While 循环语句

Python While循环语句

Python 编程中while语句用于循环执行程序,即在一些条件下,循环执行一些段程序,以处理需要重复处理的相同任务。

执行语句可以是单个语句或语句块。

判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。

当判断条件假false时,循环结束。

实例:

#!/usr/bin/python

count = 0

while ( count < 9):

print ‘The count is:‘, count

count = count +1

print " Good bye!"

以上代码执行输出结果:

The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!

while语句还有另外两个重要的命令 continue,break 来跳过循环。

  • continue 用于跳过该次循环。
  • break 则用于退出循环,此外“判断条件”还可以是个常值,表示循环必定成立。

具体操作如下:

# continue 和break 用法:

i = 1

while i < 10:

i + = 1

if 1%2 >0:    # 非双数时跳过输出

continue

print i      # 输出双数2、4、6、8、10

i = 1

while = 1:     # 循环条件为1必定成立

print i

i + = 1

if i > 10:    # 当i大于10时跳出循环

break

时间: 2024-08-09 21:19:23

Python While 循环语句的相关文章

Python for循环语句

来自:http://www.runoob.com/python/python-for-loop.html Python for 循环语句 Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串. 语法: for循环的语法格式如下: for iterating_var in sequence: statements(s) 流程图: 实例: 实例 #!/usr/bin/python # -*- coding: UTF-8 -*- for letter in 'Python': #

Python学习-7.Python的循环语句-for语句

Python中循环可以使用for语句来实现 1 list = ['Tom','Lucy','Mary'] 2 for name in list: 3 print(name) 则将会依次输出Tom Lucy Mary 另外Python还支持continue和break关键字,用法与C#相同. 比较有特点的是Python的for语句中支持else关键字 例子: 1 max = 15 2 for i in range(10): 3 if(i==max): 4 break 5 print(i) 6 el

Python的循环语句

Python的循环有两种,一种是for...in循环,第二种循环是while循环,只要条件满足,就不断循环,条件不满足时退出循环 names = ['Michael', 'Bob', 'Tracy'] for name in names: print(name) Michael Bob Tracy #所以for x in ...循环就是把每个元素代入变量x,然后执行缩进块的语句 sum = 0 for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: sum = sum

python while循环语句

while循环语句 import time  #时 while 条件: 代码块 time.sleep(1)  #延时1秒 ################ import time kaishi =1 flag = true while True: print(kaishi) if kaishi == 10 : flag = False  #(break) #########break 用于跳出循环 kaishi = kaishi + 1 #########continue 用于跳出当前循环 ti

Python条件循环语句

print 'x','y' 相当于 print 'x' print 'y' 输出 x y ------------------------------------------------------------------------------- 从模块导入函数的时候,可以 1. import somemodule                                                                                          

Python 4 循环语句while

while  [条件]:        条件这里满足布尔运算True则无限循环while里面代码. 固定条件的 基本的while循环, 如果if匹配那么 则执行打印登录成功,和break跳出整个循环,否则则执行else输入并重新循环. while 1==1: user = input('用户名:') pwd = input('密码:') if user == "admin" and pwd =="pwd" print("登录成功") break

【Python】循环语句

while循环 当条件成立时,循环体的内容可以一直执行,但是避免死循环,需要有一个跳出循环的条件才行. for循环 遍历任何序列(列表和字符串)中的每一个元素 >>> a = ["China", 'is', 'powerful'] >>> for x in a: ... print(x) ... China is powerful range() 函数 生成一个等差数列(并不是列表). range(4) => range(0, 4) list(

Python学习-8.Python的循环语句-while语句

例子: 1 i = 1 2 while i < 10: 3 print(i) 4 i+=1 5 else: 6 print('finish') 输出1至9和finish 在while语句中同样支持for语句所支持的continue.break和else

python的循环语句等

names = ['Michael', 'Bob', 'Tracy'] for name in names: print name sum = 0 for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: sum = sum + x print sum sum = 0 n = 99 while n > 0: sum = sum + n n = n - 2 print sum 以上这三种就已经够用了