python流程控制
python流程控制条件
总结
循环
for循环
脚本简单使用
for典型1+…+100python脚本
数列小细节
迭代遍历
简单操作
内嵌for循环经典-九九乘法表
for循环
常用关键字
猜随机数
while
while与for相比
while循环
打开文件的方法
读取文件的方法
小结
总结
循环
for循环
脚本简单使用
for典型1+…+100python脚本
数列小细节
迭代遍历
简单操作
内嵌for循环经典-九九乘法表
for循环
常用关键字
猜随机数
while
while与for相比
while循环
打开文件的方法
读取文件的方法
小结
python流程控制条件
- if语法
if expression: # 表达式:
statement(s) # 代码块
elif expression:
statement(s)
else exdpression:
statement(s)
- 分数判断
例如
[root@wangtian day02]# cat 2.py
#!/usr/bin/python
score = int(raw_input("Please input num: "))
if score >= 90:
print 'A'
print 'very good'
elif score >= 80:
print 'B'
print 'good'
elif score >= 70:
print 'C'
print 'pass'
else:
print 'game over'
print 'END'
- 操作
效果
[root@wangtian day02]# python 2.py Please input num: 2 game over END [root@wangtian day02]# python 2.py Please input num: 70 C pass END [root@wangtian day02]# python 2.py Please input num: 89 B good END [root@wangtian day02]# python 2.py Please input num: 90 A very good END
- 大写字母与变成小写的方法
例如
[root@wangtian day02]# cat 4.py
#!/usr/bin/python
yn = raw_input("please input [YES/NO]: ")
yn = yn.lower() #用到一个字符串的方法:大写替换成小写。与其相反的是.upper
if yn == 'y' or yn == 'yes':
print "programe is runing…"
elif yn == 'n' or yn == 'no':
print "programe is exit…"
else:
print "please input [YES/NO]: "
- 操作
效果
[root@wangtian day02]# python 4.py please input [YES/NO]: y programe is runing… [root@wangtian day02]# python 4.py please input [YES/NO]: yes programe is runing… [root@wangtian day02]# python 4.py please input [YES/NO]: n programe is exit… [root@wangtian day02]# python 4.py please input [YES/NO]: no programe is exit… [root@wangtian day02]# python 4.py please input [YES/NO]: a please input [YES/NO]:
总结
除了shell之外,1返回的是正确,0返回的是错误。
逻辑值(bool)包含了两个值:True:表示非空的量(比如:string,tuple,list,set,dictonary),所有非零数。Fasle:表示0,None,空的量等。
多条件判断可以用and,or 等
循环
- 循环是一个结构,导致程序要重复一定的次数。
- 条件循环也是如此,当条件变为假,循环结束。
for循环
- for循环:在序列里,使用for循环遍历。
- 语法:
for iterating_var in sequence: statement(s)
例如
In [4]: list1 = [1,2,3,4,5] In [5]: for i in list1: …: print i …: 1 2 3 4 5 In [6]: range(5) Out[6]: [0, 1, 2, 3, 4] In [7]: range(0,10,2) Out[7]: [0, 2, 4, 6, 8] In [8]: range(0,10,3) Out[8]: [0, 3, 6, 9] In [9]: for i in range(10): …: print i …: 0 1 2 3 4 5 6 7 8 9
脚本简单使用
[root@wangtian day02]# cat 5.py
#!/usr/bin/python
for i in range(1,11):
if i % 2 == 0:
print i
[root@wangtian day02]# vim 5.py
[root@wangtian day02]# python 5.py
[2, 4, 6, 8, 10]
[root@wangtian day02]# cat 5.py
#!/usr/bin/python
print [i for i in range(1,11) if i % 2 == 0]
[root@wangtian day02]# vim 5.py
[root@wangtian day02]# python 5.py
[1, 9, 25, 49, 81]
[root@wangtian day02]# cat 5.py
#!/usr/bin/python
print [i**2 for i in range(1,11) if i % 2 != 0]
for典型1+…+100python脚本
#!/usr/bin/python
sum = 0
for i in range(1,101):
sum = sum + i # sum += i
print sum
数列小细节
range 占用内存
xrange 遍历的时候才会占用内存
推荐使用xrange
迭代遍历
- 遍历序列:将序列中各个元素取出来。
-直接从序列取值
-通过索引来取值
简单操作
- 通过循环的方法遍历字典
例如
In [11]: dict.fromkeys('abcde',100) #利用dict.fromkeys方法创建字典
Out[11]: {'a': 100, 'b': 100, 'c': 100, 'd': 100, 'e': 100}
In [12]: dic1 = dict.fromkeys('abcde',100)
In [13]: dic1
Out[13]: {'a': 100, 'b': 100, 'c': 100, 'd': 100, 'e': 100}
In [15]: for k in dic1: #默认的情况下是取key
…: print k
…:
a
c
b
e
d
In [16]: for k in dic1: #通过索引取出value
…: print k, dic1[k]
…:
a 100
c 100
b 100
e 100
d 100
In [20]: for k in dic1: #也可以通过占位符,打印出想要的效果
…: print "%s --> %s" % (k, dic1[k])
…:
a --> 100
c --> 100
b --> 100
e --> 100
d --> 100
In [21]: for k in dic1:
…: print "%s --> %s" % (k, dic1[k]), #用,号可以抑制换行符
…:
a --> 100 c --> 100 b --> 100 e --> 100 d --> 100
In [24]: for k , v in dic1.items():print k ,v #也可以通过itmes的方法取出key,value
a 100
c 100
b 100
e 100
d 100
In [25]: for k, v in dic1.iteritems():print k, v
a 100
c 100
b 100
e 100
d 100
内嵌for循环经典-九九乘法表
- 操作
[root@wangtian day02]# cat 7.py
#!/usr/bin/python
for i in xrange(1,10): #乘法表不需要0,所以从1进行取值
for j in xrange(1,i+1): #(1,3)就会从1,2进行取值,不包括3
print "%sx%s=%s" % (j, i, j*i), #逗号抑制内部循环换行
print #内部循环结束进行换行
输出
[root@wangtian day02]# python 7.py
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
for循环
- for
- else
- for 循环如果正常结束,才会执行else语句。
常用关键字
break continue exit pass
操作
例如
[root@wangtian day02]# cat 8.py
#!/usr/bin/python
import time
import sys
for i in xrange(10):
if i == 5:
continue
elif i == 6:
pass
elif i < 9:
time.sleep(1)
print i
elif i == 9:
sys.exit(10)
else:
print "EMD"
print "hahaha"
猜随机数
[root@wangtian day02]# cat 10.py
#!/usr/bin/env python
import random
right = random.randint(1,20)
count = 0
while count < 6:
num = input('please input a number:')
if num == right:
print 'you are right'
break
else:
if num > right:
print "binger than right"
else:
print "samller than right"
count += 1
while
while与for相比
- for循环用在有次数的循环上。
-
while循环用在有条件的控制上。
while循环
- while循环,直到表达式变为假,才推出while循环,表达式是一个逻辑表达式,必须返回一个True或False。
- 语法
while expression:
statement(s)
### 操作
例1
[root@wangtian day02]# cat 11.py
#!/usr/bin/python
n = 0
while True:
if n == 10:
break
print n , 'hello'
n += 1
输出
[root@wangtian day02]# python 11.py 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9 hello
例2
[root@wangtian day02]# cat 12.py
#!/usr/bin/python
while True:
string = raw_input('please input string: ')
if string == "q":
break
输出
[root@wangtian day02]# python 12.py please input string: e please input string: w please input string: q
例3
[root@wangtian day02]# cat 13.py
#!/usr/bin/python
x = ''
while x != 'q':
x = raw_input('please input: ' )
输出
[root@wangtian day02]# python 13.py please input: e please input: w please input: q
选择了奋斗,以后可以随时还有选择安逸的权力。
但选择了安逸,可能以后就不那么轻易还能有选择奋斗的权力。

浙公网安备 33010602011771号