python time module sys module random module

1, time module

  Built-in modules in python

  

#1, show the current timestamp
print(time.time())

#2, string formatting
print(time.strftime('%Y-%m-%d-%H-%M-%S'))#Display the current time in a custom format
print(time.strftime('%c')) #The default format displays the current time

#3. Local Structured Time
print(time.localtime())

#4. UK structured time, subtract 8 from the hour
print(time.gmtime())

Example:

#1, check how many seconds have passed since 2015-2-24

l = time.strptime('2015-2-24','%Y-%m-%d')
print(time.mktime(l)) #mktime displays the timestamp of structured time
print(time.time()) #Display timestamp

#2. Use the timestamp to find the specific time
ret = time.localtime (2000000000)
print (ret)
print(time.strftime('%Y-%m-%d',ret)) #Change the timestamp to a specific year, month and day

#3, display the time in the default format of the timestamp
print(time.strftime('%c'))
print(time.ctime(123455678)) #Convert to default time format output

 

2. random module

  1, random decimal

    random.uniform(1,4

import random
random.uniform(1,4)#Use of red envelopes

   2. Random Integer

import random
random.randint(1,5)#1-5 all integers 

random.randrange(1,3)#1-2 all integers
#lottery or other requirements

   3. Random elements

import random
random.choice([1,2,'34','dsa', [1,2]]) #Random one element

   4. Random multiple elements

import random
random.sample([1,2,3,4,5,6,7,8,5,3],2))#The second parameter 2 means to take two random numbers

 

3.sys module

The os module deals with the operating system

sys is dealing with the interpreter

sys.argv List of command line parameters, the first element is the path of the program itself 
  Displays the absolute path of the current file sys.exit(n) Exit the program, exit(0) when exiting normally, exit
  directly with error sys.exit(1) sys.version Get the version information of the Python interpreter
  Query the version of the current python interpreter sys.path returns the search path of the module, using the value of the PYTHONPATH environment variable during initialization. The
  most important point, all the environment paths of python sys.platform returns
  the OS platform name Questionable version query! !

 

 

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324820560&siteId=291194637