a是非空字符串,布尔运算时python将其当做true,所以第一行结果返回的是a,而a是字符串,所以最终结果就是输出字符串的内容。
b是空字符串,布尔运算时将其当做false,所以第二行的结果取决于后面的字符串,于是返回的就是后面字符串的内容。
b是空字符串,布尔运算时将其当做false,所以第二行的结果取决于后面的字符串,于是返回的就是后面字符串的内容。
2015-08-15
来个加判断的,当复习
print [x + y*10 + z*100 for x in range(0,10) for y in range(0,10) for z in range(1,10) if x == z]
print [x + y*10 + z*100 for x in range(0,10) for y in range(0,10) for z in range(1,10) if x == z]
2015-08-15
x1 = 1
d = 3
n = 100
x100 = x1 + (n-1)*3
s = (x1 + x100)*n/2
print s
d = 3
n = 100
x100 = x1 + (n-1)*3
s = (x1 + x100)*n/2
print s
2015-08-15
import math
def quadratic_equation(a, b, c):
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):
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-08-15
def square_of_sum(L):
sum = 0
for elem in L:
sum += elem * elem
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
sum = 0
for elem in L:
sum += elem * elem
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-08-15
sum = 0
x = 1
while x <= 100:
sum += x
x = x+1
sum = sum/2
print sum
x = 1
while x <= 100:
sum += x
x = x+1
sum = sum/2
print sum
2015-08-15
L = []
i = 1
while i <= 100:
L.append(i * i)
i += 1
print sum(L)
i = 1
while i <= 100:
L.append(i * i)
i += 1
print sum(L)
2015-08-15
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for elem in L:
if elem in s:
s.remove(elem)
else:
s.add(elem)
print s
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for elem in L:
if elem in s:
s.remove(elem)
else:
s.add(elem)
print s
2015-08-15
用用之前的方法
L = ['Adam', 'Lisa', 'Bart']
L.insert(0,L.pop())
L.insert(1,L.pop())
print L
L = ['Adam', 'Lisa', 'Bart']
L.insert(0,L.pop())
L.insert(1,L.pop())
print L
2015-08-15
for x in range(1,10):
for y in range(10):
if x < y:
print x*10+y
for y in range(10):
if x < y:
print x*10+y
2015-08-15