Python commonly used standard library summary

Strive to finish and keep updating

Article Directory

1.sys

1. Identify the operating system
import sys
print(sys.pltfrom)

Os .os

Three.math

Four.random

Five.time

1. The time function is used to return the current timestamp (the total number of seconds since Greenwich Mean Time)
import time
now=time.time()
print(now)

1611303060.773287

2. The localtime function is used to convert the timestamp format into local time and return (struct_time) object
import time
now=time.localtime()
print(now)

time.struct_time(tm_year=2021, tm_mon=1, tm_mday=22, tm_hour=16, tm_min=14, tm_sec=30, tm_wday=4, tm_yday=22, tm_isdst=0)

3. The mktime function receives struct_time and returns a floating-point number representing time in seconds
import time
now=time.localtime()
print(now)
s=time.mktime(now)
print(s)

time.struct_time(tm_year=2021, tm_mon=1, tm_mday=22, tm_hour=16, tm_min=21, tm_sec=1, tm_wday=4, tm_yday=22, tm_isdst=0)
1611303661.0

4. The gmtime function can convert the timestamp to struct_time in the 0 time zone
import time
now=time.time()
print(now)
s=time.gmtime(now)
print(s)

1611303823.960081
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=22, tm_hour=8, tm_min=23, tm_sec=43, tm_wday=4, tm_yday=22, tm_isdst=0)

5. The asctime function can receive struct_time and return a readable time form
import time
now=time.time()
print(now)
s=time.gmtime(now)
print(s)
new=time.asctime(s)
print(new)

1611303959.5288363
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=22, tm_hour=8, tm_min=25, tm_sec=59, tm_wday=4, tm_yday=22, tm_isdst=0)
Fri Jan 22 08:25:59 2021

6. The ctime function can convert the timestamp into a readable form
import time
now=time.time()
print(now)
s=time.ctime(now)
print(s)

1611304057.0304146
Fri Jan 22 16:27:37 2021

7. The sleep function delays the running of the calling thread, the parameter is seconds
import time
print("start:",time.ctime())
time.sleep(9)
print("end:",time.ctime())

start: Fri Jan 22 16:29:58 2021
end: Fri Jan 22 16:30:07 2021

8. The strftime function receives a time tuple and returns a readable local time
9. The strptime function can parse time characters into time tuples

Six.datetime

1.date object
import datetime
print(datetime.MAXYEAR)   #支持的最大年份
print(datetime.MINYEAR)   #支持的最小年份
print(datetime.date.today())  #today返回当天日期
print(datetime.date.today().weekday())  #weekday放回当天的星期
print(datetime.date.today().isoformat())  #返回IOS格式

9999
1
2021-01-22
4
2021-01-22

2.time object
import datetime
print(datetime.time())	   #默认时间
print(datetime.time.max)   #支持的最大时间
print(datetime.time.min)   #支持的最小时间

00:00:00
23:59:59.999999
00:00:00

3.datetime object

datetime=date+time, return both date and time

import datetime
today=datetime.datetime.today()
print(today)
print(datetime.datetime.now())   #与today用法相同
print(datetime.datetime.utcnow())  #0时区时间
now=datetime.datetime.now()
print(now.date())   #date对象返回日期
print(now.time())   #time对象返回时间

2021-01-22 18:22:42.380595
2021-01-22 18:22:42.382589
2021-01-22 10:22:42.382589
2021-01-22
18:22:42.382589

4.timedelta object

Represents the time difference

import datetime
dt1=datetime.datetime.now()
dt2=datetime.timedelta(weeks=2)+dt1
print(dt1)
print(dt2)
print(dt1-dt2)

2021-01-22 18:34:31.429461
2021-02-05 18:34:31.429461
-14 days, 0:00:00

5.tzinfo object

Seven.calendar

1.calendar.isleap is used to judge leap years
import calendar
print(calendar.isleap(2000))
print(calendar.isleap(2018))

True
False

2.canlender.leapdays is used to return the total number of leap years between two years
import calendar
print(calendar.leapdays(1900,2000))

24

3.canlender.month method is used to make a calendar

Four parameters: theyear, themonth, w (character spacing), l (line spacing)

import calendar
print(calendar.month(2021,1))

Insert picture description here

4. The calendar.monthcalender method returns a single-level nested list, and each list contains a week
import calendar
print(calendar.monthcalendar(2021,1))

[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]]

5. The calendar.monthrange method returns a tuple of two integers. The first number indicates the day of the week on the first day of the month, and the second number indicates the number of days to change the month.
import calendar
print(calendar.monthrange(2021,1))

(4, 31)

6.calendar.weekday method returns the week code of a given date
import calendar 
print(calendar.weekday(2021,1,1))

4

7.calendar.calendar returns the entire year calendar
import calendar 
print(calendar.calendar(2021))

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_50216270/article/details/112985706