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月前
|
Python
Python 学习之路 03 之循环
03-运行时数据区概述及线程
34 0
|
2月前
|
Python
python用户输入和while循环(四)
python用户输入和while循环(四)
28 1
|
24天前
|
程序员 Python
Python控制结构:条件语句和循环详解
【4月更文挑战第8天】本文介绍了Python的两种主要控制结构——条件语句和循环。条件语句包括`if`、`elif`和`else`,用于根据条件执行不同代码块。`if`检查条件,`else`提供替代路径,`elif`用于多个条件检查。循环结构有`for`和`while`,前者常用于遍历序列,后者在满足特定条件时持续执行。`for`可结合`range()`生成数字序列。`while`循环适用于未知循环次数的情况。循环控制语句`break`和`continue`能改变循环执行流程。理解和熟练运用这些控制结构是Python编程的基础。
|
2月前
|
存储 索引 Python
python用户输入和while循环(五)
python用户输入和while循环(五)
18 0
|
2月前
|
Python
python用户输入和while循环(三)
python用户输入和while循环(三)
20 0
|
2月前
|
存储 算法 索引
python用户输入和while循环(六)
python用户输入和while循环(六)
18 0
|
2月前
|
存储 索引 Python
python用户输入和while循环(七)
python用户输入和while循环(七)
16 0
|
2天前
|
Python 容器
Python中的for循环用法详解,一文搞定它
Python中的for循环用法详解,一文搞定它
|
2天前
|
Python
Python中的while循环,知其然知其所以然
Python中的while循环,知其然知其所以然
|
8天前
|
算法 数据挖掘 数据处理
使用 Python 循环创建多个列表
在Python中,动态创建多个列表对于数据处理和算法实现十分有用。本文介绍了四种方法:1) 列表推导式,如创建偶数和奇数列表;2) 使用循环和`append()`,示例为生成斐波那契数列;3) 结合字典与循环,按条件(如正负数)分组;4) 列表生成器,用于一次性生成多组随机数列表。这些方法有助于提高代码效率和可读性。
20 1