文档章节

Python教程-循环语句

智能小松鼠
 智能小松鼠
发布于 2016/11/15 11:16
字数 698
阅读 16
收藏 0

while

1、http://www.yiibai.com/python/python_loops.html

2、http://www.yiibai.com/python/python_while_loop.html

3、http://www.cnblogs.com/way_testlife/archive/2010/06/14/1758276.html

4、http://www.runoob.com/python/python-while-loop.html

while expression:
   statement(s)
#!/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!

The Infinite Loop:

#!/usr/bin/python

var = 1
while var == 1 :  # This constructs an infinite loop
   num = raw_input("Enter a number  :")
   print "You entered: ", num

print "Good bye!"
Enter a number  :20
You entered:  20
Enter a number  :29
You entered:  29
Enter a number  :3
You entered:  3
Enter a number between :Traceback (most recent call last):
  File "test.py", line 5, in <module>
    num = raw_input("Enter a number :")
KeyboardInterrupt

The else Statement Used with Loops

#!/usr/bin/python

count = 0
while count < 5:
   print count, " is  less than 5"
   count = count + 1
else:
   print count, " is not less than 5"
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5

Single Statement Suites:

#!/usr/bin/python

flag = 1

while (flag): print 'Given flag is really true!'

print "Good bye!"

for

1、http://www.yiibai.com/python/python_for_loop.html

for iterating_var in sequence:
   statements(s)
#!/usr/bin/python

for letter in 'Python':     # First Example
   print 'Current Letter :', letter

fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        # Second Example
   print 'Current fruit :', fruit

print "Good bye!"
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!

内嵌循环

1、http://www.yiibai.com/python/python_nested_loops.html

for iterating_var in sequence:
   for iterating_var in sequence:
      statements(s)
   statements(s)
while expression:
   while expression:
      statement(s)
   statement(s)
#!/usr/bin/python

i = 2
while(i < 100):
   j = 2
   while(j <= (i/j)):
      if not(i%j): break
      j = j + 1
   if (j > i/j) : print i, " is prime"
   i = i + 1

print "Good bye!"
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime
Good bye!

循环控制语句:

break

#!/usr/bin/python

for letter in 'Python':     # First Example
   if letter == 'h':
      break
   print 'Current Letter :', letter
  
var = 10                    # Second Example
while var > 0:              
   print 'Current variable value :', var
   var = var -1
   if var == 5:
      break

print "Good bye!"
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0
Good bye!

pass

#!/usr/bin/python

for letter in 'Python': 
   if letter == 'h':
      pass
      print 'This is pass block'
   print 'Current Letter :', letter

print "Good bye!"
Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!

continue

#!/usr/bin/python

for letter in 'Python':     # First Example
   if letter == 'h':
      continue
   print 'Current Letter :', letter

var = 10                    # Second Example
while var > 0:              
   var = var -1
   if var == 5:
      continue
   print 'Current variable value :', var
print "Good bye!"
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0
Good bye!

© 著作权归作者所有

智能小松鼠
粉丝 312
博文 356
码字总数 37236
作品 0
海淀
QA/测试工程师
私信 提问
人人都能学会的python编程教程8:条件判断与循环

先公布上期编程练习的答案,没错,L是一个指向三个列表的二维元祖。 条件判断 实际的项目中条件判断可以说是使用最多的语法之一了,不管是最简单的判断还是负责的业务逻辑和算法,条件判断都...

编程老司机
2018/05/10
0
0
人人都能学会的python编程教程4:关系运算符与循环

关系运算符 if 语句 实际应用中的程序,大多不是一撮而就的,而是根据条件不同存在很多分支。 最基本的条件分支结构是if...else...语句即如果。。。否则。。。 在python当中,if condition1...

编程老司机
2018/05/10
0
0
人人都能学会的python编程教程5:循环-2

“死循环”是必须要避免的,当然“活循环”也未必都要执行完。 break 语句 比如我们要从一个列表中找到某个特定元素,那么只要找到了这次循环就可以停止了,没有必要非得走到最后。 这种情况...

编程老司机
2018/05/10
0
0
Python编程入门——基础语法详解

今天小编给大家带来Python编程入门——基础语法详解。 一、基本概念 1.内置的变量类型: Python是有变量类型的,而且会强制检查变量类型。内置的变量类型有如下几种: #浮点 float_number=2...

诸葛青云999
2018/08/19
0
0
python超详细的基础笔记你学会了么

python简介 python是一种面向对象的解释型计算机程序设计语言,python的是吉多·范罗苏姆(Guido van Rossum)于1989年发明 任何语言都有优缺点,python也不例外,python的优点是简单易懂、可...

糖宝lsh
2018/08/15
0
0

没有更多内容

加载失败,请刷新页面

加载更多

mysql mysql中的索引

一、索引的分类 普通索引 唯一性索引 unique约束设置唯一索引,主键是一种特殊的唯一索引。 全文索引 查询较大数据量时,使用全文索引可以提高查询速度。 单列索引 包括以上三个索引 多列索引...

edison_kwok
8分钟前
0
0
Springboot 集成redis

这里只做单机版简单集成,不过为项目中使用 1.在pom文件中引入支持 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redi......

贾峰uk
13分钟前
1
0
Qt编写安防视频监控系统4-删除视频

一、前言 一般会有两种处理方式来删除视频,一种是鼠标右键菜单,删除当前视频或者删除所有视频,一种是直接按住当前视频,移到视频通道界面以外就表示删除当前视频,这也是个比较人性化的设...

飞扬青云
17分钟前
0
0
NCRE考试感想 三级信息安全(下)

时代的变迁 一些学科的兴起,一些学科的没落;一些职业的兴起,一些职业的没落。在2016年,新闻报道中说:国务院取消了百余项职业资格证书。一切都处于变动之中,要找到变化的规律,跟上时代...

志成就
19分钟前
0
0
MySQL索引原理及慢查询优化,了解一下?

MySQL凭借着出色的性能、低廉的成本、丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库。 虽然性能出色,但所谓“好马配好鞍”,如何能够更好的使用它,已经成为开发工程师的必修课...

骚年锦时
24分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部