编码格式
数据类型 1
2
3
4
5
6
7
8
9
10
11
12
13
print(45678 +0x12fd2 )
print('Learn Python in imooc' )
print(100 < 99 )
print(0xff == 255 )
x1 = 1
d = 3
n = 100
x100 = 298
s = 50 *(x100+1 )
print s
Print 1
2
3
4
5
6
7
8
9
10
11
print 'The quick brown fox' ,'jumps over' ,'the lazy dog'
print (100 + 200 )
print "HELLO," .lower()+"python." .upper()
print 'Bob said \"I\'m OK\".'
print r'''"To be, or not to be": that is the question.
Whether it's nobler in the mind to suffer.'''
逻辑运算 1
2
3
4
5
6
7
print r'''1. 在计算 a and b 时,如果 a 是 False,则根据与运算法则,整个结果必定为 False,因此返回 a;
如果 a 是 True,则整个计算结果必定取决与 b,因此返回 b。
2. 在计算 a or b 时,如果 a 是 True,则根据或运算法则,整个计算结果必定为 True,因此返回 a;
如果 a 是 False,则整个计算结果必定取决于 b,因此返回 b。
'''
List 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
L = ['Adam' , 95.5 , 'Lisa' , 85 , 'Bart' , 59 ]
print L
print L[2 ]
print L[-2 ]
L.append('Paul' )
L.insert(0 , 'PaP' )
print L
L.pop()
L.pop(0 )
print L
L[0 ] = 'Beyond'
print L
Tuple 1
2
3
4
5
6
7
8
9
10
11
12
13
t = ('Adam' , 'Lisa' , 'Bart' )
print t
k = ('king' ,)
print k
t = ('a' , 'b' , ['A' , 'B' ])
L = t[2 ]
L[0 ] = 'X'
L[1 ] = 'Y'
print t
判断语句 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
age = 20
if age >= 18 :
print 'your age is' , age
print 'adult'
print 'END'
if not age >= 18 :
print 'teenager'
else :
print 'adult'
if age >= 18 :
print 'adult'
elif age >= 6 :
print 'teenager'
elif age >= 3 :
print 'kid'
else :
print 'baby'
循环语句 1
2
3
4
5
6
7
8
9
10
11
12
13
L = ['Adam' , 'Lisa' , 'Bart' ]
for name in L:
print name
N = 10
x = 0
while x < N:
print x
x = x + 1
if x == 6 :
break
Dict 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
d = {
'Adam' : 95 ,
'Lisa' : 85 ,
'Bart' : 59
}
print 'd的长度为' ,len(d)
if 'Paul' in d:
print d['Paul' ]
print 'Bart:' , d['Bart' ]
d['Paul' ] = 72
print d
for x in d:
print x+':' ,d[x]
Set 1
2
3
4
5
6
7
8
9
10
11
12
13
14
s = set(['A' , 'B' , 'C' , 'C' ])
print s
set(['A' , 'C' , 'B' ])
len(s)
print 'C' in s
s = set(['Adam' , 'Lisa' , 'Bart' ])
s.remove('Lisa' )
s.add('Beyond' )
for name in s:
print name
函数 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
print abs(-11 )
print cmp(3 ,9 )
print int(12.31 )
print str(101 )
def my_abs (x) :
if x >= 0 :
return x
else :
return -x
import math
def move (x, y, step=0 , angle=0 ) :
nx = x + step * math.cos(angle)
ny = y - step * math.sin(angle)
return nx, ny
x, y = move(100 , 100 , 60 , math.pi / 6 )
print x, y
r = move(100 , 100 , 60 , math.pi / 6 )
print r
def fn (*args) :
print args
迭代 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
for i in range(1 ,100 ):
if i%7 ==0 :
print i
L = ['Adam' , 'Lisa' , 'Bart' , 'Paul' ]
for a, b in enumerate(L):
print a, '-' , b
d = { 'Adam' : 95 , 'Lisa' : 85 , 'Bart' : 59 }
for v in d.values():
print v
for v in d.itervalues():
print v
列表生成式
print [x * x for x in range(1 , 11 ) if x % 2 == 0 ]
print [m + n for m in 'ABC' for n in '123' ]
tds = ['<tr><td>%s</td><td>%s</td></tr>' % (name, score) for name, score in d.iteritems()]
print '<table border="1">'
print '<tr><th>Name</th><th>Score</th><tr>'
print '\n' .join(tds)
print '</table>'