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

 

相关文章
|
1月前
|
Python
python用户输入和while循环(四)
python用户输入和while循环(四)
26 1
|
1月前
|
安全 Python
python用户输入和while循环(二)
python用户输入和while循环(二)
17 0
|
1月前
|
Python
在Python中while循环
在Python中while循环
16 1
|
1月前
|
Python
python用户输入和while循环(一)
python用户输入和while循环(一)
16 0
|
1月前
|
Python
Python循环语句
Python循环语句
17 0
|
1月前
|
存储 索引 Python
python用户输入和while循环(五)
python用户输入和while循环(五)
17 0
|
1月前
|
Python
python用户输入和while循环(三)
python用户输入和while循环(三)
19 0
|
1月前
|
Python
Python-循环
Python-循环
19 1
|
1月前
|
存储 算法 索引
python用户输入和while循环(六)
python用户输入和while循环(六)
18 0
|
1月前
|
存储 索引 Python
python用户输入和while循环(七)
python用户输入和while循环(七)
16 0

热门文章

最新文章