一、 python学习之模块
今天学习了模块的内容,模块就是完成某一功能的一段代码,可以是几个文件的组合,单个文件也可以成为模块。python 有很多功能强大且丰富的标准库和第三方库作支撑,基本可以实现任何功能。
1. 标准库 :就是不用安装,直接导入的库就叫标准库。是python最常用的功能,比如sys和os模块。
1-1 sys模块
import sys
print(sys.path) #相对路径
print(sys.argv) #参数
1-2 os模块
import os
cmd_dir = os.system("dir") #将当前目录下的内容显示到屏幕,并不赋值
print('--->',cmd_dir) #输出为--->0
cmd_new = os.popen("dir").read() #将当前目录下的内容显示到屏幕,并赋值
print(cmd_new)
2 第三方库:必须安装才能使用的库
注意:在程序中导入模块要在开头用import;
在login.py 中引用user.py程序,且这两个程序在同一个目录下,则可在login.py文件中写: import user #不用写 .py
二、 python学习之.pyc的作用
我们在运行程序的时候,会发现会生成一个跟运行文件同名的.pyc字节码文件,这就类似于Java文件在运行是生成的javac一样。如果你这个py文件有改变,python解释器会检查pyc文件中的生成时间,对比py文件的修改时间,如果py更新,那么就生成新的pyc。否则下次运行这个程序直接调用pyc,而不调用py文件。
三、 python学习之列表的使用
3-1 元组 是特殊的列表,一旦定义好了,就不能修改值了。
3-2 读取列表元素
books
=
["Chinese"
,
"English"
,"Math"
]
>>>books[0]
"Chinese"
>>>books[-1] #倒叙
"Math"
3-3切片
books
=
["Chinese"
,
"English"
,"Math","draw","Masterpiece"
]
>>>books[1:3] #包括1,不包括3
["English"
,"Math"
]
>>>books[1:-1] #包括1,不包括-1
[
"English"
,"Math","draw"
]
>>>books[0::2] #等同于books[::2],每隔一个取一个
[
"Chinese"
,"Math",
"Masterpiece"
]
3-4追加 append()
>>>books.append("Geography")
>>>books
["Chinese"
,
"English"
,"Math","draw","Masterpiece","Geography"
]
3-5 指定位置插入 insert()
>>>books.insert(1,"insert_date")
>>>books
["Chinese"
,"insert_date",
"English"
,"Math","draw","Masterpiece","Geography"
]
3-6 修改
>>>books[1]="修改的数据"
>>>books
["Chinese"
,"修改的数据",
"English"
,"Math","draw","Masterpiece","Geography"
]
3-7 删除
>>>del books[1] #删除某个元素
>>>books
["Chinese"
,
"English"
,"Math","draw","Masterpiece","Geography"
]
>>>del books
["Chinese"
,
"English"
,"Math","draw","Masterpiece","Geography"
]
3-8 扩展--在后面增加一个其他列表
>>>books=["Chinese"
,
"English"
,"Math","draw"
]
>>>book.extend(["Masterpiece","Geography"])
>>>books=["Chinese"
,
"English"
,"Math","Draw","Masterpiece","Geography"
]
3-9复制
>>>books_new=books.copy()
>>>books_new
["Chinese"
,
"English"
,"Math","Draw","Masterpiece",
]
3-10统计
>>>books.count("English")
1
3-11 排序和反转
>>>books.sort() #排序
["Chinese"
,"Draw",
"English"
,"Geography",,"Math"
]
>>>books.reverse()
["Geography"
,"Draw",
"Math"
,"English","Chinese"
]
3-12 获取下标
>>>books.index("Draw")
3
四、python学习之字典
4-1 特点:是无序的并且唯一的
4-2 增加
>>>students={name:"lilei",
age:"16"}
>>>students["sex"]="男"
>>>students
{name:"lilei",sex:"男",age:"16"}
4-3修改
>>>students["sex"]="man"
>>>students
{name:"lilei",sex:"man",age:"16"}
4-4 删除
>>>students.pop("sex") #方法1
"man"
>>>students
{name:"lilei",age:"16"}
>>>del students["age"] #方法2
{name:"lilei"}
4-5查找
>>>user={name:"lilei",sex:"男",age:"16"}
>>>user.get("name")
"lilei"
>>>"sex" in user
True
4-6 多级嵌套
>>>class_5={class1:{
name:"lilei",
sex:"男",
age:"16"}
class2:{
name:"lucy",
sex:"女",
age:"16"}
}
>>>class_5["class2"]["name"]
"lucy"