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 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]
#!/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
[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
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 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
r: 以读方式打开
w: 以写方式打开
a: 以追加模式打开
r+: 以读写模式打开
w+: 以读写模式打开
a+: 以读写模式打开
rb: 以二进制读模式打开
wb: 以二进制写模式打开
ab: 以二进制追加模式打开
rb+: 以二进制读写模式打开
wb+: 以二进制读写模式打开
ab+: 以二进制读写模式打开
In [31]: fd.
fd.close fd.errors fd.isatty fd.newlines fd.readinto fd.seek fd.truncate fd.xreadlines
fd.closed fd.fileno fd.mode fd.next fd.readline fd.softspace fd.write
fd.encoding fd.flush fd.name fd.read fd.readlines fd.tell fd.writelines
In [30]: fd = open(‘/tmp/tmp.txt‘)
In [31]: fd.read()
Out[31]: ‘1\n2\n3\n‘
In [32]: fd = open(‘/tmp/tmp.txt‘)
In [33]: fd.readline()
Out[33]: ‘1\n‘
In [34]: fd.readline()
Out[34]: ‘2\n‘
In [35]: fd.readline()
Out[35]: ‘3\n‘
In [36]: fd.readline()
Out[36]: ‘‘
In [37]: fd = open(‘/tmp/tmp.txt‘)
In [38]: fd.readlines()
Out[38]: [‘1\n‘, ‘2\n‘, ‘3\n‘]
In [39]: fd.readlines()
Out[39]: []
In [40]: fd = open(‘/tmp/tmp.txt‘)
In [41]: fd.next()
Out[41]: ‘1\n‘
In [42]: fd.next()
Out[42]: ‘2\n‘
In [43]: fd.next()
Out[43]: ‘3\n‘
In [44]: fd.next()
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-44-3df4eef70a28> in <module>()
----> 1 fd.next()
fd.read():返回的是字符串
fd.readline():返回的是每一行字符串
fd.readlines():返回的是一行列表
[root@wangtian day02]# cat /tmp/tmp.txt
1
2
3
[root@wangtian day02]# cat 14.py
#!/usr/bin/python
fd = open(‘/tmp/tmp.txt‘)
for line in fd.readlines():
print line,
[root@wangtian day02]# python 14.py
1
2
3
[root@wangtian day02]# cat 14.py > 15.py
[root@wangtian day02]# vi 15.py
[root@wangtian day02]# cat 15.py
#!/usr/bin/python
fd = open(‘/tmp/tmp.txt‘)
for line in fd:
print line,
[root@wangtian day02]# python 15.py
1
2
3
print line后面加个逗号,可以抑制print默认的换行。
for line in fd.readlines(): 这种方法会全部加在到内存中,不建议使用
for line in fd: 这种方法类似于fd.next(),没循环一次加在一行,推荐使用。
打开文件的时候要使用w方法会覆盖原来的文件,谨慎使用
read(),readline()readlins()的区别是字符串和列表
使用方法的时候选择最小消耗资源的方式
[root@wangtian day02]# cat 16.py
#!/usr/bin/python
fd = open(‘/tmp/tmp.txt‘)
while True:
line = fd.readline()
if not line:
break
print line,
fd.close
[root@wangtian day02]# python 16.py
1
2
3
[root@wangtian day02]# cat 17.py
#!/usr/bin/python
with open(‘/tmp/tmp.txt‘) as fd:
while True:
line = fd.readline()
if not line:
break
print line,
[root@wangtian day02]# python 17.py
1
2
3
for循环有一定的次数
while循环需要给出条件,条件语句后面要加:
for和while遍历完文件之后需要加fd.close关闭文件,养成好习惯,如果不加python执行完系统也会进行回收
with+while进行循环就不用加close的方法
原文:http://www.cnblogs.com/wtli/p/7689867.html