某网站的一套python教程今天开始啃了,东西非常多,要时刻保持记录和沉淀.
函数技巧:
- 序列传参
def calc(a,b,c)
return (a+b)*c
l=[1,5,10]
print(calc(1,5,10))
print(calc(*l)) # 结果相同
- 字典传参
使用字典为参数赋值
def health_check(name,age,weight,hr,hbp,lbp,glu):
print('您的状况良好')
param={name:'a',age:19,weight:100,hr:70,hbp:11,lbp:10,glu:10}
health_check(**param) #两个* 字典传参
- 返回值包含多个数据
人员设备资产信息都获取到
可以返回个字典/ 其他结构形式
def get_getail_info():
dict1= {'employee':['张三'],'device':[],'asset':[],'project':[]}
return dict1
d= get_getail_info()
sal=d.get('employee')[0].get('salary')
print(sal)
环境变量
电脑的设置,或者下边的代码把路径加入环境变量中,这样能够导入包
import sys
sys.path
sys.path.append('路径')
模块属性:
dir
–列出对象的所有属性及方法help
--查看类,方法的帮助信息__name__
--模块的名称__file__
--文件全路径,可以深入研究看源码__doc__
--文档注释,函数下写""" 文档注释"""
标准模块 第三方模块
- 模块学习思路:
- os , os.path 模块使用os模块和sys模块的常用方法-百度经验
- 时间,日期模块 date time
- 第三方模块使用方法
datetime:
名字 | 作用 |
---|---|
timedelta | 对时间日期进行加减操作 |
date | date类表示一个日期 |
datetime.strftime | 将datetime对象格式化成字符串 |
datetime.strptime | 将字符串按照一定格式转成datetime对象 |
time | 表示时间的一个类 |
datetime.now | 系统当前时间 |
day | datetime对象的属性,类似还有minute,hour等 |
days | datetime对象的属性,类似还有minutes ,hours等 |
from datetime import datetime
import time
now_time = datetime.now()
print("now: {0}".format(now_time)) # now: 2019-11-29 09:19:31.541513
# 当前的日期
print('now day: {0}'.format(now_time.date())) # now day: 2019-11-29
# 当前的时间
print('now time: {0}'.format(now_time.time())) # now time: 09:19:31.541513
print('now day2: {0}'.format(datetime.today())) # now day2: 2019-11-29 09:19:31.541618
print('year: {0}'.format(now_time.year)) # year: 2019
print('month: {0}'.format(now_time.month)) # month: 11
print('day: {0}'.format(now_time.day)) # day: 29
print('microsecond: {0}'.format(now_time.microsecond)) # microsecond: 541513
print(dir(now_time))
'''['__add__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fold', 'fromisoformat', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz', 'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year']
------------------'''
# 获取到毫秒数
print(time.time()) # 1574990371.541736
# 时间休息两秒
time.sleep(2)
datetime 转换
from datetime import datetime, date, time, timedelta
# 1.自定义日期和时间
d = datetime(2020, 10, 30, 14, 5)
print(d) # 2020-10-30 14:05:00
d2 = date(2019, 3, 23) # 2019-03-23
print(d2)
t = time(9, 0)
print(t) # 09:00:00
print('--------------------')
# 2. 日期、时间与字符串之间的相互转换
# 字符串转换datetime对象
ds = '2018/10/3T13:42:09'
ds_t = datetime.strptime(ds, '%Y/%m/%dT%H:%M:%S')
print(ds_t.second) # 2019-11-29 10:13:43.076504
# datetime对象转换成字符串
print('--------------')
n = datetime.now() # 2019-11-29 10:13:43.076991
print(n)
n_str = n.strftime('%m')
print(n_str) # 11
# 3. datetime之间的加减操作
print('-------------')
n = datetime.now()
print(n) # 2019-11-29 11:15:07.067026
n_next = n + timedelta(days=5, hours=42, minutes=4, seconds=56, microseconds=444)
print(n_next) # 2019-12-06 05:20:03.067470
print('---------------')
# 时间的减法
d1 = datetime(2018, 10, 15)
d2 = datetime(2018, 11, 12)
rest = d2 - d1
print(type(rest)) # <class 'datetime.timedelta'>
print(rest.days) # 28
rest2 = d1 - d2
print(rest2.days) # -28
pipy.org的标准库
从python standard library
可以查到python自带的标准库
例如:
-
Django --一个自带电池的web开发框架
-
Flask – 简单易用,快速上手的微型web开发框架
-
mysqlclient --使用python操作mysql数据的工具库
-
从
http://pypi.org
可以查询第三方库. -
上述页面上有个
programming language
写明了兼容哪些版本 -
上述页面
Meta
处值得是协议:BSD\GTL -
docs
指文档,看操作说明 -
Homepage
指这个库的主页 -
找
github图标
看源码(github中有release指已经发布的)python setup.py install
进行安装
求点赞 . 求关注 . 求转发