def greet(char='world'):
print 'Hello,',char,'.'
greet()
greet('Bart')
print 'Hello,',char,'.'
greet()
greet('Bart')
2018-12-29
def move(n, a, b, c):
if n == 1:
print a, '-->', c
return
move(n-1, a, c, b)
move(1, a, b, c)
move(n-1, b, a, c)
move(4, 'a', 'b', 'c')
if n == 1:
print a, '-->', c
return
move(n-1, a, c, b)
move(1, a, b, c)
move(n-1, b, a, c)
move(4, 'a', 'b', 'c')
2018-12-29
import math
def quadratic_equation(a, b, c):
delta = b**2 - 4*a*c
if delta < 0:
return None
elif delta == 0:
return -b/2*a
return (-b+math.sqrt(delta))/(2*a), (-b-math.sqrt(delta))/(2*a)
print(quadratic_equation(2, 3, 0))
print(quadratic_equation(1, -6, 5))
def quadratic_equation(a, b, c):
delta = b**2 - 4*a*c
if delta < 0:
return None
elif delta == 0:
return -b/2*a
return (-b+math.sqrt(delta))/(2*a), (-b-math.sqrt(delta))/(2*a)
print(quadratic_equation(2, 3, 0))
print(quadratic_equation(1, -6, 5))
2018-12-29
def square_of_sum(L):
arr = []
for item in L:
arr.append(item*item)
return sum(arr)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
arr = []
for item in L:
arr.append(item*item)
return sum(arr)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2018-12-29
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for item in L:
if item in s:
s.remove(item)
else:
s.add(item)
print s
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for item in L:
if item in s:
s.remove(item)
else:
s.add(item)
print s
2018-12-29
s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
print x[0],':',x[1]
for x in s:
print x[0],':',x[1]
2018-12-29
months = set(['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct','Nov','Dec'])
x1 = 'Feb'
x2 = 'Sun'
if x1 in months:
print 'x1: ok'
else:
print 'x1: error'
if x2 in months:
print 'x2: ok'
else:
print 'x2: error'
x1 = 'Feb'
x2 = 'Sun'
if x1 in months:
print 'x1: ok'
else:
print 'x1: error'
if x2 in months:
print 'x2: ok'
else:
print 'x2: error'
2018-12-29
s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
print s
print s
2018-12-29
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for (k,v) in d.iteritems():
print k,':',v
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for (k,v) in d.iteritems():
print k,':',v
2018-12-29
答案中给出的print key+':',d[key]应该改成print key,':',d[key]。
2018-12-29
最赞回答 / 摩卡星冰乐
( )替换成[ ],是指在初始化建立一个tuple的时候用( ),而初始化建立List是用[ ]。对于两者的访问都是用[ ],只不过list可以修改,tuple不支持修改,只能访问。
2018-12-29
for x in range(1,10):
for y in range( x+1 , 10 ):
print x*10+y,'\r'
for y in range( x+1 , 10 ):
print x*10+y,'\r'
2018-12-29
sum = 0
x = 0
while True:
x = x + 1
if x > 100:
break
if x%2==0:
continue
sum+=x
print sum
x = 0
while True:
x = x + 1
if x > 100:
break
if x%2==0:
continue
sum+=x
print sum
2018-12-29
sum = 0
x = 1
n = 1
while True:
sum+=x
x*=2
n+=1
if n>20:
break
print sum
x = 1
n = 1
while True:
sum+=x
x*=2
n+=1
if n>20:
break
print sum
2018-12-29