python攻城狮001--函数技巧\变量\包\标准库

某网站的一套python教程今天开始啃了,东西非常多,要时刻保持记录和沉淀.

函数技巧:

  1. 序列传参
def calc(a,b,c)
	return (a+b)*c

l=[1,5,10]
print(calc(1,5,10))
print(calc(*l))  # 结果相同
  1. 字典传参
    使用字典为参数赋值
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) #两个* 字典传参

  1. 返回值包含多个数据
    人员设备资产信息都获取到
    可以返回个字典/ 其他结构形式
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__ --文档注释,函数下写""" 文档注释"""

标准模块 第三方模块

datetime:

名字作用
timedelta对时间日期进行加减操作
datedate类表示一个日期
datetime.strftime将datetime对象格式化成字符串
datetime.strptime将字符串按照一定格式转成datetime对象
time表示时间的一个类
datetime.now系统当前时间
daydatetime对象的属性,类似还有minute,hour等
daysdatetime对象的属性,类似还有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进行安装


求点赞 . 求关注 . 求转发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MINUS大大

你的鼓励是我写下去的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值