In [3]:
###写函数,返回一个list中所有数字的和
def sum():
total=0
for i in numbers:
total += i
return total
numbers =[2,4,6,7,3,54]
print(sum())
In [10]:
###写函数,返回一个list中的最小值
def creat_list():
n = int(input('plz enter the number of numbers'))
numbers = []
i = 0
while i<n:
number = int(input('plz enter a number'))
numbers.append(number)
i += 1
return numbers
def min_in_list(numbers):
min = numbers [0]
for i in numbers:
if min > i:
min = i
return min
print(min_in_list(creat_list()))
In [23]:
#写函数,返回某个元素/对象在一个list中的位置,如果不在,则返回-1.
def in_list(n,numbers):
i = 0
for number in numbers:
if number == n:
return i
else:
i+=1
numbers = [1,4,6,45,564,34,3]
print(in_list(3,numbers))
In [41]:
#写函数,可求两个向量的夹角余弦值,向量可放在list中。主程序调用该函数
#????????
import math
def cos_angle(xl1,xl2):
multi_of_mo = (math.sqrt(xl1[1]**2+xl1[2]**2))*(math.sqrt(xl2[2])**2+xl2[1]**2)
cos = (xl1[1]*xl2(2)+xl1[2]*xl2[1])/multi_of_mo
return cos
xl1 = [1,2]
xl2 = [2,1]
print(cos_angle(xl1,xl2))
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [24]:
def my_abs(number):
if number < 0:
number = -number
return number
def compute_sum(end):
i = 0
total_n = 0
number = my_abs(end)
while i < number:
i = i + 1
total_n = total_n + i
return total_n
# 主程序
i=1000
print('1+2+...+', i, '=',compute_sum(i))
In [26]:
i = 0
while i < 9:
numbers.append(i)
print(numbers)
i += 1
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [10]:
###写函数,返回一个list中的最小值
def min():
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [5]:
###写函数,返回一个list中的最小值
numbers = [4,7,5,8,20,34,64,254,1,6]
print(max(numbers))
In [ ]:
In [4]:
In [ ]:
In [ ]: