1.包与模块的定义与导入

1.1.什么是python的包与模块

  • 包就是文件夹,包中还可以有包,也就是子文件夹
  • 一个个python文件模块


1.2.包的身份证

__init__.py是每一个python包里面必须存在的文件,这个文件里面可以没有任何内容


1.3.如何创建包

  • 要有一个主题,明确功能,方便使用
  • 层次分明,调用清晰
  • 文件里面要有包的身份认证文件,即__init__.py文件,证明该文件夹是一个包


1.4.包的导入

虽然说图示animal包里面还有子包子文件,但是import animal只能拿到当前包__init__.py里面的功能;当import具体文件时,比如import test1.py只能拿当前模块(当前文件)中的功能,同级animal包中的功能无法访问使用


1.5.模块的导入

2.第三方包

  • 阿里云 http://mirrors.aliyun.com/pypi/simple/
  • 豆瓣http://pypi.douban.com/simple/
  • 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
  • 中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
  • 华中科技大学http://pypi.hustunique.com/
  • 使用格式:如安装ipython版本为19.0.1的包pip install -i http://mirrors.aliyun.com/pypi/simple/ ipython==19.0.1

3.Python的datetime与time

  • datetime和time这两个包为python中常用的两个时间包
  • datetime常用于对日期的处理
  • time常用于对时间、计时等处理

3.1.datetime

  • 日期与时间的结合体-date and time
  • 获取当前时间
  • 获取时间间隔
  • 将时间对象转成时间字符串
  • 将字符串转成时间类型

3.1.1.datetime包的常用功能

获取当前时间

获取时间间隔

 1 # coding:utf-8
2
3 from datetime import datetime
4 from datetime import timedelta
5
6 now=datetime.now()
7 print(now,type(now)) #2021-12-27 16:19:18.586413 <class 'datetime.datetime'>
8
9 three_days=timedelta(days=3)
10 print(type(three_days)) #<class 'datetime.timedelta'>
11
12 after_three_day=now+three_days
13 print(after_three_day) #2021-12-30 16:19:18.586413

时间对象转字符串

1 # coding:utf-8
2
3 from datetime import datetime
4
5 now=datetime.now()
6 now_str=now.strftime('%Y-%m-%d %H:%M:%S')
7 print(now_str,type(now_str)) #2021-12-27 16:28:06 <class 'str'> 日期字符串无法实现日期的加减法,必须转成日期对象类型才能实现日期的加减法

时间字符串转时间类型

 1 # coding:utf-8
2
3 from datetime import datetime
4 from datetime import timedelta
5
6 now=datetime.now()
7 now_str=now.strftime('%Y-%m-%d %H:%M:%S')
8 print(now_str,type(now_str)) #2021-12-27 16:37:11 <class 'str'>
9
10 now_obj=datetime.strptime(now_str,'%Y-%m-%d %H:%M:%S')
11 print(now_obj,type(now_obj)) #2021-12-27 16:37:11 <class 'datetime.datetime'>,转成对象的时候,后面的格式必须得跟字符串的格式匹配
12
13 three_days=timedelta(days=3)
14 after_three_day=now_obj+three_days
15 print(after_three_day,type(after_three_day)) #2021-12-30 16:37:11 <class 'datetime.datetime'>

3.1.2.python的常用时间格式化符号

1 # coding:utf-8
2
3 from datetime import datetime
4
5 now=datetime.now()
6 now_str=now.strftime('%Y-%m-%d %H:%M:%S %p %j %U %A')
7 print(now_str,type(now_str)) #2021-12-27 16:45:31 PM 361 52 Monday <class 'str'>

3.2.time

3.2.1认识时间戳

  • 1970年1月1日00时00分00秒至今的总毫秒(秒)数
  • 使用timestamp代表时间戳
  • 时间戳是float类型的

3.2.2认识python的time模块与常用方法

生成时间戳函数time

1 # coding:utf-8
2 import time
3
4 now=time.time()
5 print(now,type(now)) #1640595949.671707 <class 'float'>

获取本地时间函数localtime

timestamp不传代表当前时间

1 # coding:utf-8
2 import time
3
4 now=time.time()
5 time_obj=time.localtime(now)
6 print(time_obj,type(time_obj)) #time.struct_time(tm_year=2021, tm_mon=12, tm_mday=27, tm_hour=17, tm_min=6, tm_sec=43, tm_wday=0, tm_yday=361, tm_isdst=0) <class 'time.struct_time'>

localtime对应字段介绍

1 # coding:utf-8
2 import time
3
4 now=time.localtime()
5 print(now) #time.struct_time(tm_year=2021, tm_mon=12, tm_mday=27, tm_hour=17, tm_min=1, tm_sec=12, tm_wday=0, tm_yday=361, tm_isdst=0)

暂停函数sleep

1 # coding:utf-8
2 import time
3
4 for i in range(10):
5 print(i)
6 time.sleep(1)

time中的strftime与strptime

 1 # coding:utf-8
2 import time
3
4 now=time.time()
5 print(now,type(now)) #1640596914.3263566 <class 'float'>
6
7 #now_str=time.strftime('%Y-%m-%d %H:%M:%S',now) #TypeError: Tuple or struct_time argument required 报错,因为now时间戳是浮点型,不是time.localtime对应的时间类型
8 print(type(time.localtime())) #<class 'time.struct_time'>
9 now_str=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
10 print(now_str,type(now_str)) #2021-12-27 17:21:54 <class 'str'>

1 # coding:utf-8
2 import time
3
4 now=time.time()
5 now_str=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
6 now_obj=time.strptime(now_str,'%Y-%m-%d %H:%M:%S')
7 print(now_obj,type(now_obj)) #time.struct_time(tm_year=2021, tm_mon=12, tm_mday=27, tm_hour=18, tm_min=5, tm_sec=40, tm_wday=0, tm_yday=361, tm_isdst=-1) <class 'time.struct_time'>

3.2.3datetime转时间戳与datetime时间戳转时间对象的方法

datetime转时间戳

1 # coding:utf-8
2
3 from datetime import datetime
4
5 now=datetime.now()
6 now_stamp=datetime.timestamp(now)
7 print(now_stamp,type(now_stamp)) #1640600288.843319 <class 'float'>

datetime时间戳以及time时间戳转时间对象

 1 # coding:utf-8
2
3 from datetime import datetime
4 import time
5
6 now=datetime.now()
7 now_stamp=datetime.timestamp(now)
8 now_stamp_obj=datetime.fromtimestamp(now_stamp)
9 print(now_stamp_obj,type(now_stamp_obj)) #2021-12-27 18:56:15.849622 <class 'datetime.datetime'>
10
11 now_time=time.time()
12 now_time_obj=datetime.fromtimestamp(now_time)
13 print(now_time_obj,type(now_time_obj)) #2021-12-27 18:56:15.849623 <class 'datetime.datetime'>

4.Python内置库os与sys模块

4.1.os模块

4.1.1.os的文件与目录函数介绍

 1 # coding:utf-8
2
3 import os
4
5 current_path=os.getcwd()
6 print(current_path)
7
8 new_path='%s/test1/test2' % current_path
9 os.makedirs(new_path)
10
11 os.removedirs('test1/test2')
12 os.rename('test1','test')
13 data=os.listdir(current_path)
14 print(data)

4.1.2.os.path模块常用函数介绍


4.2.sys模块

