• 倒序打印用户输入的n个单词。

In [5]:
words=[]
i=0
n=int(input('please enter an integer as the number of the word you want to enter. '))

while i < n :
    words.append(input('please enter a word. '))
    i+=1

for i in range (n-1,n-11,-1):
    print(words[i])


please enter an integer as the number of the word you want to enter. 11
please enter a word. one
please enter a word. two
please enter a word. three
please enter a word. four
please enter a word. five
please enter a word. six
please enter a word. seven
please enter a word. eight
please enter a word. nine
please enter a word. ten
please enter a word. eleven
eleven
ten
nine
eight
seven
six
five
four
three
two
  • 蒙特卡洛方法求π的值

In [11]:
import random

def mentekaluo_pi (n):
    inside_number=0
    for i in range (n):
        x,y=random.random(),random.random()
        if x*x+y*y<=1 :
            inside_number+=1
    return 4*inside_number/n

n=int(input('please enter times. '))
pi=mentekaluo_pi(n)
print(pi)


please enter times. 50000
3.14504

In [14]:
#9*9 乘法表
for i in range(1, 10):
    j=1
    while j <= i :
        print('|{}x{}={:2}| '.format(i, j, i*j), end = '')
        j+=1
    print()
    print('---------------------------------------------------------------------------------------------------------------------------')


|1x1= 1| 
---------------------------------------------------------------------------------------------------------------------------
|2x1= 2| |2x2= 4| 
---------------------------------------------------------------------------------------------------------------------------
|3x1= 3| |3x2= 6| |3x3= 9| 
---------------------------------------------------------------------------------------------------------------------------
|4x1= 4| |4x2= 8| |4x3=12| |4x4=16| 
---------------------------------------------------------------------------------------------------------------------------
|5x1= 5| |5x2=10| |5x3=15| |5x4=20| |5x5=25| 
---------------------------------------------------------------------------------------------------------------------------
|6x1= 6| |6x2=12| |6x3=18| |6x4=24| |6x5=30| |6x6=36| 
---------------------------------------------------------------------------------------------------------------------------
|7x1= 7| |7x2=14| |7x3=21| |7x4=28| |7x5=35| |7x6=42| |7x7=49| 
---------------------------------------------------------------------------------------------------------------------------
|8x1= 8| |8x2=16| |8x3=24| |8x4=32| |8x5=40| |8x6=48| |8x7=56| |8x8=64| 
---------------------------------------------------------------------------------------------------------------------------
|9x1= 9| |9x2=18| |9x3=27| |9x4=36| |9x5=45| |9x6=54| |9x7=63| |9x8=72| |9x9=81| 
---------------------------------------------------------------------------------------------------------------------------
  • 将前面几章用while循环的习题,用for循环实现,并尽量写成函数。
  • 1.可求m!+n!+k!
  • 2.写函数可返回1 - 1/3 + 1/5 - 1/7...的前n项的和。在主程序中,分别令n=1000及100000,打印4倍该函数的和。

In [17]:
#1
def factorial (m):
    result=1
    for i in range(1,m+1,1):
        result*=i
    return result
m=int(input('please enter an integer. '))
n=int(input('please enter an integer. '))
k=int(input('please enter an integer. '))

total=factorial(m)+factorial(n)+factorial(k)
print(total)


please enter an integer. 3
please enter an integer. 3
please enter an integer. 3
18

In [2]:
# 2
def calculate (n):
    sum=0
    for i in range (1,n+1,1) :
        if i%2==0 :
            sum-=1/(i*2-1)
        else :
            sum+=1/(i*2-1)
    return sum


print(4*calculate(1000))
print(4*calculate(100000))


3.140592653839794
3.1415826535897198
  • 写函数,返回一个list中所有数字的和

In [8]:
def sum_list (mylist):
    mysum=sum(mylist)
    return mysum

number=[1,2,3,6,5,9]
print('The sum of the number in the list is :',sum_list(number))


The sum of the number in the list is : 26
  • 写函数,返回一个list中最小的数

In [7]:
def find_min (mylist):
    mymin=min(mylist)
    return mymin

number=[1,2,3,6,5,9]
print('The smallest number in the list is :',find_min(number))


The smallest number in the list is : 1
  • 写函数,返回某个元素/对象在一个list中的位置,如果不在,则返回-1.

In [13]:
def find_pos (mylist,m):
    pos=-1
    for i in range(len(mylist)) :
        if m==mylist[i] :
             pos=i
    return pos
    
number=[1,2,3,4,5,6,7,8,9]
i=int(input('plesase enter an integer. '))
pos=find_pos(number,i)
if pos==-1:
    print(i,'不在列表中。')
else :
    print(i,'在列表中的位置为第:',pos+1,'个')


plesase enter an integer. 3
3 在列表中的位置为第: 3 个
  • 练习二:写函数,可求两个向量的夹角余弦值,向量可放在list中。主程序调用该函数。

In [1]:
import math
def calculate_cos(a,b):
    moa=0
    mob=0
    s=0
    for x in a :
        moa+=x*x
    for y in b :
        mob+=y*y
    for i in range(len(a)) :
        s+=a[i]*b[i]
    cos=s/(math.sqrt(moa*mob))
    return cos
a=[]
b=[]
while True :
    x=input('请输入向量a的一个坐标值,以f 结束')
    if x=='f' :
        break
    a.append(int(x))
while True :
    y=input('请输入向量b的一个坐标值,以f 结束')
    if y=='f' :
        break
    b.append(int(y)) 
print('这两个向量夹角的余弦值是:',calculate_cos(a,b) )


请输入向量a的一个坐标值,以f 结束2
请输入向量a的一个坐标值,以f 结束2
请输入向量a的一个坐标值,以f 结束f
请输入向量b的一个坐标值,以f 结束1
请输入向量b的一个坐标值,以f 结束1
请输入向量b的一个坐标值,以f 结束f
这两个向量夹角的余弦值是: 1.0
  • 挑战性习题:python语言老师为了激励学生学python,自费买了100个完全相同的Macbook Pro,分给三个班级,每个班级至少分5个,用穷举法计算共有多少种分法?

In [3]:
method=0
for i in range (5,66,1):
    for j in range (5,66,1):
        for k in range(5,66,1) :
            if i+j+k==100 :
                method+=1
print(method)


2766