It has been 0 days since the last update of this article. Some contents may be outdated. Please pay attention to screening.
本站之前有多篇文章是介绍不同python的函数,现将这些文章整合一下。
素数判断函数
1 2 3 4 5 6 7
defIsPrime(n): if n<2: returnFalse for i inrange(2,n): if n % i==0: returnFalse returnTrue
计算每位数的阶乘的函数
1 2 3 4 5 6 7 8 9
defFactorial(n): sum1 = 0 for i instr(n): i = int(i) fact = 1 for j inrange(2,i+1): fact *= j sum1 += fact return sum1
寻找互质数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
for m inrange(100,1000): guam = m nstr = str(m) n = nstr[::-1] if n[0] == '0': continue else: n=eval(n) guan = n r = m%n while r!= 0: m=n n=r r=m%n if n == 1: print('{}与{}互质'.format(guam,guan))
找出规定范围内每位数都是素数的数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
for n inrange(100,1000): nstr=str(n) for s in nstr: ints=eval(s) if ints<2: break for i inrange(2,ints): if ints%i==0: flag=False break else: flag=True ifnot flag: break else: print(n)
寻找范围内的完数
1 2 3 4 5 6 7 8 9
for n inrange(10,10000): st="" sum1=0 for i inrange(1,n): if n%i==0: st=st+str(i)+"+" sum1+=i if sum1==n: print("{}={}".format(n,st[:-1]))
最小公倍数
1 2 3 4 5 6 7 8 9
m = int(input("Please input the first num:")) n = int(input("Please in put the second num:")) p = m*n r = m%n while r != 0: m = n n = r r = m % n print(p//n)
寻找4位降序数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import random ls=[] for i inrange(30): x=random.randint(1000,9999) ls.append(x) print(ls) count=0 for t in ls: s=str(t) for j inrange(len(s)-1): if s[j]<=s[j+1]: break else: print(s) count+=1 if count==0: print("无降序数")
实现读取json文件功能的部分函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import os
defReadJson(fn): retL = [] f = open(fn, encoding = 'utf-8') for line in f: retL.append(eval(line.strip())[0]) f.close() return retL
defReadFiles(fp): retAll = [] files = os.listdir(fp) for file in files: fa = fp + '\\' + file retT = ReadJson(fa) retAll += retT return retAll
income = eval(input('请输入月收入:')) if income <= 2000: tax = 0 elif income > 2000and income <=3000: tax = ( income - 2000 ) * 0.05 elif income > 3000and income <=4000: tax = ( income - 3000 ) * 0.15 + 50 elif income > 4000and income <=5000: tax = ( income - 4000 ) * 0.25 + 200 else: tax = ( income - 5000 ) * 0.35 + 450 print('月收入{:.2f},调节税为{:.2f}'.format(income,tax))
生成指定项数的斐波拉契数列
1 2 3 4 5 6 7 8 9 10 11 12
list0 = [1,1] terms = eval(input('斐波拉契数列的项数:')) for i inrange(terms-2): list0.append(list0[-2]+list0[-1]) print(list0) for t inrange(2,terms-1): #t表示index num = list0[t] #num为从列表中取出的数 for k inrange(2,t): #k表示可能的因数 if num%k == 0: break else: print(num)
将分钟转化为X小时X分钟
1 2 3 4 5 6 7 8 9
min_time = eval(input('please input you num(minute):\n')) hour = 0 whileTrue: if min_time >= 60 : min_time -= 60 hour += 1 else: break print('{}hours and {}minutes'.format(hour,min_time))
##operation:1register 2login 3changepwd 4logout ##在break之前为mode赋值实现切换功能 prompt = "Select the appropriate operation: 1:register 2:login 3:change-password 4:exit\n" users = {} mode = 0 whileTrue: if mode == 0: print("We will use the 'num-' -like form to indicate what you are doing.") mode = input(prompt) elif mode == "1": whileTrue: user = input("1-The user will now continue to be created.\n1-Type # to end.\n\n1-Please input your username.\n") if user == "#": mode = 0##程序初始化 break pwd = input("1-Please input your password now.\n") whileTrue: if user == pwd: print("1-Password is too weak!") pwd = input("1-Please input your password now.\n") ##密码强度检查 else: break if user notin users: users[user] = pwd else: print("1-@@@Alert!The user already exists!") elif mode == "2": whileTrue: user = input("2-Please input your username.\n2-Type # to end.\n") if user == "#": mode = 0##初始化 break if user in users: pwd = input("2-Please input your password now.\n") if users[user] == pwd: print("2-Login successfully!") else: print("2-Wrong password provided!") else: mode = input("2-The user does not exist.\n2-Enter the function number provided before to switch to another function.\n") break##实现功能跳转。 break elif mode == "3": whileTrue: user = input("3-Please input your username.\n3-Type # to end.\n") if user == "#": mode = 0##初始化 break if user in users: pwd = input("3-Please input your original password now.\n") if users[user] == pwd: pwd1 = input("3-Correct password!\n3-Now please input the new password.\n") while pwd1 == pwd: pwd1 = input("3-The password is the same as the old one. Please re-enter it.\n") if pwd1 == input("3-Please input your new password again!\n"): users[user] = pwd1 print("3-Password updated successfully!") else: print("3-Wrong password provided!") else: mode = input("3-The user does not exist.\n3-Enter the function number provided before to switch to another function.\n") break##实现功能跳转。 break else: break