python的一些小练习(1)

(1)用起泡法对十个数由大到小排序,即将相邻两个数比较,将小的调到前头

input_values=input("输入十个数")
values=[int(x) for x in input_values.split( )]
for i in range(9):
    for j in range(9-i):
        if values[j]<values[j+1]:
            t=values[j+1]
            values[j+1]=values[j]
            values[j]=t
print("排序后:",values)

(2)有一个4x5矩阵,编程求出其最大值及其所处的位置

import random
matrix=[[random.randint(0,100) for i in range(3)] for j in range(4)]
max=matrix[0][0]
for i in range(4):
    for j in range(3):
        if max<matrix[i][j]:
            max=matrix[i][j]
            a=i
            b=j
print("该矩阵的最大值为:",max,"\n位置为第",a,"行","第",b,"列")

(3)求\sum\limits_{i=1}^nn!

sum=0
for i in range(1,21):
    s = 1
    for j in range(1,i+1):
        s=s*j
    sum=sum+s
print(sum)

(5)有一函数f\left ( x,y\right )=x^{2}+\sin xy+2y,写一程序,输入自变量的值,输出函数值

import numpy as np
x=float(input("输入x值:"))
y=float(input("输入y值:"))
def f(x,y):
    f=x**2+np.sin(x*y)+2*y
    return f
print("函数值:",f(x,y))