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/len(d)
sum = 0.0
for k, v in d.items():
sum = sum + v
print k,':',v
print 'average', ':', sum/len(d)
2015-09-25
def firstCharUpper(s):
return s[0].upper()+s[1:]
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
return s[0].upper()+s[1:]
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
2015-09-25
def greet(x='world'):
print 'Hello,',x,'.'
greet()
greet('Bart')
print 'Hello,',x,'.'
greet()
greet('Bart')
2015-09-25
import math
def quadratic_equation(a, b, c):
if(b*b-4*a*c<0):
return 'Error'
else:
return (-b+math.sqrt(b*b-4*a*c))/(2*a),(-b-math.sqrt(b*b-4*a*c))/(2*a)
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
if(b*b-4*a*c<0):
return 'Error'
else:
return (-b+math.sqrt(b*b-4*a*c))/(2*a),(-b-math.sqrt(b*b-4*a*c))/(2*a)
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2015-09-25
大概是这样:n个盘要从A移到C,应为要遵循那个算法,所以要将(n-1)个移动到B第n-1个要移动到C,遵循算法,(n-2)个就要移动到A,第n-2个移动到C,(n-3)个就要移动到B,这样就绕回来了.....真是绕啊!
2015-09-25
for x in range(1,10):
for y in range(1,10):
if x < y:
z=x*10+y
print z
for y in range(1,10):
if x < y:
z=x*10+y
print z
2015-09-25
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for index, name in enumerate(L):
print index + 1, '-', name
for index, name in enumerate(L):
print index + 1, '-', name
sum = 0
x = 0
while True:
x = x + 1
if x > 100:
break
if (x%2== 0):
continue
sum = sum + x
print sum
x = 0
while True:
x = x + 1
if x > 100:
break
if (x%2== 0):
continue
sum = sum + x
print sum
2015-09-25
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for x in L:
if x in s:
s.remove(x)
else:
s.add(x)
print s
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for x in L:
if x in s:
s.remove(x)
else:
s.add(x)
print s
2015-09-25
s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
print list(x)[0],':',list(x)[1]
for x in s:
print list(x)[0],':',list(x)[1]
2015-09-25
sum = 0
x = 1
n = 1
while True:
sum=sum+x
x=2**n
n=n+1
if n > 20:
break
print sum
x = 1
n = 1
while True:
sum=sum+x
x=2**n
n=n+1
if n > 20:
break
print sum
2015-09-25