Python中while语句的一般形式:

while 判断条件:

语句

同样需要注意冒号和缩进,另外在Python中没有do…while循环

下面的实例计算1到100总和

##calc.py
n = 100 sum = 0
counter = 1
while counter <= n:
sum = sum + counter
counter += 1 print("total from 1 to 100 : %d",sum)

运行结果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3.5 calc.py

total from 1 to 100 : %d 5050

while循环中使用else语句

在while…else在条件语句为false时执行els语句块

#while.py
count = 0
while count < 5:
print(count," < 5")
count = count + 1
else :
print(count ," >= 5")

运行结果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3.5 while.py

0  < 5

1  < 5

2  < 5

3  < 5

4  < 5

5  >= 5

for循环:

Python for循环可以遍历任何序列的项目,如一个列表或一个字符串

for循环的 一般格式如下

for <variable> in <sequence>:
<statement>
else:
<statement>

实例:

break语句用于跳出当前循环体:

##break.py
sites = ["Baidu","Google","Runoob","Taobao"]
for site in sites:
if site == "Runoob":
print("cainiao!")
break
print("loop data " + site)
else:
print("Having no loop data!")
print("loop end!")

运行结果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3.5 break.py

loop data Baidu

loop data Google

cainiao!

loop end!

range()函数

如果你需要遍历数字序列,可以使用内置的range()函数,它会生成数列,例如:

也可以使range以指定数字开始并指定不同的增量,(甚至可以是负数,有时这也叫步长)

负数:

也可以结合range()和len()函数以遍历一个序列的索引:

还可以使用range()函数来创建一个列表:

break和continue语句及循环中的else子句

break语句可以跳出for和while循环体,如果你从for或while循环终止,任何对应的循环else块将不执行:

#else.py
for letter in 'Runoob':
if letter == 'b':
break;
print('the current letter : ',letter) print("the next example") var = 10
while var > 0:
print('the current variable : ',var)
var = var - 1
if var == 5:
break;
print("GOOF bye!")

运行结果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3 else.py

the current letter :  R

the current letter :  u

the current letter :  n

the current letter :  o

the current letter :  o

the next example

the current variable :  10

the current variable :  9

the current variable :  8

the current variable :  7

the current variable :  6

GOOF bye!

continue语句被用来Python跳出当前循环块的剩余语句,然后继续下一轮循环。

循环语句可以有else子句,它在穷尽列表(for循环)或条件变为false(以while循环)导致循环终止时被执行,但循环被break终止时不执行。

下列是查询质数的循环例子:

