Python Day02
程序员文章站
2022-03-01 20:43:39
...
- 模块
sys
import sys
print(sys.path)
print(sys.argv)
os
import os
os.system("dir")
cmd_res = os.popen("dir").read()
print(cmd_res)
- pyc
- 数据类型
1.int 在Python中整型数据超过边界后不会报错,类型会变为long,但是在C#中会报错
#32位
type(2*30)
==>int
type(2*31)
==>long
#64位
type(2*62)
==>int
type(2**63)
==>long
2.long 在Python中长整型没有数值的大小
3.float 占8个字节(64位),其中52位表示底,11位表示指数,剩下1位表示符号
4.bool
5.string
- 三元运算
a,b,c = 1,3,5
d = a if a > b else c
==>d = 5
- 进制
- 编码解码
各类型转二进制 encode相反为decode 类似C#(Encoding.Default.GetBytes(str)/Encoding.Defalut.GetString(Bytes)
msg = "陕西西安"
# print(msg.encode()) 因为本是为UTF8所以windows下不会报错但是Linux会
print(msg.encode("utf-8"))
print(msg.encode(encoding="utf-8"))
print(msg.encode(encoding="utf-8").decode(encoding="utf-8"))
======================================================
b'\xe9\x99\x95\xe8\xa5\xbf\xe8\xa5\xbf\xe5\xae\x89'
b'\xe9\x99\x95\xe8\xa5\xbf\xe8\xa5\xbf\xe5\xae\x89'
陕西西安
=======================================================
- List
names = ["XiYangyang", "NuanYangyang", "MeiYangyang","LanYangyang","LanYangyang"]
print(names[0])
=> XiYangyang
# 打印区间
print(names[0:3])
=>["XiYangyang", "NuanYangyang", "MeiYangyang"]
# 从右边打印一个
print(names[-1])
=>LanYangyang
# 打印右边2个
print(names[-1:-3]
=>['NuanYangyang', 'MeiYangyang']
# 打印后三个
print(names[-3:])
=>['NuanYangyang', 'MeiYangyang', 'LanYangyang']
# 追加
names.append("HuiTailang")
print(names)
=>['XiYangyang', 'NuanYangyang', 'MeiYangyang', 'LanYangyang', 'HuiTailang']
# 指定位置插入
names.insert(0,"HongTailang")
print(names)
=>['HongTailang', 'XiYangyang', 'NuanYangyang', 'MeiYangyang', 'LanYangyang', 'HuiTailang']
names[0] = "ManYangyang"
print(names)
=>['ManYangyang', 'XiYangyang', 'NuanYangyang', 'MeiYangyang', 'LanYangyang', 'HuiTailang']
# 删除
names.remove("ManYangyang") #del names[0]
prints(names)
=>['XiYangyang', 'NuanYangyang', 'MeiYangyang', 'LanYangyang', 'HuiTailang']
# 弹出一个元素 如果不指定位置则弹出最后一位
popname = names.pop()#names.pop(0)
print(names,popname)
=>['XiYangyang', 'NuanYangyang', 'MeiYangyang', 'LanYangyang'] HuiTailang
# 元素在list中的位置
print(names.index("NuanYangyang"))
=>1
# 元素在list中总个数
names = ["XiYangyang", "NuanYangyang", "MeiYangyang","LanYangyang","LanYangyang"]
print(names.count("LanYangyang"))
=>2
# 方向排列
names.reverse()
print(names)
=>['LanYangyang', 'LanYangyang', 'MeiYangyang', 'NuanYangyang', 'XiYangyang']
# 排序
names.sort()
print(names)
=>['LanYangyang', 'LanYangyang', 'MeiYangyang', 'NuanYangyang', 'XiYangyang']
# 延伸
names2 = [1,2,3]
names.extend(names2)
print(names)
=>['LanYangyang', 'LanYangyang', 'MeiYangyang', 'NuanYangyang', 'XiYangyang', 1, 2, 3]
names.clear()
=>[]