Common module 2

There are several types of these libraries in python, and the one we use the longest is the standard library

  1> standard library
  2> open source modules/third party modules
  3> custom modules

 

time module

In python, there are basically ways to represent time: ① timestamp, ② formatted time string

%y Two-digit year representation (00-99 )
 %Y Four-digit year representation (000-9999 )
 %m Month (01-12 )
 %d Day of the month (0-31 )
 %H 24 hours Hours (0-23 )
 %I Hours in 12-hour format (01-12 )
 %M Minutes (00=59 )
 %S Seconds (00-59 )
 % a Local Abbreviated Week Name
 % A Local Full Week Name
 % b local abbreviated month name
 % B local full month name
 % c local corresponding date representation and time representation
 %j day of the year (001-366 )
 % p local AM or PM equivalent
 %U week of the year Number (00-53 ) Sunday is the start of the week
 %w Week (0-6 ), Sunday is the start
 of the week %W Number of the week in the year (00-53 ) Monday is the start of the week
 % x local corresponding date Represents
 the local corresponding time representation of % X
% Z The name of the current time zone
 %%% sign itself

③The tuple (struct_time) has a total of nine elements.

Index (Index) Attribute Values
0 tm_year (year) such as 2011
1 tm_mon (Monday) 1 - 12
2 tm_mday (day) 1 - 31
3 tm_hour(时) 0 - 23
4 tm_min (minutes) 0 - 59
5 tm_sec (seconds) 0 - 60
6 tm_wday(weekday) 0 - 6 (0 means Monday)
7 tm_yday (day of the year) 1 - 366
8 tm_isdst (whether daylight saving time) Default is 0

 

First, let's import the time module to understand several formats for representing time in python:

#import time module
>>>import time

#timestamp
>>>time.time()
1500875844.800804

#time string
>>>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'

#time tuple: localtime converts a timestamp to struct_time of the current time zone
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)

Summary: Timestamps are times that computers can recognize; time strings are times that people can understand; tuples are used to manipulate time

Conversion between several formats

#Timestamp --> structured time
#time.gmtime(timestamp) #UTC time, consistent with the local time in London, UK
#time.localtime(timestamp) #Local time. For example, we are now executing this method in Beijing: 8 hours difference from UTC time, UTC time + 8 hours = Beijing time
>>>time.gmtime(1500000000)
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
>>>time.localtime(1500000000)
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)

#structured time --> timestamp 
#time.mktime (structured time)
>>>time_tuple = time.localtime(1500000000)
>>>time.mktime(time_tuple)
1500000000.0
 
#structured time --> string time
#time.strftime("Format Definition", "Structured Time") If the structured time parameter is not passed, the current time will be displayed
>>>time.strftime("%Y-%m-%d %X")
'2017-07-24 14:55:36'
>>>time.strftime("%Y-%m-%d",time.localtime(1500000000))
'2017-07-14'

#String time --> Structured time
#time.strptime(time string, string corresponding format)
>>>time.strptime("2017-03-16","%Y-%m-%d")
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=75, tm_isdst=-1)
>>>time.strptime("07/24/2017","%m/%d/%Y")
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=205, tm_isdst=-1)

 

 
#Structured time --> %a %b %d %H:%M:%S %Y string
#time.asctime(structured time) If no parameters are passed, the formatted string of the current time is returned directly
>>>time.asctime(time.localtime(1500000000))
'Fri Jul 14 10:40:00 2017'
>>>time.asctime()
'Mon Jul 24 15:18:33 2017'

#Timestamp --> %a %d %d %H:%M:%S %Y string
#time.ctime(timestamp) If no parameters are passed, the format string of the current time is directly returned
>>>time.ctime()
'Mon Jul 24 15:19:07 2017'
>>>time.ctime(1500000000)
'Fri Jul 14 10:40:00 2017' 
 

 

random module

 

>>> import random
#random decimal
>>> random.random() # decimals greater than 0 and less than 1
0.7664338663654585
>>> random.uniform(1,3) # decimals greater than 1 and less than 3
1.6270147180533838

#random integer
>>> random.randint(1,5) # An integer greater than or equal to 1 and less than or equal to 5
>>> random.randrange(1,10,2) # Odd numbers greater than or equal to 1 and less than 10


# randomly select one to return
>>> random.choice([1,'23',[4,5]]) # #1 or 23 or [4,5]
#Randomly select multiple returns, the number of returns is the second parameter of the function
>>> random.sample([1,'23',[4,5]],2) # #Any 2 combinations of list elements
[[4, 5], '23']


#shuffle the list order
>>> item=[1,3,5,7,9]
>>> random.shuffle(item) # shuffle the order
>>> item
[5, 1, 3, 7, 9]
>>> random.shuffle(item)
>>> item
[5, 9, 7, 1, 3]

sys module

The sys module is an interface for interacting with the python interpreter

sys.argv command line parameter list, the first element is the path of the program itself
sys.exit(n) Exit the program, exit(0) when exiting normally, exit sys.exit(1) with error
sys.version Get the version information of the Python interpreter
sys.path returns the search path of the module, using the value of the PYTHONPATH environment variable during initialization
sys.platform returns the OS platform name
import sys
try:
    sys.exit(1)
except SystemExit as e:
    print (s)

 

Guess you like

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