Python基础-时间模块和radom模块
2024-10-16 08:21:57
时间模块
import time # 引入时间模块
print(time.time()) # 1508146954.9455004: 时间戳
print(time.clock()) # 计算CPU执行时间
print(time.gmtime()) # UTC时间
print(time.localtime()) # 本地时间
print(time.strftime("%Y-%m-%d %H:%M:%S"))
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM. print(time.strftime("%Y-%m-%d %H:%M:%S%z"))
执行结果:
2017-10-16 18:17:39
fansik = time.strptime('2017-10-16 18:16:26',"%Y-%m-%d %H:%M:%S") print(fansik)
执行结果:
time.struct_time(tm_year=2017, tm_mon=10, tm_mday=16, tm_hour=18, tm_min=16, tm_sec=26, tm_wday=0, tm_yday=289, tm_isdst=-1) print(fansik.tm_year)
执行结果:
2017 import datetime
print(datetime.datetime.now())
执行结果:
2017-10-16 18:27:03.256569
random模块
import random
print(random.random()) # 0到1的随机数:0.06054267487515341
print(random.randint(1, 9999)) # 1到9999的随机数,包含1和9999
print(random.randrange(1, 9999)) # 1到9999的随机数,不包含9999
print(random.choice('fansik')) # 随机fansik中的一个字符
print(random.choice(['fansik', 'fanjinbao', 'zhangsan'])) # 随机这三个名字
print(random.sample(['fansik', 'zhangsan', 'fanjinbao', 'lisi'], 2)) # 随机选两个名字 随机验证码:
import random
def v_code():
code = ''
for i in range(5):
add = random.choice([random.randrange(10), chr(random.randrange(65, 91))])
# if i == random.randint(0, 4):
# add = random.randrange(10)
# else:
# add = chr(random.randrange(65, 91))
code += str(add)
print(code)
v_code()
最新文章
- 1Z0-053 争议题目解析683
- Java中分割字符串
- ELK日志分析系统搭建(转)
- iOS开发——源代码管理——SVN
- 我给出的一份Java服务器端面试题-适合电话面试
- hdu1241 Oil Deposits
- 数据的存储-NSKeyedArchiver和write to file介绍
- Android studio GPU Monitor :GPU Profiling needs to be enabled in the device's developer options
- composer api 参考
- 深入浅出jsonp(转)
- 鼠标点击 input,显示瞬间的边框颜色,对之修改与隐藏
- CentOS 7.2下安装Mono 5.0
- java压缩包上传,解压,预览(利用editor.md和Jstree实现)和下载
- Spring:(一)入门基础学习
- 【译】PX、EM还是REM媒体查询?
- 在operator =中要处理“自我赋值”
- [LeetCode] 240. Search a 2D Matrix II_Medium tag: Binary Search
- JavaScript事件监听以及addEventListener参数分析
- taro refs引用
- Intel Galileo驱动单总线设备(DHT11\DHT22)(转)
热门文章
- iOS_16_控制器切换_modal_storyboard拖线的方式
- Java客户端Jedis
- UML类图详解_关联关系_多对一
- char device
- codeforces 651a oysticks
- python 开发技巧(2)-- Django的安装与使用
- Vsphere日记03.ESXi5.5.client
- address-already in use 以及查看端口
- Android_Exception_Solution Lib
- 接入qq登录功能出现的问题