Python commonly used modules are
- calendar
- time
- datetime
- timeit
- os
- shutil
- zip
- math
- string
- Theoretically, all the above modules should be imported first, and string is a special case
- calendar, time, datetime refer to Chinese meaning
calendar
- Calendar related modules
- Import before use
import calendar
Calendar: gets the calendar string of a year
- calendar parameter:
1.w = number of interval characters per date
2.l = number of rows occupied per week
3.c = number of interval characters between each month
cal = calendar.calendar(2019) print(cal)
Month() gets the calendar string of a month
- Format: calendar.month (year, month)
- Reverse value: string of month calendar
m3 = calendar.month(2019, 3) print(m3)
monthrange() gets the week start and days of a month
- Format: calendar.monthrange (year, month)
- Return value: tuple (start of week, total days)
- Note: Week default 0-6 means Monday to Sunday
w,t = calendar.monthrange(2019, 3) print(w) print(t)
monthcalendar() returns a list of matrices for each day of the month
- Format: calendar.monthcalendar (year, month)
- Return value: secondary list
- Note: there are no days in the matrix represented by 0
m = calendar.monthcalendar(2019, 3) print(type(m)) print(m)
prcal: print calendar
calendar.prcal(2018)
prmonth() prints the calendar for the entire month directly
- Format: calendar.prmonth (year, month)
- Return value: None
calendar.prmonth(2018, 3)
weekday() get the day of the week
- Format: calendar.weekday (year, month, day)
- Return value: the number corresponding to the day of the week
calendar.weekday(2018, 3, 26)
time module
time stamp
- Time units, which can be integers or floating-point numbers according to different languages
- It's the number of seconds from 0:00:00 on January 1, 1970 to now
- If the time indicated is from before 1970 or too far in the future, there may be an exception
- 32-bit operating system can support by 2038
UTC time
- UTC is also known as world coordinated time. It takes the time of Greenwich astronomical region in England as the reference time, also known as world standard time.
- China time is UTC+8 zone 8
Daylight saving time (it's obsolete, just know it)
- Daylight saving time is to speed up the time by an hour in summer, which is to urge everyone to go to bed early and get up early! 25 hours a day, 24 hours in essence
time tuples
A normal tuple containing time content
- time module needs to be imported separately
- Properties of time module
1.timezone: the number of seconds between the front time zone and UTC time. The interval without daylight saving time is - 28800 in the East 8 zone
2.ltzone gets the number of seconds between the current time zone and UTC time. When there is daylight saving time,
3.aylight measures whether the current state is daylight saving time. 0 means yes
print(time.daylight) print(time.altzone) print(time.timezone)
Get timestamp
time.time()
Local time, get the time structure of the current time - you can get the content of the corresponding attribute element through the dot operator
t = time.localtime() print(t.tm_hour)
ASCII () returns the time format after normal stringing of tuples
- Format: time.asctime (time tuple)
- Return value: String Tue Jun 6 11:11:00 2017
t = time.localtime() tt = time.asctime(t) print(type(tt)) print(tt) ----------------------- time.struct_time(tm_year=2019, tm_mon=5, tm_mday=22, tm_hour=14, tm_min=42, tm_sec=59, tm_wday=2, tm_yday=142, tm_isdst=0) <class 'str'> Wed May 22 14:42:59 2019
ctime: get the current time of stringing
t = time.ctime() print(type(t)) print(t) ------------------ <class 'str'> Wed May 22 15:03:37 2019
clock: get cpu time. Version 3.0-3.3 is used directly. There is a problem with the 3.6 call
- Sleep: sleep the program, and continue in n seconds
for i in range(10): print(i) time.sleep(1) ---------------------- 0 1 2 3 4 5 6 7 8 9
import time def p(): time.sleep(2) t0 = time.clock() p() t1 = time.clock() print(t1 - t0)
strftime: converts a time tuple to a custom string format
Format meaning remarks
%a local simplified week name
%A local full week name
%b local simplified month name
%B local full month name
%c local corresponding date and time representation
%d day of the month (01-31)
%H the hours of the day (24-hour system, 00-23)
%I hours of the day (12 hour system, 01-12)
%j day of the year (001-366)
%m month (01 - 12)
%M minutes (00 - 59)
%p corresponding notation 1 for local am or pm
%S seconds (01-61) Note 2
%U weeks of the year (00-53 Sunday is the beginning of the week) all days before the first Sunday are on week 0 note 3
%w day of the week (0 - 6, 0 is Sunday) Note 3
%W and% U are basically the same, but% w starts on Monday
%x local corresponding date
%X local corresponding time
%y year excluding Century (00 - 99)
%Y full year
%z use + HHMM or - HHMM for the time zone offset from Greenwich (H for decimal hours, M for decimal minutes)
%%% itself
# Expressed as 2019year4month18day 15:26format import time # Module needs to be imported first t = time.localtime() ft = time.strftime("%Y year%m month%d day %H:%M", t) print(ft) ------------------- 2019year05month22day 14:48
datetime module
- datetime provides operation and representation of date and time
import datetime,time # datetime common properties # datetime.date: An ideal and date to provide year, month, day attribute dt = datetime.date(2019, 5, 22) print(dt) print(dt.year) print(dt.month) print(dt.day) ----------------- 2019-05-22 2019 5 22
Common format, best memory
# datetime.timedelta # Represents a time interval from datetime import datetime, timedelta t1 = datetime.now() print( t1.strftime("%Y-%m-%d %H:%M:%S")) # td is the length of time in hours td = timedelta(hours=1) # After adding the time interval to the current time, format the time after one hour to output print( (t1+td).strftime("%Y-%m-%d %H:%M:%S")) -------------------- 2019-05-22 14:52:45 2019-05-22 15:52:45
timeit: a time measurement tool
import time # timeit - Time measurement tools # Measurement program run time interval experiment def p(): time.sleep(2.5) t1 = time.time() p() print(time.time() - t1) ---------------------- 2.5001275539398193
import timeit # Comparison of two methods of generating list forloop & List generation # If you simply compare the time to generate a list, it may be difficult to achieve c = ''' sum = [] for i in range(1000): sum.append(i) ''' # Calling code with timeit, executing100000Times, view run time t1 = timeit.timeit(stmt="[i for i in range(1000)]", number=100000) # Measurement code c execution100000Run results t2 = timeit.timeit(stmt=c, number=100000) print(t1) print(t2) # In this case, list generation is faster ------------------------- 4.318821437999759 7.035042254000018
# timeit can execute a function to measure the execution time of a function def doit(): num = 3 for i in range(num): print("Repeat for {}".format(i)) # Execute function, repeat10second t = timeit.timeit(stmt=doit, number=10) print(t) ------------------------ Repeat for 0 Repeat for 1 Repeat for 2 Repeat for 0 Repeat for 1 Repeat for 2 Repeat for 0 Repeat for 1 Repeat for 2 0.0001706670000203303
s = ''' def doIt(num): for i in range(num): print("Repeat for {0}".format(i)) ''' # implementdoIt(num) # setup is responsible for preparing environment variables # In fact, it is equivalent to creating a small environment for timeit # In the authoring environment, the order of code execution is roughly # ''' def doIt(num): ..... num = 3 doIt(num) ''' t = timeit.timeit("doIt(num)", setup=s + "num=3", number=10) print(t) ------------------------ Repeat for 0 Repeat for 1 Repeat for 2 Repeat for 0 Repeat for 1 Repeat for 2 Repeat for 0 Repeat for 1 Repeat for 2 Repeat for 0 Repeat for 1 Repeat for 2 Repeat for 0 Repeat for 1 Repeat for 2 Repeat for 0 Repeat for 1 Repeat for 2 Repeat for 0 Repeat for 1 Repeat for 2 Repeat for 0 Repeat for 1 Repeat for 2 Repeat for 0 Repeat for 1 Repeat for 2 Repeat for 0 Repeat for 1 Repeat for 2 0.0008973889998742379
datetime.datetime modular
- It's just a good time
- Class definition class datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])
- The year, month and day arguments are required.
- MINYEAR <= year <= MAXYEAR
- 1 <= month <= 12
- 1 <= day <= n
- 0 <= hour < 24
- 0 <= minute < 60
- 0 <= second < 60
- 0 <= microsecond < 10**
- Class method
- datetime.today(): returns the current local datetime. With tzinfo None
- datetime.fromtimestamp(time.time()).
- datetime.now([tz]): returns the current local date and time. If the optional parameter tz is None or there is no detailed description, this method will look like today()
- datetime.utcnow(): returns the current UTC date and time. If tzinfo None, it is similar to now()
- *** ***
- *** ***
- Instance method
- datetime.date(): returns the date object of the same date
- datetime.time(): returns the time object of the same minute, second and microsecond
- datetime.replace(kw): kw in [year, month, day, hour, minute, second, microsecond, tzinfo], similar to date
- Class properties
- datetime.min: datetime(MINYEAR, 1, 1).
- datetime.max: datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999).
- Instance property (read only)
- datetime.year: 1 to 9999
- datetime.month: 1 to 12
datetime.day: 1 to n - datetime.hour: In range(24). 0 to 23
- datetime.minute: In range(60).
- datetime.second: In range(60).
- datetime.microsecond: In range(1000000).