In [3]:
###写函数,返回一个list中所有数字的和


def sum():

    total=0
    for i in numbers:
        total += i
    return total
    
numbers =[2,4,6,7,3,54]
print(sum())


76

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()))


plz enter the number of numbers3
plz enter a number2
plz enter a number6
plz enter a number8
2

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))


6

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))


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-41-1ec748575f9d> in <module>()
     11 xl1 = [1,2]
     12 xl2 = [2,1]
---> 13 print(cos_angle(xl1,xl2))

<ipython-input-41-1ec748575f9d> in cos_angle(xl1, xl2)
      5 
      6 def cos_angle(xl1,xl2):
----> 7     multi_of_mo = (math.sqrt(xl1[1]**2+xl1[2]**2))*(math.sqrt(xl2[2])**2+xl2[1]**2)
      8     cos = (xl1[1]*xl2(2)+xl1[2]*xl2[1])/multi_of_mo
      9     return cos

IndexError: list index out of range

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))


1+2+...+ 1000 = 500500

In [26]:
i = 0
while i < 9:
    numbers.append(i)
    print(numbers)
    i += 1


[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]

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():


  File "<ipython-input-10-92fe8c570ca3>", line 5
    return min
         ^
IndentationError: expected an indented block

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [5]:
###写函数,返回一个list中的最小值
numbers = [4,7,5,8,20,34,64,254,1,6]

print(max(numbers))


254

In [ ]:


In [4]:



169 


In [ ]:


In [ ]: