21.time and random

Original: https://www.cnblogs.com/yuanchenqi/article/5732581.html

time module

Three Time representation

In Python, there are usually several ways to represent this time:

  • Timestamp (timestamp): Generally speaking, the timestamp indicates that from January 1970 00:00:00 1st offset press in seconds. We run "type (time.time ())", it returns a float.
  • Time string formatted
  • Tuple (struct_time): struct_time tuple total of nine elements were nine elements :( year, month, day, hour, minute, second, the first few weeks of the year, day of the year, daylight saving time)
Import Time 
 
# 1 Time (): Returns the current time timestamp 
time.time ()   # 1,473,525,444.037215 
 
# ---------------------------- ------------------------------ 
 
# 2 localtime ([secs]) 
# to convert a timestamp of the current time zone struct_time. secs parameter is not provided, the current time places subject. 
time.localtime () # a time.struct_time (tm_year = 2016, = tm_mon. 9, tm_mday =. 11, tm_hour = 0, 
# tm_min = 38 is, tm_sec = 39, = tm_wday. 6, tm_yday = 255, the tm_isdst = 0) 
time.localtime (1,473,525,444.037215 ) 
 
# ---------------------------------------------- ------------ 
 
# similar 3 gmtime ([secs]) and localtime () method, gmtime () method is to convert a timestamp struct_time the UTC time zone (0:00 region). 
 
#-------------------------------------------------- -------- 
 
# . 4 the mktime (T): the conversion of a struct_time timestamp. 
Print (time.mktime (time.localtime ())) # 1473525749.0 
 
# ---------------------------------- ------------------------ 
 
# . 5 the asctime ([T]): tuples represented by a time or as expressed in this form struct_time: 'Sun jun 20 23:21:05 1993 '. 
# If there are no parameters, it will be time.localtime () as a parameter. 
Print (time.asctime ()) # Sun Sep 11 00:43:43 2016 
 
# ------------------------------- --------------------------- 
 
# . 6 the ctime ([secs]): to a timestamp (in seconds floating point calculations) was converted to time.asctime () form. If the parameter is not given or 
# None of the time, it will default time.time () as a parameter. It acts time.asctime (time.localtime (secs)).
Print (time.ctime ())   # the Sun On Sep. 11 00:46:38 2016 
 
Print (time.ctime (the time.time ()))   # the Sun On Sep 2016 00:46:38. 11 
 
# . 7 the strftime (the format [, T] ): represents the time tuple or struct_time (as indicated by time.localtime () and 
# returns time.gmtime ()) is converted to time format string. If t is not specified, the incoming time.localtime (). If any one tuple 
# element out of bounds, ValueError error will be thrown. 
Print (The time.strftime ( " % D%% Y-X-M-% " , time.localtime ())) # 2016-09-11 00:49:56 
 
# . 8 the time.strptime (String [, the format]) 
# the time string into a format struct_time. In fact it strftime () is the inverse operation. 
Print (the time.strptime ( ' 2011-05-05 16:37:06' , ' % Y-M-%%% X-D ' )) 
 
# a time.struct_time (tm_year = 2011, tm_mon =. 5, tm_mday =. 5, tm_hour = 16, = 37 [tm_min, tm_sec =. 6, 
#   tm_wday =. 3, = 125 tm_yday, the tm_isdst = -1) 
 
# in this function, format default: "% a% b% d % H:% M:% S% Y". 
 
 
# 9 SLEEP (secs) 
# thread running postpone the specified time, in seconds. 
 
# 10 Clock () 
# this note, different meanings on different systems. On UNIX systems, it returns a "process of time", which is a floating-point (time stamp) in seconds. 
# In WINDOWS, the first call, the return is the actual running time of the process. The second was called after the first call since the current run 
# time, that is twice the time difference.
import time

#时间戳  #计算
# print(time.time())    #1481321748.481654秒

#结构化时间---当地时间
# print(time.localtime(1531242343))
# t=time.localtime()
# print(t.tm_year)
# print(t.tm_wday)
# #-----#结构化时间---UTC
# print(time.gmtime())

#-----将结构化时间转换成时间戳

# print(time.mktime(time.localtime()))
#------将结构化时间转成字符串时间strftime
#print(time.strftime("%Y---%m-%d %X",time.localtime()))
#------将字符串时间转成结构化时间strptime
#print(time.strptime("2016:12:24:17:50:36","%Y:%m:%d:%X"))

# print(time.asctime())
# print(time.ctime())


# import datetime
# print(datetime.datetime.now())

random模块

import random
 
print(random.random())#(0,1)----float
 
print(random.randint(1,3))  #[1,3]
 
print(random.randrange(1,3)) #[1,3)
 
print(random.choice([1,'23',[4,5]]))#23
 
print(random.sample([1,'23',[4,5]],2))#[[4, 5], '23']
 
print(random.uniform(1,3))#1.927109612082716
 
 
item=[1,3,5,7,9]
random.shuffle(item)
print(item)

 

import random

# print(random.random())#0.8882268701490094
# print(random.randint(1,3))#1到3, 2
# print(random.randrange(1,3))#1到2, 2
# print(random.choice([11,22,33,44,55]))#22
# print(random.sample([11,22,33,44,55],2))#[33, 44]
# print(random.uniform(1,4))#1到4的浮点型,3.5961920452336846

ret=[1,2,3,4,5]
random.shuffle(ret)#打乱
print(ret)#[4, 5, 2, 3, 1]
'''
def v_code():
    ret=""
    for i in range(5):
        num=random.randint(0,9)
        alf=chr(random.randint(65,122))
        s=str(random.choice([num,alf]))
        ret+=s
    return ret
print(v_code())
'''

 

 

 

验证码:

import random
def v_code():
    code = ''
    for i in range(5):
        num=random.randint(0,9)
        alf=chr(random.randint(65,90))
        add=random.choice([num,alf])
        code += str(add)
    return code
print(v_code())

 

 

 

Guess you like

Origin www.cnblogs.com/raitorei/p/11964111.html