##prime.py
for n in range(2,10):
for x in range(2,n):
if n % x == 0:
print(n," == ",x, '*', n//x )
break
else:
print(n," is prime")

运行结果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3 prime.py

2  is prime

3  is prime

4  ==  2 * 2

5  is prime

6  ==  2 * 3

7  is prime

8  ==  2 * 4

9  ==  3 * 3

pass语句

Python pass是空语句,是为了保持程序结构的完整性。

pass不做任何事情,一般用作占位语句:

#pass.py
for letter in 'Runoob':
if letter == 'o':
pass
print('execute pass block')
print('the current letter : ',letter) print("Good bye!")

运行结果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3 pass.py

the current letter :  R

the current letter :  u

the current letter :  n

execute pass block

the current letter :  o

execute pass block

the current letter :  o

the current letter :  b

Good bye!

pass只是为了防止语法的错误

pass就是一条空语句,在代码段中或定义函数时,如果没有内容,或者就先不做任何处理,直接跳过,就可以先使用pass

十进制转换:

#translate.py
while True:
number = input('please input a integer(enter Q exit ):')
if number in ['q','Q']:
break
elif not number.isdigit():
print("input error,please continue input : ")
continue
else:
number = int(number)
print("decimal --> hexadecimal: %d -> 0x%x"%(number,number))
print("decimal --> octonary: %d -> 0x%o"%(number,number))
print("decimal --> binary: %d -> "%number,bin(number))

运行结果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3 translate.py

please input a integer(enter Q exit ):9

decimal --> hexadecimal: 9 -> 0x9

decimal --> octonary: 9 -> 0x11

decimal --> binary: 9 ->  0b1001

please input a integer(enter Q exit ):18

decimal --> hexadecimal: 18 -> 0x12

decimal --> octonary: 18 -> 0x22

decimal --> binary: 18 ->  0b10010

please input a integer(enter Q exit ):q

Python3循环的更多相关文章

  1. Python3 循环语句

    Python3 循环语句 转来的  很适合小白   感谢作者   Python中的循环语句有 for 和 while. Python循环语句的控制结构图如下所示: while 循环 Python中wh ...

  2. 【python】Python3 循环语句

    [python]几种常见的循环 注意:如果涉及到程序中print语句中含有%d,%s,那么要在脚本最开始写语句:#coding=utf-8,才能够正常输出想要的数字或者字符串. Python3 循环语 ...

  3. Python3循环语句

    Python3 循环语句 Python中的循环语句有for和while. 循环语句控制结构图如下: 一.while循环 ①循环结构 while 判断条件: 执行语句 实例: n = int(input ...

  4. python013 Python3 循环语句

    Python3 循环语句本章节将为大家介绍Python循环语句的使用.Python中的循环语句有 for 和 while.Python循环语句的控制结构图如下所示: while 循环Python中wh ...

  5. python3循环语句while

    Python的循环语句有for和while语句,这里讲while语句. Python中while语句的一般形式: while 条件判断 : 语句 需要注意冒号和缩进.另外,注意Python中没有do. ...

  6. Python3 循环

    Python中的循环语句有 for 和 while. Python循环语句的控制结构图如下所示: while 循环 Python中while语句的一般形式: while 判断条件: statement ...

  7. Python3 循环语句(十)

    Python中的循环语句有 for 和 while. Python循环语句的控制结构图如下所示: while 循环 Python中while语句的一般形式: while 判断条件: 语句 同样需要注意 ...

  8. Python3 循环表达式

    一 While循环 基本循环 while 条件: 执行内容 #循环体 ... #循环体 ... #循环体 # 若条件为真,执行循环体内容 # 若条件为假,不执行循环体内容 实例1(Python 3.0 ...

  9. (三)Python3 循环语句——while

    while语句的一般形式: while 判断条件: 语句 同样需要注意冒号和缩进.另外,在 Python 中没有 do..while 循环. 以下实例使用了 while 来计算 1 到 100 的总和 ...

  10. (四)Python3 循环语句——for

    for循环的一般格式如下: for <variable> in <sequence>: <statements> else: <statements> ...

随机推荐

  1. Eclipse RCP使用SWT.EMBEDDED方式显示batik的svgCanvas后窗口最大化变白问题

    // 设置svg组件一直动态(这一行代码导致了最大化变白的问题)svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);// 以下两行解决最大化变白 ...

  2. 1.sql统计语句

    select exam_item_code, exam_item, EXAMDATE, count(distinct patient_id) from (select t2.exam_item_cod ...

  3. Spring Security Oauth2 : Possible CSRF detected

    Spring Security Oauth2 : Possible CSRF detected 使用Spring Security 作为 Oauth2 授权服务器时,在授权服务器登录授权后,重定向到客 ...

  4. MySQL LAST_INSERT_ID()用法

    last_insert_id()函数是适用于id为自动生成的表 下面是插入表数据时last_insert_id()函数的两种用法: 表结构: 此表使用last_insert_id()函数的字段为par ...

  5. git bash push 本地的commit到远程 -- ssh keys设置

    1.  检查是否已经创建 ssh keys git bash 下,cd ~/.ssh 如何出现“No such file or directory”,则表示需要创建一个ssh keys. 2. 创建新 ...

  6. 进程间的通信—套接字(socket)

      前面说到的进程间的通信,所通信的进程都是在同一台计算机上的,而使用socket进行通信的进程可以是同一台计算机的进程,也是可以是通过网络连接起来的不同计算机上的进程.通常我们使用socket进行网 ...

  7. 如何使用点击超链接的方式打开Android手机上的应用

    在Android应用的AndroidManifest.xml里加入如下的配置片段: <action android:name="my_action"/> <cat ...

  8. Computer Vision_33_SIFT:An efficient SIFT-based mode-seeking algorithm for sub-pixel registration of remotely sensed images——2015

    此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...

  9. Vue 中的Vue Router一级路由,二级路由,三级路由以及跳转

    今天编写了一下Vue中的路由 先用命令行新建一个空的项目,并且我知道要用路由,就下载了路由的相关依赖 vue init webpack demo5 完毕之后进入所在的项目 cd demo5 之后用vs ...

  10. KubeEdge,一个Kubernetes原生边缘计算框架

    ​KubeEdge成为第一个Kubernetes原生边缘计算平台,Edge和云组件现已开源! 开源边缘计算正在经历其业界最具活力的发展阶段.如此多的开源平台,如此多的整合以及如此多的标准化举措!这显示 ...