import io
s = "Hello everyone"
sio =io.StringIO(s)
print(sio)
print(sio.getvalue())
sio.seek(5) #指针移动到字符串索引为5的位置处
sio.write("6")
print(sio.getvalue())
字符串定义了则无法改变,重新赋值,原字符串仍然存在,此处io使得字符串可变,
修改字符串则会直接修改,不会产生新的对象
a = 5
print(1<a<6)
位操作符
a = 0b11001
b = 0b01000
print(bin(a|b)) #按位或
print(bin(a&b)) #按位与
print(bin(a^b)) #按位异或
左移和右移
a=2
b=8
print(a<<2) #左移
print(b>>2) #右移
print([10,20,30]+[20,30,40])
10、20、30、40均为对象,包含id、type和value
列表(序列)连续的内存空间存放10、20、30、40(对象)的地址
列表也是对象,变量a存储列表的地址,当a[0]找10的地址,进而找到10这个对象
推导式创建列表:
a = [x*2 for x in range(10)]
print(a)
b = [x*2 for x in range(10) if x%3==0]
print(b)
del (元素索引):
删除过程相当于元素的拷贝
pop(元素索引):
remove(元素):
index()索引
count()统计
len()
成员资格判断
a = (1,2,3)
b = 1,2,3
c = (1,) #元组
print(c,type(c))
d = (1) #数
print(d,type(d))
e = 1,
print(e,type(e))
f = tuple()
print(f)
g = tuple("abc")
print(g)
h = tuple(range(10))
print(h)
print(list((1,2,3)))
print(list([1,2,3]))
j = tuple(list([1,2,3]))
print(j)
a = [1,2,3,5,6,4,3]
b = sorted(a) #返回列表
print(b)
c = 1,2
d = 3,4
e = c+d
print(e)
a = [1,2]
b = [3,4]
c = [5,5]
d = zip(a,b,c)
print(d)
print(list(d))
a = (x*2 for x in range(5))
# print(a.__next__())
# print(a.__next__())
# print(a.__next__())
# print(a.__next__())
# print(a.__next__())
# print(a.__next__()) #已全部取出,报错
print(a)
print(tuple(a)) #生成器只能用一次
print(list(a))
a = {"name":"zpl","age":"18","city":"shanghai"}
b = dict(name="zpl",age=18,city="shanghai")
c = dict([("name","zpl"),("age",18)])
d = {}
e = dict()
k = ["name","age"]
v = ["zpl",18]
f = dict(zip(k,v))
g = dict.fromkeys(["name","age"])#值为None
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
print(g)
a = {"name":"zpl","age":"18","city":"shanghai"}
print(a["name"])
print(a.get("name"))
print(a.get("ns","不存在"))
print(a.items())
print(a.values())
print(len(a))
print("name" in a) #键是否存在a中
a["address"] = "where"
print(a)
a.update(b)
print(a)
del(a["name"])
print(a)
b = a.pop("age")
print(a)
print(b)
a.clear()#全部删除
x,y,z = (10,20,30)
[a,b,c] = [10,20,30]
s = {"name":"zpl","age":"18","city":"shanghai"}
e,f,g = s.values()
h,i,j = s
l,m,n = s.items()
print(e,f,g)
print(h,i,j)
print(l,m,n)
Python会根据散列表的拥挤程度扩容,即创造更大的数组将原有内容拷贝到新数组中,接近2/3时就会扩容