python学习7:(模块创建及import指令运用)python标准模块 —— random随机数、time时间模块

模块创建及import指令运用

Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句

# 模块路径问题

import pandas
print(pandas.__file__)
# 查看现有包所在路径

#import sys
#sys.path.append('C:/Users/Hjx/Desktop/')
# 加载sys包,把新建的testmodel所在路径添加上

# 简化模块名:import...as...
# 调用部分模块语句:From…import 语句

python标准模块 —— random随机数

import random

x = random.random()
y = random.randint(0,10)
print(x,y)
# random.random()随机生成一个[0:1)的随机数
# random.randint(a,b)随机生成一个[a:b]的整数

lst = list(range(10))
st1 = random.choice(lst)
st2 = random.sample(lst,5)
random.shuffle(lst)
print(lst,st1,st2,type(st1),type(st2))
# random.choice()随机获取()中的一个元素,()中必须是一个有序类型
# random.sample(a,b)随机获取a中指定b长度的片段,不会改变原序列
# random.shuffle(list)将一个列表内的元素打乱

python标准模块 —— time时间模块
time.sleep()程序休息()秒
time.ctime()将当前时间转换为一个字符串

import time
for i in range(2):
    print('hello')
    time.sleep(1)
#time.sleep()程序休息()秒

print(time.ctime(),type(time.ctime()))
#将当前时间转换为一个字符串

print(time.localtime(),type(time.localtime()))
# 将当前时间转为当前时区的struct_time
# wday 0-6表示周日到周六
# ydat 1-366 一年中的第几天
# isdst 是否为夏令时,默认为-1

print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))

时间符号表示

%y 两位数的年份表示(00-99%Y 四位数的年份表示(000-9999%m 月份(01-12%d 月内中的一天(0-31%H 24小时制小时数(0-23%I 12小时制小时数(01-12%M 分钟数(00=59%S 秒(00-59%a 本地简化星期名称

%A 本地完整星期名称

%b 本地简化的月份名称

%B 本地完整的月份名称

%c 本地相应的日期表示和时间表示

%j 年内的一天(001-366%p 本地A.M.或P.M.的等价符

%U 一年中的星期数(00-53)星期天为星期的开始

%w 星期(0-6),星期天为星期的开始

%W 一年中的星期数(00-53)星期一为星期的开始

%x 本地相应的日期表示

%X 本地相应的时间表示

%Z 当前时区的名称

%% %号本身
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值