主要内容:1. 模块的简单认识2. collections模块3. time时间模块4. random模块5. os模块6. sys模块 一. 模块的简单认识什么是模块. 模块就是我们把装有特定功能的代码进行归类的结果. 从代码编写的单位来看我们的程序, 从小到大的顺序: 一条代码 < 语句句块 < 代码块(函数, 类) < 模块. 我们目前写的所有的py文件都是模块. 引入模块的⽅方式: 1. import 模块 2. from xxx import 模块 ☆ 对于模块方面,可以自己安…
一.模块1.定义模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test)包:用来从逻辑上组织模块的,本质就是一个目录(必须带有一个__init__.py文件)2.导入方法import module_nameimport module_name,module2_namefrom module_alex import *from module_alex import m1,m2,m3from…
collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict.namedtuple.OrderedDict等. namedtuple: 生成可以使用名字来访问元素内容的tuple # tuple可以用来表示一个坐标 from collections import namedtuple point = namedtuple('poi',['x','y','z']…
简要介绍一下各种集合: 列表.元组.字典.集合(含frozenset).字符串.堆栈(如手枪弹夹:先进后出).队列(如马克沁机枪的弹夹:先进先出) 1.collections 1)queue 队列介绍 如马克沁机枪的弹夹:先进先出,还如排队买票,先排的先买! import queue q = queue.Queue() q.put([1, 2, 3]) q.put(4) q.put(5) print(q) # 队列是一个黑盒子,你看不到里面放着什么! q.get() # 得到[1, 2, 3]…
一.random模块 import random print(random.random())#(0,1)----float 大于0且小于1之间的小数 print(random.randint(1,3)) #[1,3] 大于等于1且小于等于3之间的整数 print(random.randrange(1,3)) #[1,3) 大于等于1且小于3之间的整数 print(random.choice([1,'23',[4,5]]))#1或者23或者[4,5] print(random.sample([1…
######################################################### 模块time ####################################### 多用于时间戳与字符串的转换 import timeprint(time.time()) #出来的结果是现在距离1970年1月1日过去了多少秒print(time.localtime()) ####输出当前系统的时间time.sleep(0.1) #########d等待3秒print("h…
os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functionality. 这个模块提供了一种方便的使用操作系统函数的方法. sys: This module provides access to some variables used or maintained by the interpreter and to functions that intera…
os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functionality. 这个模块提供了一种方便的使用操作系统函数的方法. sys: This module provides access to some variables used or maintained by the interpreter and to functions that intera…
python os和sys模块使用 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.curdir 返回当前目录: ('.') os.pardir 获取当前目录的父目录字符串名:('..') os.makedirs('dirname1/dirname2') 可生成多层递归目录 os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录…
os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functionality. 这个模块提供了一种方便的使用操作系统函数的方法. sys: This module provides access to some variables used or maintained by the interpreter and to functions that intera…