python 循环语句

简介: while循环#!/usr/bin/pythoncount = 0while (count < 9): print 'The count is:', count count = count + 1print "Good bye!"结果:The count is: 0...

while循环

#!/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!

continue 和break的使用:

# !/usr/bin/python
# -*- coding: UTF-8 -*-
i = 1
while i < 10:
    i += 1
    if i % 2 > 0:  # 非双数时跳过输出
        continue
    print i  # 输出双数2、4、6、8、10
print "----------------------------------"
i = 1
while 1:  # 循环条件为1必定成立
    print i  # 输出1~10
    i += 1
    if i > 10:  # 当i大于10时跳出循环
        break

结果:

2
4
6
8
10
----------------------------------
1
2
3
4
5
6
7
8
9
10

无限循环:

var = 1
while var == 1 :  # 该条件永远为true,循环将无限执行下去
   num = raw_input("Enter a number  :")
   print "You entered: ", num

print "Good bye!"

结果 因为var始终=1,因此会不换要求你填写内容,然后输出你写的内容:

Enter a number  :soyoungboy
You entered:  soyoungboy
Enter a number  :haha
You entered:  haha
Enter a number  :

循环while+else

当满足count不小于5的条件走else语句

#!/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

死循环:

#!/usr/bin/python

flag = 1

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

print "Good bye!"

 for循环

# !/usr/bin/python
# -*- coding: UTF-8 -*-

for letter in 'Python':  # 第一个实例
    print '当前字母 :', letter

fruits = ['banana', 'apple', 'mango']
for fruit in fruits:  # 第二个实例
    print '当前水果 :', fruit

print "Good bye!"

结果:

当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!

序列索引迭代

fruits = ['banana', 'apple', 'mango']
i = len(fruits)
l = range(i)
# i相当于size,l相当于position
print i
print l
for index in l:
    print '当前水果 :', fruits[index]

print "Good bye!"

结果:

3
[0, 1, 2]
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!

循环使用else语句

for num in range(10, 20):  # 迭代 10 到 20 之间的数字
    for i in range(2, num):  # 根据因子迭代
        if num % i == 0:  # 确定第一个因子
            j = num / i  # 计算第二个因子
            print '%d 等于 %d * %d' % (num, i, j)
            break  # 跳出当前循环
    else:  # 循环的 else 部分
        print num, '是一个质数'

结果:

10 等于 2 * 5
11 是一个质数
12 等于 2 * 6
13 是一个质数
14 等于 2 * 7
15 等于 3 * 5
16 等于 2 * 8
17 是一个质数
18 等于 2 * 9
19 是一个质数

循环嵌套:

# !/usr/bin/python
# -*- coding: UTF-8 -*-

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, " 是素数"
    i = i + 1

print "Good bye!"

结果:

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

break语句:

# !/usr/bin/python
# -*- coding: UTF-8 -*-
for letter in 'hello world':  # First Example
    if letter == 'o':
        break
    print 'Current Letter :', letter

结果:

Current Letter : h
Current Letter : e
Current Letter : l
Current Letter : l

continue语句

# !/usr/bin/python
# -*- coding: UTF-8 -*-
for letter in 'hello world':  # First Example
    if letter == 'o':
        continue
    print 'Current Letter :', letter
Current Letter : h
Current Letter : e
Current Letter : l
Current Letter : l
Current Letter :  
Current Letter : w
Current Letter : r
Current Letter : l
Current Letter : d

pass语句 占位语句:

for letter in 'hello world':  # First Example
    if letter == 'o':
        pass
        print "-----------我是分割线-----------"
    print 'Current Letter :', letter

结果:

Current Letter : h
Current Letter : e
Current Letter : l
Current Letter : l
-----------我是分割线-----------
Current Letter : o
Current Letter :  
Current Letter : w
-----------我是分割线-----------
Current Letter : o
Current Letter : r
Current Letter : l
Current Letter : d

 

相关文章
|
2月前
|
算法 Java Docker
(Python基础)新时代语言!一起学习Python吧!(三):IF条件判断和match匹配;Python中的循环:for...in、while循环;循环操作关键字;Python函数使用方法
IF 条件判断 使用if语句,对条件进行判断 true则执行代码块缩进语句 false则不执行代码块缩进语句,如果有else 或 elif 则进入相应的规则中执行
293 2
|
5月前
|
Python
Python中的循环可以嵌套使用吗?
Python中的循环可以嵌套使用吗?
343 57
|
7月前
|
机器学习/深度学习 算法 关系型数据库
Python循环进阶:嵌套与控制的深度解析
本文深入探讨Python中嵌套循环的原理与应用,从数学模型到工程实践全面解析。内容涵盖嵌套循环的本质(如笛卡尔积实现、变量作用域)、精细控制技巧(如break/continue、迭代器协议、异常处理),以及性能优化策略(预计算、向量化等)。同时结合树形结构遍历、动态规划、游戏开发等典型场景,提供最佳实践建议。掌握这些技巧,助你突破编程瓶颈,实现复杂问题的优雅解决。
249 6
|
8月前
|
存储 Shell 开发者
Python用户输入与While循环
本文介绍了Python中用户输入与while循环的结合使用,通过`input()`函数获取用户输入,并利用while循环实现重复操作,如创建交互式程序或用户驱动的循环。示例代码展示了如何让用户输入数字并计算总和,直到输入指定退出命令。这种组合能帮助开发者构建强大的交互式Python应用。
235 1
|
开发工具 Python
[oeasy]python043_自己制作的ascii码表_循环语句_条件语句_缩进_indent
本文介绍了如何使用Python制作ASCII码表,回顾了上一次课程中`print`函数的`end`参数,并通过循环和条件语句实现每8个字符换行的功能。通过调整代码中的缩进,实现了正确的输出格式。最后展示了制作完成的ASCII码表,并预告了下一次课程的内容。
160 2
|
Python
在 Python 中实现各种类型的循环判断
在 Python 中实现各种类型的循环判断
264 2
|
Python
Python 中,循环判断
Python 中,循环判断
268 1
|
人工智能 Python
[oeasy]python039_for循环_循环遍历_循环变量
本文回顾了上一次的内容,介绍了小写和大写字母的序号范围,并通过 `range` 函数生成了 `for` 循环。重点讲解了 `range(start, stop)` 的使用方法,解释了为什么不会输出 `stop` 值,并通过示例展示了如何遍历小写和大写字母的序号。最后总结了 `range` 函数的结构和 `for` 循环的使用技巧。
139 5
|
Java 索引 Python
【10月更文挑战第19天】「Mac上学Python 30」基础篇11 - 高级循环技巧与应用
本篇将介绍更深入的循环应用与优化方法,重点放在高级技巧和场景实践。我们将讲解enumerate()与zip()的妙用、迭代器与生成器、并发循环以及性能优化技巧。这些内容将帮助您编写更高效、结构更合理的代码。
159 5
|
Python
Python 循环语句的高级应用与深度探索
本文深入探讨了Python中循环语句的高级应用,包括`for`循环遍历字典获取键值、同步遍历多个序列,以及`while`循环结合条件判断和异常处理。通过嵌套循环实现了矩阵乘法,并介绍了如何优化循环以提升程序性能。示例代码展示了这些技术的实际应用。
156 15

推荐镜像

更多