Python-常用包 calendar 、time、datetime、timeit

常用模块

  • calendar
  • time
  • datetime
  • timeit
  • os
  • shutil
  • zip
  • math
  • string

上述所有模块使用理论上都应该先导入,string是特例
calendar,time,datetime的区别参考中文意思

1. calendar模块

利用help命令查看calendar 包中的功能

FUNCTIONS
1-calendar =formatyear(theyear, w=2, l=1, c=6, m=3) method of TextCalendar instance
prcal 和calendar用法相似,直接打印某一年日历

获取一年的日历字符串,参数为:

  • w = 每个日期之间的间隔字符数
  • l = 每周所占用的行数
  • c = 每个月之间的间隔字符数
  • m=每行显示的月份数
    import calendar
    cal = calendar.calendar(2019,2,1,6,4)
    print (cal)
    print(type(cal))

输出:
在这里插入图片描述

2-isleap和leapdays

isleap判断一年是否为闰年
leapdays: 获取指定年份之间的闰年的个数

leapdays(y1, y2)
    Return number of leap years in range [y1, y2).
    Assume y1 <= y2.
import calendar

cal = calendar.isleap(2019)
print(cal)

cal2 = calendar.leapdays(2000,2019)
print(cal2)

输出:

False
5
3-month
prmonth用法同month, 直接打印某一个月的日历

获取某个月的日历字符串
格式:calendar.month(年,月)
回值:月日历的字符串

import calendar

cal = calendar.month(2019,9)
print (cal)

输出:

在这里插入图片描述

4-monthrange

获取某一年的某一个月从几号开始以及天数
monthrange(年份,月数)
返回值(周几开始,天数)0-6周一到周日

import calendar

cal = calendar.monthrange(2019,9)
w,d = calendar.monthrange(2019,9)
print (cal)
print(type(cal))
print("开始为:{0},天数为:{1}".format(w,d))

输出:

(6, 30) 
<class 'tuple'>   
开始为:6,天数为:30
5-monthcalendar

返回某一年某月的天数的二级列表

import calendar

cal = calendar.monthcalendar(2019,9)
print (cal)
print(type(cal))

输出:

[[0, 0, 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, 0, 0, 0, 0, 0, 0]]
<class 'list'>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值