Python教程

《Python数学实验与建模》习题1

本文主要是介绍《Python数学实验与建模》习题1,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

习题1

  1. 数据类型有int,float,str,set,tuple,list,dic
pip install Matplotlib
  1. 元素组成的集合,list(元素有序可重复),set(元素无序不可重复),tuple(元素无序不可改),dic(无序键值对)
a = set()
b = dic{}
'''
字典:dic={a':12,'b':34}
集合:s={1,2,3,4}
列表:li=[1,2,3,3]
元组:tup=(1,2,3,4)#元组是不可更改的列表
'''
#集合的
s.add(x)
s.remove(x)
s.discard(x)
s.clear()
s.copy()
s.pop()
s.update(s2)#与s2并集并赋给s
s.difference(s2)#差
s.symmetric_difference(s2)#对称差AUB-A^B
s.intersection(s2)#交集
s.union(s2)#并集
#三种
import numpy [as np]
from numpy import random [as rd]
from numpy import *
def judge(x):
    if (x//100!=0 and x//1000==0) and (x==(x//100)**3+(x%100//10)**3+(x%10)**3):
        return True
    else: return False
x = eval(input("Please input a number:"))
print(judge(x))
'''
def judge(x):
    if (x//100!=0 and x//1000==0) and (x==(x//100)**3+(x%100//10)**3+(x%10)**3):
        return x
    else: return False
x = 0
while x <= 999:
    if judge(x) !=0 : print(judge(x))#judge(x)也可以直接作为条件
    x +=1
'''
import numpy
num = numpy.random.randint(100,1000)
print("The number generated is: ",(num))
print("The number prosessed is: ",(num-10*(num//10%10)))
x,y,z = eval(input("Please input 3 numbers: "))
if x*x+y*y+z*z > 1000: print((x*x+y*y+z*z)//1000)
else : print(x*x+y*y+z*z)

//eval的蜜汁判断

p,w,s = eval(input("Please input 3 numbers: "))
if s<250: d = 0#它认为百分号是string?
elif 250<=s<500: d = 2.5/100
elif 500<=s<1000: d = 4.5/100
elif 1000<=s<2000: d = 7.5/100
elif 2000<=s<2500: d = 9.0/100
elif 2500<=s<3000: d= 12.0/100
elif 3000<=s: d = 15.0/100
f = p*w*s*(1-d)
print("The total fee is: ",f)
  1. 公式怎么来的有待发现
m,d,y = eval(input("Please input 3 numbers(month,day,year): "))
y0 = y-(14-m)//12
x = y0+y0//4-y0//100+y0//400
m0 = m+12*((14-m)//12)-2
d0 = (d+x+(31*m0)//12)%7
print("%d年%d月%d日是星期%d"%(y,m,d,d0))
  1. 不知道为什么是这个 Pr ⁡ ′ ( 1 + r ′ ) N ′ ( 1 + r ′ ) N ′ − 1 \frac{\operatorname{Pr}^{\prime}\left(1+r^{\prime}\right)^{N^{\prime}}}{\left(1+r^{\prime}\right)^{N^{\prime}}-1} (1+r′)N′−1Pr′(1+r′)N′​
N,r,P = eval(input("请输入给定年限、利润率和贷款金额:"))
N_ = 12*N
r_ = r/12
P_ = P*r_*(1+r_)**N_/((1+r_)**N_-1)
print("每月需还贷金额为:",P_)
  1. 试了试北京到上海的距离,还蛮接近的(1088公里),此外就是角度的表示和与弧度互转的问题了
from numpy import radians,cos,sin
from numpy.lib.scimath import arccos
x1,y1,x2,y2 = eval(input("请输入两点的坐标A(x1,y1)、B(x2,y2):"))
R = 6370
d = R*arccos(cos(radians(x1-x2))*cos(radians(y1))*cos(radians(y2))+sin(radians(y1))*sin(radians(y2)))  
print("两点之间的距离为:%d km"%(d))
'''
请输入两点的坐标A(x1,y1)、B(x2,y2):116,39,120,30
两点之间的距离为:1065 km
'''
for n in range(1,1001):
    s = 0
    for m in range(1,n):
        if n%m == 0: s +=m
    if n == s: print(n,end="\t")
  1. 这个*与zip的搭配我还是有点迷
#answer is ([1,2,3],[-1,-2,-3])
'''可以比较好的解释
m2,n2 = zip(*zip(m, n))
print(m == list(m2) and n == list(n2))#Ture
'''
  1. 这里最后输出想在print里面用for循环直接输出,但是无果
def count(x):
    box = [0,0,0,0]
    for i in x:
        if 'A'<=i<='Z': box[0] +=1
        elif 'a'<=i<='z': box[1] +=1
        elif '0'<=i<='9': box[2] += 1
        else : box[3] += 1
    return box
x = input("请输入字符串:")
print("大写字母:%d\t小写字母:%d\t数字:%d\t其他:%d"%(count(x)[0],count(x)[1],count(x)[2],count(x)[3]))
'''
请输入字符串:ABCDabcd123456===\\
大写字母:4	小写字母:4	数字:6	其他:5
'''
  1. 处理得十分傻瓜
#处理个位及1~19
def p1(x):
    if x%100>19: x = x%10
    if x == 1: return "one"
    elif x == 2: return "two"
    elif x == 3: return "three"
    elif x == 4: return "four"
    elif x == 5: return "five"
    elif x == 6: return "six"
    elif x == 7: return "seven"
    elif x == 8: return "eight"
    elif x == 9: return "nine"
    elif x == 10: return "ten"
    elif x == 11: return "eleven"
    elif x == 12: return "twelve"
    elif x == 13: return "thirteen"
    elif x == 14: return "fourteen"
    elif x == 15: return "fifteen"
    elif x == 16: return "sixteen"
    elif x == 17: return "seventeen"
    elif x == 18: return "eighteen"
    elif x == 19: return "nineteen"
    else : return ""
#处理十位
def p2(x):
    x = x%100//10
    if x ==2: return "twenty"
    elif x == 3: return "thirty"
    elif x == 4: return "forty"
    elif x == 5: return "fifty"
    elif x == 6: return "sixty"
    elif x == 7: return "seventy"
    elif x == 8: return "eighty"
    elif x == 9: return "ninety"
    else : return ""
#处理百位
def p3(x):
    x = x//100
    if x:return p1(x)+" hundred"
    else :return ""
num = eval(input("please input a number: "))
#连接操作
#and
land = " and "
if p3(num)=="" or (p2(num)+p1(num)==""):land = ""
#连接存在的十位和个位
l1 = ""
if p2(num) and p1(num):l1=" "
print("It is %s"%(p3(num)+land+p2(num)+l1+p1(num)))
这篇关于《Python数学实验与建模》习题1的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!