L = range(1, 101)
print L[0:10]
print L[2:100:3]
print L[4:50:5]
#起始索引,结束索引,步长
print L[0:10]
print L[2:100:3]
print L[4:50:5]
#起始索引,结束索引,步长
2015-07-23
import math
def quadratic_equation(a, b, c):
y = math.sqrt(b * b - 4 * a * c)
if y>=0:
x1 = (-b + y) / (2 * a)
x2 = (-b - y) / (2 * a)
else:
print 'error'
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
#数学是硬伤、、、、已经忘了!!!
def quadratic_equation(a, b, c):
y = math.sqrt(b * b - 4 * a * c)
if y>=0:
x1 = (-b + y) / (2 * a)
x2 = (-b - y) / (2 * a)
else:
print 'error'
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
#数学是硬伤、、、、已经忘了!!!
2015-07-23
def square_of_sum(L):
sum=0
for i in L:
sum+=i**2
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
sum=0
for i in L:
sum+=i**2
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-07-23
s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
print x[0]+':'+str(x[1])
for x in s:
print x[0]+':'+str(x[1])
2015-07-23
s = set(['adam', 'lisa', 'bart', 'paul'])
print 'adam' in s
print 'bart' in s
print 'adam' in s
print 'bart' in s
2015-07-23
sum = 0
n = 0
while True:
sum+=2**(n-1)
n=n+1
if n>20:
break
print sum
n = 0
while True:
sum+=2**(n-1)
n=n+1
if n>20:
break
print sum
2015-07-23
Python把0、空字符串''和None看成 False,其他数值和非空字符串都看成
所以a=>True b=>False
所以a=>True b=>False
2015-07-23
def square_of_sum(L):
sum = 0
for key in L:
sum = sum + key * key
return sum
sum = 0
for key in L:
sum = sum + key * key
return sum
2015-07-23
def square_of_sum(L):
sum=0
for x in L:
a=0
a=x*x
sum=sum+a
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
sum=0
for x in L:
a=0
a=x*x
sum=sum+a
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-07-22