1.1 while 循环
#!/usr/bin/python
# -*- coding: UTF-8 -*-
i = 1
while i <10:
i = i + 1
print i
# while循环
numbers = raw_input("输入几个数字,用逗号分隔:").split(",") #去除逗号
print numbers #输出列表
x = 0
while x < len(numbers): #输出列表元素
print numbers[x]
x += 1
# 带else子句的while循环
x = input("输入x的值:")
i = 0
while(x <> 0):
if(x > 0):
x -= 1
else:
x += 1
i = i + 1
print "第%d次循环:" %i, x
else:
print "x等于0:", x
输出:
>>>
2
3
4
5
6
7
8
9
10
输入几个数字,用逗号分隔:10,12,15
['10', '12', '15']
10
12
15
输入x的值:7
第1次循环: 6
第2次循环: 5
第3次循环: 4
第4次循环: 3
第5次循环: 2
第6次循环: 1
第7次循环: 0
x等于0: 0
1.2 for 循环
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# for in语句
for x in range(-1, 2):
if x > 0:
print "正数:",x
elif x == 0 :
print "零:",x
else:
print "负数:",x
else:
print "循环结束"
# 传统for循环的实现
x = 0
while x < 5:
print x
x = x + 2
for x in range(0, 5, 2):
print x
输出:
>>>
负数: -1
零: 0
正数: 1
循环结束
0
2
4
0
2
4
对list实现循环赋值
>>> l = [[None]*3 for x in range (6)]
>>> l
[[None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None]]
>>> l = [[1]*3 for x in range (6)]
>>> l
[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
>>> l = [[x]*3 for x in range (6)]
>>> l
[[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5]]
>>> l = [[x]*2 for x in range (6)]
>>> l
[[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]
>>> l = [x*1 for x in range (6)]
>>> l
[0, 1, 2, 3, 4, 5]
>>> l = [x*2 for x in range (6)]
>>> l
[0, 2, 4, 6, 8, 10]
>>> l = [x*2+3 for x in range (6)]
>>> l
[3, 5, 7, 9, 11, 13]
>>>
1.3 break 和continue 语句
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# break语句
x = input("输入x的值:")
y = 0
for y in range(0, 100):
if x == y:
print "找到数字:", x
break
else:
print "没有找到"
输出:
>>>
输入x的值:78
找到数字: 78
>>>
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# continue语句
x = input("输入x的值:")
y = 0
for y in range(0, 100):
if x <> y:
print "y=:", y
continue
else:
print "x-", x
break
输出:
>>>
输入x的值:5
y=: 0
y=: 1
y=: 2
y=: 3
y=: 4
x- 5
>>>
1.4 冒泡排序
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# xrange() #xrange用法 xrange([start],stop[,step]) range将返回一个普通列表,但是xrange将返回一个特殊目的对象,将显示为其自身的特殊方式
x = xrange(0,8)
print x
print x[0]
print x[7]
# 冒泡排序
def bubbleSort(numbers): # 冒泡算法的实现
for j in xrange(len(numbers) - 1, -1, -1):
for i in xrange(j):
if numbers[i] > numbers[i+1]: # 把数值小的数字放到顶端
numbers[i], numbers[i+1] = numbers[i+1], numbers[i]
print numbers
def main(): # 主函数
numbers = [23, 12, 9, 15, 6]
bubbleSort(numbers)
if __name__ == '__main__':
main()
关于步长为负数时 range的解释,
>>> for j in xrange(5, 1, -1):
print j
输出:
5
4
3
2
也就是第一个参数减1,
为了使循环看起来更容易,对输出时加了些说明,情况如下:
# 冒泡排序
def bubbleSort(numbers): # 冒泡算法的实现
for j in xrange(len(numbers) - 1, -1, -1):
print '\n'
print "第j=%d时外循环情况"%j
for i in xrange(j):
print "第i=%d次比较 "%i
if numbers[i] > numbers[i+1]: # 把数值小的数字放到顶端
numbers[i], numbers[i+1] = numbers[i+1], numbers[i] #该写法等同于C中的b = numbers[i], numbers[i] = numbers[i+1], numbers[i+1] = b,分行书写证实可用
print numbers
def main(): # 主函数
numbers = [23, 12, 9, 15, 6, 27]
bubbleSort(numbers)
if __name__ == '__main__':
main()
输出:整个排序中相邻比较的过程
>>>
第j=5时外循环情况
第i=0次比较
[12, 23, 9, 15, 6, 27]
第i=1次比较
[12, 9, 23, 15, 6, 27]
第i=2次比较
[12, 9, 15, 23, 6, 27]
第i=3次比较
[12, 9, 15, 6, 23, 27]
第i=4次比较
[12, 9, 15, 6, 23, 27]
第j=4时外循环情况
第i=0次比较
[9, 12, 15, 6, 23, 27]
第i=1次比较
[9, 12, 15, 6, 23, 27]
第i=2次比较
[9, 12, 6, 15, 23, 27]
第i=3次比较
[9, 12, 6, 15, 23, 27]
第j=3时外循环情况
第i=0次比较
[9, 12, 6, 15, 23, 27]
第i=1次比较
[9, 6, 12, 15, 23, 27]
第i=2次比较
[9, 6, 12, 15, 23, 27]
第j=2时外循环情况
第i=0次比较
[6, 9, 12, 15, 23, 27]
第i=1次比较
[6, 9, 12, 15, 23, 27]
第j=1时外循环情况
第i=0次比较
[6, 9, 12, 15, 23, 27]
第j=0时外循环情况
>>>
本文来自博客园,作者:{Julius},转载请注明原文链接:https://www.cnblogs.com/bestechshare/p/16447902.html
可微信加我,了解更多,WeChat:{KingisOK}