标签:查找 相对 生成 入门 环境 条件过滤 合并 键值 交互
2015.7.31
s=‘hello \‘word\‘
print s
L=[1,2,3]
print L[0]
T=(1,2,3)
print T[0]
D={‘a‘:1,‘b‘:2}
print D[‘a‘]
s = set([(‘Adam‘,95),(‘Lisa‘,85),(‘Bart‘,59)])
for x in s:
print x[0],‘:‘,x[1]
L =[75,92,59,68]
sum =0.0
for score in L:
sum=sum+score
print sum /4
def add(x,y)
return x+y
print ff(4,5)
import math
def move(x, y, step, angle):
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
def fact(n):
if n==1:
return1
return n * fact(n -1)
print fact(20)
def power(x, n=2):
s =1
while n >0:
n = n -1
s = s * x
return s
print power(5)
def average(*args):
sum =0.0
if len(args)==0:
return sum
for x in args:
sum = sum + x
return sum / len(args)
print average()
print average(1,2)
print average(1,2,2,3,4)
L=[1,2,3,4,5,6,7,8,9,0];
print L[x:n]#从第L[x]取到L[n-1]
print L[x:n:y]#从L[x]取到L[n-1]每隔y位取一个
print L[:n]#从起始位取到L[n-1]
#将每个字符串的首字母变为大写
def firstCharUpper(s):
a=s[:1].upper()+s[1:]
return a
print firstCharUpper(‘hello‘)
print firstCharUpper(‘sunday‘)
print firstCharUpper(‘september‘)
L =[‘Adam‘,‘Lisa‘,‘Bart‘,‘Paul‘]
for index,name in enumerate(L):
print index,‘-‘,name
L =[‘Adam‘,‘Lisa‘,‘Bart‘,‘Paul‘]
for index, name in zip(range(1,len(L)+1),L):
print index,‘-‘, name
d ={‘Adam‘:95,‘Lisa‘:85,‘Bart‘:59,‘Paul‘:74}
sum =0.0
for k, v in d.items():
sum = sum + v
print k,‘:‘,v
print‘average‘,‘:‘,sum/4
print[x*(x+1)for x in range(1,100,2)]
d ={‘Adam‘:95,‘Lisa‘:85,‘Bart‘:59}
def generate_tr(name, score):
if score<60:
return‘<tr><td>%s</td><td style="color:red">%s</td></tr>‘%(name, score)
return‘<tr><td>%s</td><td>%s</td></tr>‘%(name, score)
tds =[generate_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>‘
def toUppers(L):
return[x.upper()for x in L if isinstance(x,str)]
print toUppers([‘Hello‘,‘world‘,101])
print[m*100+n*10+x for m in range(1,10)for n in range(10)for x in range(10)if m==x]
标签:查找 相对 生成 入门 环境 条件过滤 合并 键值 交互
原文地址:http://www.cnblogs.com/kinmos/p/6829848.html