time&异常&import

一、高阶函数

1.1 filter 过滤

函数: filter(function or None, iterable) --> filter object

l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
# 判断一个元素是否为偶数
def is_odd(x):
   if x % 2 == 0:
       return True

第一种方式:
f_l = filter(is_odd,l
第二种方式: 
f_l = filter(lambda x:x%2==1,l)
          
第二个例子,过滤出全是小写的字母:
courses = ["java","Py","Groovy"]
def low(x):
   return x.islower()
c_l = filter(low,courses)
print(list(c_l))
1.2 reduce 聚合

函数: reduce(function, sequence[, initial])

from functools import reduce
nums = [1,2,3,4,5,6,7,8]
multi = 1
multiResult = reduce(lambda x,y:x*y,nums)
print(multiResult)

二、时间相关模块

2.1 常用时间模块

time: 时间

datetime: 日期

calendar: 日历

2.2 time

UTC:格林威治天文时间, 世界标准时间, UTC+8

DST:夏令时

import time
# 获取当前时间,时间戳,以s为单位
c = time.time()
# print(c)
# 将时间戳转换为UTC时间
g = time.gmtime(c)
print(g)
# 将时间戳转换成本地时间
l = time.localtime(c)
print(l)
# 将UTC时间转换成时间戳
c2 = time.mktime(l)
print(c2)
c3 = time.mktime((2019,12,17,20,59,59,1,351,0))
print(c3)
# 将时间戳可视化
c4 = time.ctime(c3)
print(c4)
# 将元组格式化字符串
s = time.strftime("%Y:%m:%d %H:%M:%S",l)
print(s)
# 将格式化时间字符串转换成时间元组
p = time.strptime("2019-12-17 21:07:50","%Y-%m-%d %H:%M:%S")
print(p)
# 休眠暂停
time.sleep(5)
2.3 datatime 日期模块
import datetime
# 获取当前时间
d1 = datetime.datetime.now()
print(d1)

# 将指定时间转换成字符串
d4 = d1.strftime("%Y-%m-%d")
print(d4)
# 根据元组获取指定时间
d2 = datetime.datetime(2019,12,17,21,19,59,124213)
print(d2)
# 把字符串转换成时间
d5 = datetime.datetime.strptime(d4,"%Y-%m-%d")
print(d5)
# 获取时间间隔
d6 = datetime.datetime(2019,12,15,21,23,59,8888)
d7 = datetime.datetime(2019,12,17,21,22,59,9999)
d8 = d7 - d6
print(d8,d8.days)
2.4 calendar 日历模块
import calendar
# 获取指定年和月日历形式
print(calendar.month(2019,12))
# 二维元组列表形式展现
print(calendar.monthcalendar(2019,12))
# 查看一年的日历
print(calendar.calendar(2019))

# 判断是否为闰年
print(calendar.isleap(2018))
# 年份区间段里, 有几个闰年
print(calendar.leapdays(2000,2020))

# 指定月份的天数,这个月第一天周几?
print(calendar.monthrange(2019,12))
# 判断当前日期星期数
print(calendar.weekday(2019,12,17))

三、导入

import urllib.request.urlopen
# 通过包名和模块名[pkg.module.xxx]
urllib.request.urlopen(None)

from urllib.request import urlopen
urlopen(None)

from urllib.request import *
urlretrieve()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值