@导入模块时,会先搜索目前路径的同名py文件,再去全局环境变量找
@看模块的环境变量
import sys print(sys.path)
@site-package存放第三方库,可以把自己建的拷贝在里面。
@sys.path是python的搜索模块的路径集,sys.argv是文件的相对路径(包括文件名),都是列表。
@sys.argv可以传参
@三元运算
a,b,c=1,3,5 a=b if a>b else c
@!!!!编码
在python2默认编码是ASCII, python3里默认是unicode,转为其他的就用encode;
encode,在转码的同时还会把string 变成bytes类型,decode在解码的同时还会把bytes变回string。
msg = "我爱北京天安门" msg_gb2312 = msg.encode("gb2312") #默认就是unicode,不用再decode,喜大普奔 gb2312_to_unicode = msg_gb2312.decode("gb2312") gb2312_to_utf8 = msg_gb2312.decode("gb2312").encode("utf-8")
查看默认编码
sys.getdefaultencoding()
@列表操作
1.根据位置删除并不用
del list[1]
2.末尾删除并用
list.pop(1)#效果同上,但是可以赋值给变量,默认弹出栈顶元素;
3.根据值删除并不用
list.remove('lalal')
4.根据变量删除值并用
m='lalala' list.remove(m) print(m)
5.翻转(注意reverse()后只能打印原变量,直接输出reverse是none)
>>> m=[1,4,123] >>> m.reverse() >>> print(m) [123, 4, 1] >>> n=m.reverse() >>> print(n) None
6.复制
list[:]
@元组只有count和index两个方法
@带序号遍历
for index,i in enumerate(list)
@字典删除
info.pop("stu1101") #标准删除姿势 del info['stu1103'] #换个姿势删除 info.popitem()#随机删除
@字典取值
dict.setdefault("stu1106","Alex")#有则取值,无责增加 #updat,有则改,没有则加 >>> info {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'} >>> b = {1:2,3:4, "stu1102":"龙泽萝拉"} >>> info.update(b) >>> info {'stu1102': '龙泽萝拉', 1: 2, 3: 4, 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
@创建字典
1.通过关键字dict和关键字参数创建
>>> dic = dict(spam = 1, egg = 2, bar =3)
>>> dic
{'bar': 3, 'egg': 2, 'spam': 1}
2.通过二元组列表创建
>>> list = [('spam', 1), ('egg', 2), ('bar', 3)]
>>> dic = dict(list)
>>> dic
{'bar': 3, 'egg': 2, 'spam': 1}
3.dict和zip结合创建
>>> dic = dict(zip('abc', [1, 2, 3]))
>>> dic
{'a': 1, 'c': 3, 'b': 2}
4.通过字典推导式创建
>>> dic = {i:2*i for i in range(3)}
>>> dic
{0: 0, 1: 2, 2: 4}
5.通过dict.fromkeys()创建
通常用来初始化字典, 设置value的默认值
>>> dic = dict.fromkeys(range(3), 'x')
>>> dic
{0: 'x', 1: 'x', 2: 'x'}
6.其他
>>> list = ['x', 1, 'y', 2, 'z', 3]
>>> dic = dict(zip(list[::2], list[1::2]))
>>> dic
{'y': 2, 'x': 1, 'z': 3}
7.创建有序字典
collections里面的OrderdeDict
@字典与判断
if 'lala' not in dict.keys():
@字符串方法
name.capitalize() 首字母大写 name.casefold() 所有语言大写全部变小写 name.center(50,"-") 输出 '---------------------Alex Li----------------------' name.count('lex') 统计 lex出现次数 name.encode() 将字符串编码成bytes格式 name.endswith("Li") 判断字符串是否以 Li结尾 "Alex\tLi".expandtabs(10) 输出'Alex Li', 将\t转换成多长的空格 name.find('A') 查找A,找到返回其索引, 找不到返回-1 format : >>> msg = "my name is {}, and age is {}" >>> msg.format("alex",22) 'my name is alex, and age is 22' >>> msg = "my name is {1}, and age is {0}" >>> msg.format("alex",22) 'my name is 22, and age is alex' >>> msg = "my name is {name}, and age is {age}" >>> msg.format(age=22,name="ale") 'my name is ale, and age is 22' format_map >>> msg.format_map({'name':'alex','age':22}) 'my name is alex, and age is 22' msg.index('a') 返回a所在字符串的索引 '9aA'.isalnum() True '9'.isdigit() 是否整数 name.isnumeric name.isprintable name.isspace name.istitle name.isupper "|".join(['alex','jack','rain']) 'alex|jack|rain' maketrans >>> intab = "aeiou" #This is the string having actual characters. >>> outtab = "12345" #This is the string having corresponding mapping character >>> trantab = str.maketrans(intab, outtab) >>> >>> str = "this is string example....wow!!!" >>> str.translate(trantab) 'th3s 3s str3ng 2x1mpl2....w4w!!!' msg.partition('is') 输出 ('my name ', 'is', ' {name}, and age is {age}') >>> "alex li, chinese name is lijie".replace("li","LI",1) 'alex LI, chinese name is lijie' msg.swapcase 大小写互换 >>> msg.zfill(40) '00000my name is {name}, and age is {age}' >>> n4.ljust(40,"-") 'Hello 2orld-----------------------------' >>> n4.rjust(40,"-") '-----------------------------Hello 2orld' >>> b="ddefdsdff_哈哈" >>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则 True
@