d = {
95: 'Adam',
85: 'Lisa',
59: 'Bart'
}
if not d.get(72):
d[72] = 'Paul'
print d
95: 'Adam',
85: 'Lisa',
59: 'Bart'
}
if not d.get(72):
d[72] = 'Paul'
print d
2018-11-07
for x in [1,2,3,4,5,6,7,8,9]:
for y in [1,2,3,4,5,6,7,8,9]:
if x < y:
print x*10+y
if x*10+y > 100:
break
for y in [1,2,3,4,5,6,7,8,9]:
if x < y:
print x*10+y
if x*10+y > 100:
break
2018-11-07
t = ('a', 'b', ('A', 'B'))
print t
print t
2018-11-07
a = 'Adam'
b = 'Lisa'
c = 'Bart'
L = [a,95.5,b,85,c,59]
print L
b = 'Lisa'
c = 'Bart'
L = [a,95.5,b,85,c,59]
print L
2018-11-07
print [100*m+10*n+m for m in range (1,10) for n in range (0,10) ], 我这样是不是更简单?因为第一位和第三位本来就是一个数,所以没有必要用三个变量啊。
2018-11-07
print [int(x+y+x) for x in '123456789' for y in '0123456789' for z in '0123456789' if x==z]
2018-11-07
L = [str(x) for x in range(100,1000)]
print [int(l) for l in L if l[0]==l[2]]
print [int(l) for l in L if l[0]==l[2]]
2018-11-06
def average(*args):
if args:
s = sum([i for i in args])
return s * 1.0 / len(args)
else:
return 0.0
if args:
s = sum([i for i in args])
return s * 1.0 / len(args)
else:
return 0.0
2018-11-06
def greet(a='world'):
print 'Hello, %s.' % (a)
print 'Hello, %s.' % (a)
2018-11-06
上条运行结果错误了
import math
def quadratic_equation(a, b, c):
if b**2 - 4 * a * c >=0:
t = math.sqrt(b**2 - 4 * a * c)
return (-b + t)/(2.0 * a), (-b - t)/(2.0 * a)
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
import math
def quadratic_equation(a, b, c):
if b**2 - 4 * a * c >=0:
t = math.sqrt(b**2 - 4 * a * c)
return (-b + t)/(2.0 * a), (-b - t)/(2.0 * a)
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2018-11-06
import math
def quadratic_equation(a, b, c):
t = math.sqrt(math.fabs(b**2 - 4 * a * c))
return (-b + t)/2.0 * a, (-b - t)/2.0 * a
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
t = math.sqrt(math.fabs(b**2 - 4 * a * c))
return (-b + t)/2.0 * a, (-b - t)/2.0 * a
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2018-11-06
def square_of_sum(L):
return sum([i**2 for i in L])
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
return sum([i**2 for i in L])
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2018-11-06