python-day19_time_OS等模块

 

1,

#列表、元祖
#字典
#集合、frozenset
#字符串
#堆栈 : 先进后出
#队列 :先进先出 FIFO

# from collections import namedtuple
# Point = namedtuple('point',['x','y','z'])
# p1 = Point(1,2,3)
# p2 = Point(3,2,1)
# print(p1.x)
# print(p1.y)
# print(p1,p2)

#花色和数字
# Card = namedtuple('card',['suits','number'])
# c1 = Card('红桃',2)
# print(c1)
# print(c1.number)
# print(c1.suits)

#队列
# import queue
# q = queue.Queue()
# q.put([1,2,3])
# q.put(5)
# q.put(6)
# print(q)
# print(q.get())
# print(q.get())
# print(q.get())
# print(q.get()) # 队列取完后,再取时,程序阻塞
# print(q.qsize())

 

# from collections import deque  #双端口队列,从队列前后2个端口进行取放
# dq = deque([1,2])
# dq.append('a') # 从后面放数据 [1,2,'a']
# dq.appendleft('b') # 从前面放数据 ['b',1,2,'a']
# dq.insert(2,3) #['b',1,3,2,'a']
# print(dq.pop()) # 从后面取数据
# print(dq.pop()) # 从后面取数据
# print(dq.popleft()) # 从前面取数据
# print(dq)

2,

#有序字典
# from collections import OrderedDict
# od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
# print(od) # OrderedDict的Key是有序的
# print(od['a'])
# for k in od:
# print(k)

# from collections import defaultdict
# d = defaultdict(lambda : 5)
# print(d['k'])

3,

# 格式化时间 —— 字符串: 给人看的
# 时间戳时间 —— float时间 : 计算机看的
# 结构化时间 —— 元祖 :计算用的

表示时间的三种方式

在Python中,通常有这三种方式来表示时间:时间戳、元组(struct_time)、格式化的时间字符串:

(1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。

(2)格式化的时间字符串(Format String): ‘1999-12-06’

%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 当前时区的名称
%% %号本身
View Code

 

(3)元组(struct_time) :struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)

索引(Index)属性(Attribute)值(Values)
0 tm_year(年) 比如2011
1 tm_mon(月) 1 - 12
2 tm_mday(日) 1 - 31
3 tm_hour(时) 0 - 23
4 tm_min(分) 0 - 59
5 tm_sec(秒) 0 - 60
6 tm_wday(weekday) 0 - 6(0表示周一)
7 tm_yday(一年中的第几天) 1 - 366
8 tm_isdst(是否是夏令时) 默认为0

 

几种格式之间的转换

#时间戳,秒为单位,自1970年1月1日00:00:00以来经历了多少秒
  >>>time.time()
  1500875844.800804

#时间元组: localtime将一个时间戳转换为当前时区的struct_time
  print(time.localtime()) # 可传参数值为秒的值
  #输出:time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24,
            tm_hour=13, tm_min=59, tm_sec=37,
            tm_wday=0, tm_yday=205, tm_isdst=0)

#时间字符串
  time.strftime("%Y-%m-%d %H-%M-%S",date_touple)
  >>>time.strftime("%Y-%m-%d %X")
  '2017-07-24 13:54:37'
  >>>time.strftime("%Y-%m-%d %H-%M-%S")
  '2017-07-24 13-55-04'

# t = time.time()
# print(t)
# print(time.localtime(3000000000))
# print(time.gmtime(t))

import time

# timestamp >> struct_time >> format string
mytime = 400000000
struc_str = time.gmtime()   # 格林威治时间
print(struc_str)
struc_str1 = time.gmtime(mytime)    # 按格林威治标准时间计算
print(struc_str1)
struc_str2 = time.localtime(mytime)  # 按北京时间,会自动+8。推荐
print(struc_str2)
format_str = time.strftime('%Y-%m-%d %H:%M:%S',struc_str2)
print(format_str)   # 1982-09-04 23:06:40

# format string >> struct_time >> timestamp
format_str = '1982-09-04 23:06:40'
struc_str = time.strptime(format_str,'%Y-%m-%d %H:%M:%S')
print(struc_str)
mytime = time.mktime(struc_str)
print(mytime)  # 400000000.0
时间格式转换示范

 

4,随机数

>>> import random
#随机小数
>>> random.random()      # 大于0且小于1之间的小数
0.7664338663654585
>>> random.uniform(1,3) #大于1小于3的小数
1.6270147180533838
#恒富:发红包 #随机整数 >>> random.randint(1,5) # 大于等于1且小于等于5之间的整数 >>> random.randrange(1,10,2) # 大于等于1且小于10之间的奇数 #随机选择一个返回 >>> random.choice([1,'23',[4,5]]) # #1或者23或者[4,5] #随机选择多个返回,返回的个数为函数的第二个参数 >>> random.sample([1,'23',[4,5]],2) # #列表元素任意2个组合 [[4, 5], '23']

 #打乱列表顺序

>>> item=[1,3,5,7,9]
>>> random.shuffle(item) # 打乱次序

 5,OS模块

# os.system("dir")  #执行操作系统下的命令
# ret = os.popen("dir").read()
# print(ret)

ret = sys.argv  #在操作系统终端界面操作用,可用于给python程序传参数
name = ret[1]
pwd = ret[2]
if name == 'alex' and pwd == 'alex3714':
  print('登陆成功')
else:
  print("错误的用户名和密码")
  sys.exit()
print('你可以使用计算器了')

 

import time
old_time = time.mktime(time.strptime('1986-11-07 07:20:00','%Y-%m-%d %H:%M:%S')) # 时间戳,单位秒
now = time.time()
now_format = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(now))
vv = now - old_time
struct_time = time.localtime(vv)
print('-从{}开始,到现在{}-'.format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(old_time)),now_format))
print('共经历了:{}年{}月{}日 {}:{}:{}'.format(struct_time.tm_year-1970,struct_time.tm_mon-1,
                                      struct_time.tm_mday-1,struct_time.tm_hour,struct_time.tm_min,
                                      struct_time.tm_sec))
时间差计算

 

# import random
# li = []
# for i in range(4):
#     check_num1 = str(random.randrange(0, 9))
#     check_num2 = chr(random.randrange(65, 90))
#     li.append(random.choice([check_num1,check_num2]))
# result = ''.join(li)
# print(result)
random_verify_code

 

posted @ 2019-01-08 11:20  烟云过眼  阅读(151)  评论(0编辑  收藏  举报