Python基础入门(8)- Python模块和包的更多相关文章

  1. python基础教程总结9——模块,包,标准库

    1. 模块 在python中一个文件可以被看成一个独立模块,而包对应着文件夹,模块把python代码分成一些有组织的代码段,通过导入的方式实现代码重用. 1.1 模块搜索路径 导入模块时,是按照sys ...

  2. Python基础入门知识点——Python中的异常

    前言 在先前的一些章节里你已经执行了一些代码,你一定遇到了程序“崩溃”或因未解决的错误而终止的情况.你会看到“跟踪记录(traceback)”消息以及随后解释器向你提供的信息,包括错误的名称.原因和发 ...

  3. Python基础入门教程

    Python基础入门教程 Python基础教程 Python 简介 Python环境搭建 Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 Python 循 ...

  4. Python基础入门总结

    Python基础入门教学 基础中的基础 列表.元组(tuple).字典.字符串 变量和引用 函数 python视频教程下载 基础中的基础 解释型语言和编译型语言差距: Python概述 解释器执行原理 ...

  5. 【Python教程】《零基础入门学习Python》(小甲鱼)

    [Python教程]<零基础入门学习Python>(小甲鱼) 讲解通俗易懂,诙谐. 哈哈哈. https://www.bilibili.com/video/av27789609

  6. 《零基础入门学习Python》【第一版】视频课后答案第001讲

    测试题答案: 0. Python 是什么类型的语言? Python是脚本语言 脚本语言(Scripting language)是电脑编程语言,因此也能让开发者藉以编写出让电脑听命行事的程序.以简单的方 ...

  7. 零基础入门学习Python(1)--我和Python的第一次亲密接触

    前言 最近在学习Python编程语言,于是乎就在网上找资源.其中小甲鱼<零基础入门学习Python>试听了几节课,感觉还挺不错,里面的视频都是免费下载,小甲鱼讲话也挺幽默风趣的,所以呢,就 ...

  8. 学习参考《零基础入门学习Python》电子书PDF+笔记+课后题及答案

    国内编写的关于python入门的书,初学者可以看看. 参考: <零基础入门学习Python>电子书PDF+笔记+课后题及答案 Python3入门必备; 小甲鱼手把手教授Python; 包含 ...

  9. 学习《零基础入门学习Python》电子书PDF+笔记+课后题及答案

    初学python入门建议学习<零基础入门学习Python>.适合新手入门,很简单很易懂.前一半将语法,后一半讲了实际的应用. Python3入门必备,小甲鱼手把手教授Python,包含电子 ...

  10. [新手必备]Python 基础入门必学知识点笔记

    Python 作为近几年越来越流行的语言,吸引了大量的学员开始学习,为了方便新手小白在学习过程中,更加快捷方便的查漏补缺.根据网上各种乱七八糟的资料以及实验楼的 Python 基础内容整理了一份极度适 ...

随机推荐

  1. 生产调优1 HDFS-核心参数

    目录 1 HFDS核心参数 1.1 NameNode 内存生产配置 问题描述 hadoop-env.sh中配置 1.2 NameNode 心跳并发配置 修改hdfs-site.xml配置 1.3 开启 ...

  2. day14函数递归调用

    day14函数递归调用 1.装饰器叠加 def deco1(func1): def wrapper1(*args,**kwargs): print('=====>wrapper1 ') res1 ...

  3. C++之无子数

    题目如下: 1 #include <iostream> 2 3 using namespace std; 4 5 6 bool isThisNumhaveChild(int num); 7 ...

  4. ping (网络诊断工具)

    Ping是Windows.Unix和Lnix系统下的一个命令,ping也属于一个通信协议,是TCP/IP协议的一部分,利用Ping命令可以检查网络是否连通,可以很好地帮助我们分析和判定网络故障.应用格 ...

  5. Java中的Date和时区转换

    1.Date中保存的是什么 在java中,只要我们执行 Date date = new Date(); 就可以得到当前时间.如: Date date = new Date(); System.out. ...

  6. SpringIOC原理

    IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用 ...

  7. matplotlib 画图中图和次坐标轴

    一: fig.add_axes 画图中图 fig = plt.figure() x = np.arange(1, 9, 1) y = np.linspace(1, 10, 8) left, botto ...

  8. python中的虚拟环境(在jupyter和pycharm中的使用)

    1.通过anaconda新建虚拟环境 创建虚拟环境:conda create -n your_env_name python=3.6 激活虚拟环境:activate your_env_name(虚拟环 ...

  9. 程序员Meme 第00期

  10. java 多线程,单例模式类(创建对象)最优写法

    单例模式 单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式涉及到一个单一的类,该类负责创 ...