time&datetime&random模块


import time
import datetime

'''Return the CPU time or real time since the start of the process or since
the first call to clock(). This has as much precision as the system
records.'''

print(time.clock())
print(time.process_time())#返回处理器时间
print(time.asctime())#返回时间格式Fri Apr 14 11:00:03 2017
print(time.asctime(time.localtime())) #返回时间格式,参数为struct time对象格式
print(time.localtime(time.time()))#返回本地时间 的struct time对象格式,参数是时间戳(秒)
print(time.localtime())#同上
print(time.ctime()) #Fri Apr 14 11:08:29 2017,参数为时间戳(秒)
print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式,参数是时间戳(秒)
print(time.gmtime(time.time() - 800000)) # 返回utc时间的struc时间对象格式,参数p_tuple


# 日期字符串 转成 时间戳 strptime
string_2_struct = time.strptime("2017/04/14","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
print(string_2_struct)#time.struct_time(tm_year=2017, tm_mon=4, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0,                                                  tm_wday=4, tm_yday=104, tm_isdst=-1)
struct_2_string=time.strftime("%x",string_2_struct) #将struct时间对象转换成日期格式 04/14/17
print(struct_2_string)#'04/14/17'

struct2_2_string=time.strftime("%Y-%m-%d %A %B",string_2_struct)
print(struct2_2_string)#'2017-04-14 Friday April'


#时间加减
print(datetime.datetime.now()) #返回 2017-08-19 12:47:03.941925
print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
print(datetime.datetime.now() )
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分

c_time  = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换
DirectiveMeaningNotes
%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.  
%d Day of the month as a decimal number [01,31].  
%H Hour (24-hour clock) as a decimal number [00,23].  
%I Hour (12-hour clock) as a decimal number [01,12].  
%j Day of the year as a decimal number [001,366].  
%m Month as a decimal number [01,12].  
%M Minute as a decimal number [00,59].  
%p Locale’s equivalent of either AM or PM. (1)
%S Second as a decimal number [00,61]. (2)
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
%w Weekday as a decimal number [0(Sunday),6].  
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
%x Locale’s appropriate date representation.  
%X Locale’s appropriate time representation.  
%y Year without century as a decimal number [00,99].  
%Y Year with century as a decimal number.  
%z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].  
%Z Time zone name (no characters if no time zone exists).  
%% A literal '%' character.


####random模块


import random
print random.random()#产生随机数
print random.randint(1,10)#1-10之间的随机整数
print random.randrange(1,10)#1-10之间不包括(1和10)的随机整数
 
写个随机生成验证码的函数玩玩
##随机生成验证码
def get_code():
checkcode = ''#初始化验证码
strings=[] #验证码生成来源
for i in range(1,5): #生成四个验证码
current = random.randrange(0,5)
if current != i:
chr1 = chr(random.randint(65,90)) #将数字转换成大写字母
chr2= chr(random.randint(97,122)) #将数字转换成小写字母
strings.append(chr1)
strings.append(chr2)
else:
digit = random.randint(0,9)
strings.append(str(digit))
checkcode += random.choice(strings)+" "
print(checkcode)
if __name__=="__main__":
get_code()

###杀人游戏角色随机分配
def get_role(L_role): #接收一个角色的列表
#从A-Z选取玩杀人游戏的人数;游戏人数肯定不超过26个
gamers=(chr(i) for i in range(65,91))#A-Z的生成器
game_players=[]#玩游戏的人
roles=[]#分配的游戏角色
for i in range(len(L_role)): #根据游戏的角色的人数选择参与游戏的人
# chr1 = chr(random.randint(65, 90))
random.shuffle(L_role)#打乱角色的顺序;其实有没有都无所谓,只是为了练习
game_players.append(next(gamers)) #调用生成器,选择一个参与游戏的人选
temp=random.choice(L_role) #随机分配游戏角色
roles.append(temp) #已经分配好的角色
L_role.remove(temp) #删除已经被分配了的角色
for gamer,role in zip(game_players,roles): #遍历输出游戏选手对应的游戏角色
print(gamer+" ",role)
L_role=["警察","警察","警察","杀手","杀手","杀手","平民","平民","平民","平民","平民","平民","平民"]
if __name__=="__main__":
# get_code()
print("gamer---role")
get_role(L_role)
 
posted @ 2017-04-14 00:01  WhatTTEver  阅读(182)  评论(0编辑  收藏  举报