函数模块
# 编写一个求平均值的函数 def f(*m): s = 0 lst = [] for i in m: while str(i).isnumeric(): s += i lst.append(i) else: print('输入端内的内容包含有非法数字') return (s / len(lst))
# 定义一个函数 函数的作用是把输入的列表变成一连串字典的key lst = eval(input('请输入一个列表: ')) def f(lt): return (dict.fromkeys(lt, 0)) print(f(lst))
创建一个模块,包含一个阶乘函数f1(n),一个列表删除值函数f2(lst,x),一个等差求和函数f3(a,d,n) def f1(n): y = 1 for i in range(1,n+1): y = y * i return y def f2(lst,x): while x in lst: lst.remove(x) return lst def f3(a,b,n): an = a s = 0 for i in range(n-1): an = an + b s = s + an return s
import sys sys.path.append('C:/Users/11786/Desktop') #将自建的模块写入系统路径里面 注意此时反斜杠的方向 import tmodel print(tmodel.f1(10)) print(tmodel.f2([1,2,3333,3333,4,5,6],3333)) print(tmodel.f3(10,5,5))
#python标准模块 -random函数 import random x = random.random() y = random.random() print(x, y*100) m = random.randint(0,100)#随机取整数值在0-100之间 print(m) lst = list(range(20))#随机在给定的范围内选取值 s = random.choice(lst) print(s) sli = random.sample(lst,6)#随机在给定的列表范围内选取若干个数值 print(sli) lst random.shuffle(lst)
#pip:python工具包管理工具 用于安装和卸载python工具包 #在cmd 中运行 ,安装包 pip install xxx
#python函数模块- time模块 import time for i in range(5): print("hello") time.sleep(0.11)#让time语句执行的时间往后顺延time.sleep() print(time.ctime())#生成当时时间的时间戳 其类型为字符串 print(time.localtime( ))#将当前的时间转为当前时区的struct_time wday0-6表示周日到周六 #ydat 1-366表示一年中的第几天 isdst表示是否为夏令时 默认为-1 print(time.strftime('%Y-%m-%d %H:%M-%S',time.localtime())) #time.strftime(a,b) #a 为格式字符串格式 #b 为时间戳 一般为localtime() #具体时间格式参照python基本语法里面的
如有侵权 请联系作者删除 邮箱1178658361@qq.com

浙公网安备 33010602011771号