L = ['Adam', 'Lisa', 'Bart', 'Paul']
for index, name in enumerate(L,start=1):
print index, '-', name
for index, name in enumerate(L,start=1):
print index, '-', name
def average(*args):
s = 0
i = 0
for arg in args:
i=i+1.0
s += arg
return s/i
print average(0)
print average(1, 2)
print average(1, 2, 2, 3, 4)
s = 0
i = 0
for arg in args:
i=i+1.0
s += arg
return s/i
print average(0)
print average(1, 2)
print average(1, 2, 2, 3, 4)
2019-01-14
def greet(s='world.'):
print 'Hello,',s
greet()
greet('Bart.')
print 'Hello,',s
greet()
greet('Bart.')
2019-01-14
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)
2019-01-14
def square_of_sum(L):
s = 0;
for v in L:
s = s+pow(v,2)
return s
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
s = 0;
for v in L:
s = s+pow(v,2)
return s
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2019-01-14
import math
def quadratic_equation(a, b, c):
if (b*b-(4*a*c))>=0:
x1 = (-b+math.sqrt(b*b-(4*a*c)))/(2*a)
x2 = (-b-math.sqrt(b*b-(4*a*c)))/(2*a)
return x1, x2
else:
return
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
# math.sqrt
def quadratic_equation(a, b, c):
if (b*b-(4*a*c))>=0:
x1 = (-b+math.sqrt(b*b-(4*a*c)))/(2*a)
x2 = (-b-math.sqrt(b*b-(4*a*c)))/(2*a)
return x1, x2
else:
return
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
# math.sqrt
2019-01-14
L = []
i = 0
j = 0;
while j<100:
i= i+1
j= j+1
L.append(i*j)
print sum(L)
i = 0
j = 0;
while j<100:
i= i+1
j= j+1
L.append(i*j)
print sum(L)
2019-01-14
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for v in L:
if v in s:
s.remove(v)
else:
s.add(v)
print s
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for v in L:
if v in s:
s.remove(v)
else:
s.add(v)
print s
2019-01-14
for x in [1,2,3,4,5,6,7,8,9,]:
for y in [0,1,2,3,4,5,6,7,8,9]:
if(x<y):
print x*10+y
for y in [0,1,2,3,4,5,6,7,8,9]:
if(x<y):
print x*10+y
2019-01-13
sum = 0
x = 1
n = 1
while True:
if n<=20:
x = pow(2,20);
sum = (1-x)/(1-2);
else:
break
n = n+1
print sum
x = 1
n = 1
while True:
if n<=20:
x = pow(2,20);
sum = (1-x)/(1-2);
else:
break
n = n+1
print sum
2019-01-13
# 按照题目逻辑进行编写
def move(n, a, b, c):
if n == 1:
print a,"-->",c
else:
move(n-1, a, c, b)
move(1, a, b, c)
move(n-1, b, a, c)
move(4, 'A', 'B', 'C')
def move(n, a, b, c):
if n == 1:
print a,"-->",c
else:
move(n-1, a, c, b)
move(1, a, b, c)
move(n-1, b, a, c)
move(4, 'A', 'B', 'C')
2019-01-13
while是和for不同的一种循环,while循环不会迭代list或tuple的元素,而是根据表达式循环是否结束。while循坏每次要先判断whileX<N,如果为T则循坏,否则退出,在循环内,X=X+1,会让X不断增加,最终因为X<N不成立而退出
2019-01-13
依次取出list,tuple中的元素用for循环,for name in X,name这个变量是在for循环中定义的,意思是依次取出其中的每一个元素,把元素赋值给name,然后执行for循环体,也就是缩进的代码块
2019-01-13