不可变类型:
值改变了,内存地址也改变、其实改变的不是原值、而是改变之后又生成一块空间来保存新值
可变类型:
值改变了,内存地址没有改变,其实改变的是原值,只要你改变了只,原来的值就会发生变化
颠倒列表元素
l = [1, 2, 3, 4, 5, 6]
l.reverse()
print(l)
给列表元素排序
l = [2, 3, 4, 6, 5, 1]
l.sort()
print(l)
字典的取值和赋值
dic = {
'name': 'xxx',
'age': 18,
'hobby': ['play game', 'basketball']
}
# 字典的取值
print(dic['name'])
print(dic['age'])
print(dic['hobby'][1])
# 字典的赋值
dic['gender'] = 'male'
dic['name'] = 'jerry'
print(dic)
求字典的长度及其成员运算
dic = {
'name': 'xxx',
'age': 18,
'hobby': ['play game', 'basketball']
}
# 求字典的长度
print(len(dic))
# 字典的成员运算
# in 的运算
print('name' in dic)
print('salary' in dic)
# not in的运算
print('name' not in dic)
print('salary' not in dic)
元组的类型转换
# 字符串转元组
res1 = tuple('hello')
print(res1)
# 列表转元组
res2 = tuple([1, 2, 3, 4])
print(res2)
# 字典转元组
res3 = tuple({'username': 'kevin', 'age': 18})
print(res3)
# 集合转元组
res4 = tuple({1, 2, 3, 4})
print(res4)
元组的取值和切片
tup = (1, 'hello', 15000.00, 11, 22, 33)
# 列表的正向取值
print(tup[2])
# 列表的逆向取值
print(tup[-2])
# 列表的正向切片
print(tup[0:4])
print(tup[0:4:2])
# 列表的逆向切片
print(tup[-5:-1])
print(tup[-5:-1:2])
# 列表的颠倒
print(tup[::-1])
求元组的长度及其成员运算
tup = (1, 'tony', 15000.00, 11, 22, 33)
print(len(tup))
# 列表的成员运算
# in的运算
print(1 in tup)
print(8 in tup)
print('tony' in tup)
print('kevin' in tup)
# not in的运算
print(1 not in tup)
print(8 not in tup)
print('tony' not in tup)
print('kevin' not in tup)
元组的循环
t = (1,2,3,4,5,6,7,8,9)
for i in t:
print(i)
集合的数据类型转换
# 字符串转集合
res1 = set('egon')
print(res1)
# 列表转集合
res2 = set([1,2,3,4])
print(res2)
# 元组转集合
res3 = set((1,2,3,4))
print(res3)
# 字典转集合
res4 = set({'name':'jason',})
print(res4)
集合的去重
name_list = ['kevin', 'jerry', 'tony', 'oscar', 'tony', 'oscar', 'jerry', ]
s = set(name_list)
new_name_list = list(s)
print(new_name_list)
求集合的长度及其成员运算
s={'a',4,'b',9,'c'}
print(len(s))
# 集合的成员运算
# in的运算
print(1 in s)
print(4 in s)
print('a' in s)
print('d' in s)
# not in的运算
print(1 not in s)
print(4 not in s)
print('a' not in s)
print('b' not in s)
集合的循环
item = {1,2,3,4,5,6,7,8,9}
for i in item:
print(i)