# encoding:utf-8
s=range(1, 101)
print s[:10]
print s[2::3]
print s[4:50:5]
s=range(1, 101)
print s[:10]
print s[2::3]
print s[4:50:5]
2018-05-16
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print key+':'+str(d[key])
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print key+':'+str(d[key])
2018-05-16
def generate_tr(name, score):
if score>=60:
return '<tr><td>%s</td><td>%s</td></tr>' % (name, score)
else:
return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)
tds = [generate_tr(name, score) for name, score in d.iteritems()]
if score>=60:
return '<tr><td>%s</td><td>%s</td></tr>' % (name, score)
else:
return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)
tds = [generate_tr(name, score) for name, score in d.iteritems()]
2018-05-16
def average(*args):
if len(args)==0:
return 0.0
m=0.0
n=0.0
for i in args:
m=m+i
n=n+1
return (m/n)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
if len(args)==0:
return 0.0
m=0.0
n=0.0
for i in args:
m=m+i
n=n+1
return (m/n)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2018-05-16
def greet(x='world'):
print 'hello,',x,'.' #最后面还有个点
greet()
greet('Bart')
print 'hello,',x,'.' #最后面还有个点
greet()
greet('Bart')
2018-05-16
如果要表示多行字符串,可以用'''...'''表示:(如果前面不加r,里面有单引号或者双引号会报错)
2018-05-16
sum = 0
x = 1
n = 1
while True:
sum = x + sum
x = 2**n
n = n + 1
if n > 20:
break
print sum
1048575
x = 1
n = 1
while True:
sum = x + sum
x = 2**n
n = n + 1
if n > 20:
break
print sum
1048575
2018-05-15
L = ['Adam', 'Lisa', 'Bart']
L[-3]='Bart'
L[-1]='Adam'
print L
L[-3]='Bart'
L[-1]='Adam'
print L
2018-05-15
L = ['Adam', 'Lisa', 'Paul', 'Bart']
L.pop(-2)
L.pop(-1)
print L
L.pop(-2)
L.pop(-1)
print L
2018-05-15
L = ['Adam', 'Lisa', 'Paul', 'Bart']
L.pop(2)
L.pop()
print L
L.pop(2)
L.pop()
print L
2018-05-15
L = ['Adam',95.5,'Lisa',85,'Bart',59]
print L
print L
2018-05-15
老师给的思路很对,move这个函数实际上就是把第二个值(开始时为'a')移到第四个值(开始时为'c'):于是代码为:def move(n, a, b, c):
if n != 1:
move(n-1,a,c,b)
move(1,a,b,c)
move(n-1,b,a,c)
else:
print a+'-->'+c
move(4, 'a', 'b', 'c')
if n != 1:
move(n-1,a,c,b)
move(1,a,b,c)
move(n-1,b,a,c)
else:
print a+'-->'+c
move(4, 'a', 'b', 'c')
2018-05-15