1 模块介绍
标准库和第三方模块
(1) 标准库
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
print(sys.path)
"""
'/Users/develop/Documents/Python_study/s14/day2',
'/Users/develop/Documents/Python_study/s14',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
'/Users/develop/Documents/Python_study/s14/venv/lib/python3.7/site-packages',
'/Users/develop/Documents/Python_study/s14/venv/lib/python3.7/site-packages/setuptools-40.8.0-py3.7.egg',
'/Users/develop/Documents/Python_study/s14/venv/lib/python3.7/site-packages/pip-19.0.3-py3.7.egg']
"""
python的标准库一般都存在/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7中例如socket.py网络相关,re模块是正则表达式
print(sys.argv[2])在终端输入develop$ python3 文件名.py 1 2 3会输出结果2
'''
输入模块.py 1 2 3
会打印出2
zzhdeMacBook-Pro:day2 develop$ python3 模块.py 1 2 3
['/Users/develop/Documents/Python_study/s14/day2', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']
2
'''
os.mkdir("new")会在当前目录创建一个new的文件夹
(2) 第三方模块
'''
#引用第三方 先从当前目录下选择
# 1.放在/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7中变为自己写的模块
# 2.添加路径
'''
例如:在当前目录创建了一个文件guess_age.py复制一份放于/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7
import guess_age.py#直接会读取guess_age.py里面的程序
'''
guess_age:50
think bigger!
guess_age:80
think smaller...
guess_age:60
think smaller...
you have tried too many times...fuck off
'''
二 pyc
python是解释性语言,但是生成的pyc是干嘛的呢,c应该是complied的缩写才对啊
计算机不能够识别高级语言,所以通过运行一个高级语言时候需要翻译为计算机识别的语言
在运行python的过程跟Jave一样先编译后解释。python跟Java是基于虚拟机的语言
pyc文件就是编译器真正编译的结果
过程如下:
python运行时候,编译结果会存放在内存的PyCodeObject中,当python运行结束时,pyhton
编译器会将PyCodeObject写回到pyc中。
当python程序第二次运行时,程序去硬盘中寻找pyc文件,如果找到直接载入,否则重复以上过程(第一次没找到就重复)
如果pyc已经存在,修改了源代码,怎么办。是从更新时间来判断,是否重新编译生成pyc
例如import guess_age之后会在/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7'/__pycache__生成guess_age.cpython-37.pyc
三 字符串
name = "my name is Jerry"
print("-----语法----")
print("capitalize()")
print(name.capitalize()) #首字母大写其他都会变小写 结果:My name is jerry
print("count")
print(name.count("e")) #e 出现的次数
print("center")
print(name.center(50,"*"))# 总共打印50个字符 不够的把name放中间,两边填充 *:*****************my name is Jerry*****************
print("endswith")
print(name.endswith("rry")) #判断是否以rry结尾 返回True
print("expandtabs()")
name1 = "my \tname is Jerry"
print(name1.expandtabs(tabsize=10)) #字符串有tab的时候,把tab转为多少个空格。结果:my name is Jerry
print("find")
print(name.find("is"))#返回下标 8
print(name[name.find("is"):10])#跟list类似可以切片。结果:is
print("fomat")
name2 = "my name is {name} and i am {age}"
print(name2.format(name="Jerry",age=25))
print(name2.format_map({"name":"Jerry","age":25}))#字典的形式
print("index")
print("isalnum")#是不是阿拉伯数字(英文字符加0-9数字)
print("isalpha")#是否纯英文字符
print("isdigit是否整数")
print("isidentifier是不是合法的变量名")
print("islower是不是小写")
print("isnumeric是否为数字")
print("33.3".isnumeric())#只有数字
print("33.3".isdigit())#常用
print("isspaces是否为空格")
print("istitle是否为开头大写")
print("isupper是否所有为大写")
print("join")
name4 = "my name is Jerry"
print(name4.join(["1","2","3","4"]))
print("+".join(["1","2","3"]))
print("ljust")
print(name4.ljust(50 ,"*"))#总共输出50个字符不足的话后面不* 与rjust相反
print("lower")#把大写变为小写
print("upper")#把小写变为大写
print("lstrip")#strip会去掉两边空格/换行 lstrip是去掉左边 rstrip是去掉右边
print("maketrans把对应的字符互相交换,相同的对应最后一个")
p = str.maketrans("jergg","12345")
print("jergy".translate(p))
print("replace替换 替换默认全部可传替换个数")
print("Jerrry".replace("r","R",2))
print("rfind从左找到最后面的值的下标")
print("Jerry".rfind("r"))
print("split按什么截取,默认是空格")
print("Jerry is".split())
print("1+2+3+4".split("+"))
print("splitlines按换行截取")
print("1+2\n+3+4".splitlines())
print("startswith按什么开始")
print("swapcase大小写互换")
print("Jerry".swapcase())
print("title每个字符的首字母变大写")
print("my name is".title())
print("zfill")
print("Jerry".zfill(50))