First, the time module
1. timestamp calculation done
Get is the number of seconds, from at 0:00 on January 1, 1976 start to count now
import time print(time.time())
2. Structure of time
It shows the current date, time
You can then obtain the desired value (example: Year extract) through to a variable
import time print(time.localtime()) t = time.localtime() print(t.tm_year)
3. The time stamp is converted to a structured
Parameters: seconds
import time print(time.localtime(1272352345))
4. Universal Time, UTC (UK time zone)
import time print(time.gmtime())
The converting structure of the time stamp
import time print(time.mktime(time.localtime()))
6. The conversion time is structured string time strftime
% Y: Year% m: month% d: Day% X: minutes and seconds, the middle "-" customize
import time print(time.strftime("%Y-%m-%d %X", time.localtime()))
7. Converts a string structured time to time strptime
8. The time is the conversion structure of a fixed format string time
import time print(time.asctime()) #参数默认为time.localtime() print(time.asctime(time.localtime(1734124122)))
9. The time stamp is converted to a fixed format string time
Time Import Print (time.ctime ()) # default parameters time.time () Print (time.ctime (1,724,239,748))
10. The thread running postpone the specified time, in seconds
Time Import the time.sleep (2) Print ( "I did not print qaq after two seconds")
11 shows the current month and year time
import datetime print(datetime.datetime.now())
Two, random module
1. give float (0 - 1) of the random number
import random print(random.random())
2. Get an integer [1, 3] random number
import random print(random.randint(1, 3))
3. Integer to give [1, 3) a random number
import random print (random.randrange (1, 3))
4. Parameters: iterables
Wherein obtain a random value
import random print(random.choice([11, 22, 33]))
5. Incoming iterables and a value of n, n random values obtained
import random print(random.sample("werdwqrfdqw",3))
6. Get any range of floating-point numbers, exclusive of a number
import random print(random.uniform(1, 3))
7. The list of upset (I tested only upset list)
import random str1 = [11, 22, 33, 44, 55] random.shuffle(str1) print(str1)
8. The preparation of a 5-bit program codes, a random combination of alphanumeric
pass: ascill code
chr(65, 90) ----> A - Z
chr(97, 122) ------> a - z
Random Import DEF v_code (): RET = "" for I in Range (. 5): NUM the random.randint = (0,. 9) ALF = CHR (the random.randint (65, 90)) in # A - Z randomized to receive a alf2 = chr (random.randint (97, 122)) # in a - to give a random Z RES = STR (the random.choice ([ALF, formation of aluminum difluoride])) # in a - z to obtain a random res2 = str (random. choice ([res, num]) ) # between letters and numbers to give a random RET RET = + RES2 return RET Print (v_code ())