L = range(1, 101)
print L[:10]
print L[2::3]
print L[4:50:5]#从索引4开始,每个5位取一个,取到索引50结束,50索引不包含
print L[:10]
print L[2::3]
print L[4:50:5]#从索引4开始,每个5位取一个,取到索引50结束,50索引不包含
2018-07-06
def average(*args):
sum = 0.0
aver=0.0
if len(args) == 0:
return sum
else:
for x in args:
sum += x
aver = sum/len(args)
return aver
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
注意sum和aver是浮点数
sum = 0.0
aver=0.0
if len(args) == 0:
return sum
else:
for x in args:
sum += x
aver = sum/len(args)
return aver
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
注意sum和aver是浮点数
2018-07-06
def greet(a='world'):
print 'Hello,',a,'.'
greet()
greet('Bart')
print 'Hello,',a,'.'
greet()
greet('Bart')
2018-07-06
已经有注释 # -*- coding: utf-8 -*- ,那么就不需要在u''' ''',直接''' '''
2018-07-06
既然是tuple的指向不变,因此在确保对象类型不变的情况下,正确的答案应该是:t = ('a','b',[('A',),('B',)])
个人见解o(* ̄︶ ̄*)o
个人见解o(* ̄︶ ̄*)o
2018-07-05
n =b**2-4*a*c
if n < 0:
print('n')
elif n == 0:
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
else:
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
print('d')
return x1,x2
if n < 0:
print('n')
elif n == 0:
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
else:
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
print('d')
return x1,x2
2018-07-05
import math
def quadratic_equation(a, b, c):
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
注意2*a一定要用括号给括起来
def quadratic_equation(a, b, c):
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
注意2*a一定要用括号给括起来
2018-07-05
def square_of_sum(L):
sum = 0
for n in L:
sum +=n**2
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
这里用到for in语句
sum = 0
for n in L:
sum +=n**2
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
这里用到for in语句
2018-07-05
L = []
x=0
while x < 100:
x +=1
L.append(x*x)
print sum(L)
开始讲while语句写成了if,一直不通过
x=0
while x < 100:
x +=1
L.append(x*x)
print sum(L)
开始讲while语句写成了if,一直不通过
2018-07-05
L = []
x=0
n=0
while x < 100:
x +=1
n=x*x
L.append(n)
print sum(L)
x=0
n=0
while x < 100:
x +=1
n=x*x
L.append(n)
print sum(L)
2018-07-05
L = []
n = 0
sum = 0
for i in range(100):
n=n+1
sum=sum+n**2
L=sum
print(L)
n = 0
sum = 0
for i in range(100):
n=n+1
sum=sum+n**2
L=sum
print(L)
2018-07-04
感觉给的标准答案是错的提供的标准代码在慕课运行是可以的,但是在其他环境就报错,我写的代码是这样的:
def average(*args):
sum = 0
n= 0
if len(args) == 0:
return sum
else:
for i in args:
sum =sum +i
return sum/len(args)
print (average())
print (average(1, 2))
print (average(1, 2, 2, 3, 4))
def average(*args):
sum = 0
n= 0
if len(args) == 0:
return sum
else:
for i in args:
sum =sum +i
return sum/len(args)
print (average())
print (average(1, 2))
print (average(1, 2, 2, 3, 4))
2018-07-04