习题1
pip install Matplotlib
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)
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))
N,r,P = eval(input("请输入给定年限、利润率和贷款金额:")) N_ = 12*N r_ = r/12 P_ = P*r_*(1+r_)**N_/((1+r_)**N_-1) print("每月需还贷金额为:",P_)
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")
#answer is ([1,2,3],[-1,-2,-3]) '''可以比较好的解释 m2,n2 = zip(*zip(m, n)) print(m == list(m2) and n == list(n2))#Ture '''
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~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)))