Python日常练习一

先附学长的代码,我没来得及做,检查了一遍

#练习1
#1(1)
import math
import numpy as np

def fun1(x1,x2):
    y = math.log(x1 + x2**2)
    return y

f1 = fun1(1,2)
print('函数值为:',np.round(f1,2))
#1(2)
def fun2(x):
    y = math.sin(x**2) + math.exp(2*x**3)
    return y

f2 = fun2(2)
print('函数值为:%.2f'%f2)
#2
def fun3(x):
    y = [math.sin(x),math.cos(x),math.exp(x),abs(x)]
    return y

x3 = input('请输入自变量:')
x_3 = eval(x3)
f3 = fun3(x_3)
print('自变量所对应的正弦值为%.2f\n余弦值为%.2f\n指数函数值为%.2f\n绝对值为%.2f'
      %(f3[0],f3[1],f3[2],f3[3]))

#练习2
#1
A = np.array(((1,2,-1,3,5),(1,-2,9,0,-6),(-3,3,-4,7,1),(9,8,0,7,6)))
print('A =\n',A)
# 2
A1 = A[0:2,1:3]
print('A1 =\n',A1)
#3
A2 = np.array([A[0,:],A[2,:]])
print('A2 =\n',A2)
#4
A3 = np.array([A[2,:],A[1,:],A[0,:]])
print('A3 =\n',A3)
#5
A_4 = []
for i in range(2):
    for j in range(5):
        if abs(A2[i,j]) > 3:
            A_4.append(A2[i,j])
        else:
            pass
A4 = np.array([A_4])
print('A4 =\n',A4)
#5
a = A[0,0]
for i in range(4):
    for j in range(5):
        if A[i,j] > a:
            a = A[i,j]
            x = i + 1
            y = j + 1
        else:
            pass
print('A的最大值为%d\n位于A的第%d行第%d列'%(a,x,y))

#练习3
#1
for i in range(1,11):
    x = math.sin(i * math.pi / 10)
    print('sin(%d*pi/10) = %.2f\n'%(i,x))
#2
x = 10000
i = 0
while x < 20000:
    x = x * 1.1125
    i = i + 1
    print('%d年后共有%.2f元'%(i,x))
#3
x1 = 0
x2 = 1
while x2 < 10000:
    t = x2
    x2 = x1 + x2
    x1 = t
print('Fibonacci数列中第一个大于10000的元素为:',x2)
#4
n = 1
sum = 0
for i in range(1,21):
    n = n * i
    sum = sum + n
print('所求值为:',sum)

#练习题4
#1
x = []
for i in range(10):
    a = input('请输入数组中第%d个数:'%(i+1))
    a1 = eval(a)
    x.append(a1)
for j in range(9):
    for i in range(0,9-j):
        if x[i] > x[i+1]:
            t = x[i]
            x[i] = x[i+1]
            x[i+1] = t
        else:
            pass
print('这十个数由小到大排序为:',x)
#2
S = [8,9,11,-9,0,2,-8,2,4,2,3,5]
s = []
for i in S:
    if abs(i) > 3:
        s.append(i)
s = np.array(s)
print('所求向量为:',s)
#3
h = 100
i = 0
sum = 0
while i < 10:
    h = h / 2
    sum = sum + 1.5 * h
    i = i + 1
sum = sum - h
print('小球第10次落地时,共经过%f米\n第十次反弹为%f米'%(sum,h))
#4
def fun4(x):
    if x < 2:
        y = x + 1
    elif x <= 8:
        y = 3 * x
    elif x <= 20:
        y = 4 * x - 5
    else:
        y = math.cos(x) + math.sin(x)
    return y
x1 = input('请输入自变量的取值:')
x2 = eval(x1)
f = fun4(x2)
print('对应函数值为:',f)

#练习5
#1
from scipy import linalg
A = np.array([[3,1,-1],[1,2,4],[-1,4,5]])
b = np.array([3.6,2.1,-1.4])
x = linalg.solve(A,b)
print('该线性方程组的解为:',x)
#2
import matplotlib.pyplot as plt
x = np.linspace(0,6)
y1 = np.sin(2*x)
y2 = np.sin(x**2)
y3 = np.sin(x)**2
plt.plot(x,y1)
plt.plot(x,y2)
plt.plot(x,y3)
plt.show()

还是有些补充的:

1、题一省略

2、题二
Python日常练习一_第1张图片
第三题,按行形成矩阵,一开始我想到的是另一个函数:

A2 = np.hstack((A[0,:],A[2,:]))
out:
 [ 1  2 -1  3  5 -3  3 -4  7  1]

np.hstack((a,b))是将ab两个矩阵按照行拼接
同理np.vstack((a,b))将ab两个矩阵按照列拼接

第四题:逆序提取,我理解的是 :行是顺序,列是逆序。

A3 = np.array([A[0,::-1],A[1,::-1],A[2,::-1]])

切片的三个变量(起始,终点,步数)。

第五题:直接a4=A2[abs(A2)>3],学长的可以优化
同理第六题:直接k=np.max(A) x=np.where(A==k)

Python日常练习一_第2张图片
第一次知道linalg的用法

#1
from scipy import linalg
A = np.array([[3,1,-1],[1,2,4],[-1,4,5]])
b = np.array([3.6,2.1,-1.4])
x = linalg.solve(A,b)
print('该线性方程组的解为:',x)
#2
import matplotlib.pyplot as plt
x = np.linspace(0,6)
y1 = np.sin(2*x)
y2 = np.sin(x**2)
y3 = np.sin(x)**2
plt.plot(x,y1)
plt.plot(x,y2)
plt.plot(x,y3)
plt.show()

本来想附一些函数的链接,但函数一大推,用的时候可以查,不求太简洁,只要做对就行了。


下面赋其他题的截图
Python日常练习一_第3张图片

Python日常练习一_第4张图片

Python日常练习一_第5张图片

你可能感兴趣的:(Python学习,python)