1、元组 tuple
由一系列变量组成的不可变的序列容器
计算机内存:
占用连续的空间,存储空间按需分配【节省空间】
说明:
1、存储任意类型的数据
2、存储的元素有先后顺序(索引)
表示方式:
(元素1, 元素2, …)
创建方式:
特殊的表示方式
1、单个元素存储【应用:参数传递】
t3 = (4,)
print(t3, type(t3))
2、数据1, 数据2, …
t4 = 4, 6, 8, ‘python’
print(t4, type(t4))
序列赋值: 拆包
x, y = 5, 6 x, y = (5, 6)
print(x, type([x, y]))
构造方法:
tuple(可迭代对象)
# 元组
t = ()
print(t, type(t))
t1 = tuple()
print(t1, type(t1))
t2 = (4, 5.6, 7j, True, 'Java', [6, 5], (7, 8))
print(t2, type(t2))
# 特殊的表示方式
# 1、单个元素存储【应用:参数传递】
t3 = (4,)
print(t3, type(t3))
# 2、数据1, 数据2, ...
t4 = 4, 6, 8, 'python'
print(t4, type(t4))
# 序列赋值: 拆包
x, y = 5, 6 # x, y = (5, 6)
print(x, type([x, y]))
# 构造方法:tuple(可迭代对象)
print(tuple('PYTHON'))
print(tuple(range(4)))
运算符
+
+=
*
*=
> >= <= < == !=
in/not in
索引
切片
返回值:元组
遍历
# 元组的运算符
t = (0, 1, "26", 3)
print(0 not in t) # 元组中存储元素0
print('6' in t) # 元组中的元素是一个整体
print('62' not in t) # 元组中的元素不能反向
# 索引
t = (0, 1, "26", 3)
print(t[1])
print(3, t[-1])
# 切片
t = (0, 1, "26", 3) * 2
print(t) # (0, 1, '26', 3, 0, 1, '26', 3)
print(t[0::-1])
print(t[0:-1:-1])
print(len(t))
print('-' * 50)
# 遍历
t = (0, 1, '26', 3, 0, 1, '26', 3)
for item in t:
print(item, end=' ')
for i in range(len(t)):
print(t[i], end=' ')
for i in range(len(t)-1, -1, -1):
print(t[i], end=' ')
常用方法
index
count
#常用方法
t1 = (0, 1, '26', 3, 0, 1, '26', 3)
# 获取索引值(默认从左至右)
print(t1.index(1))
# print(t1.index(5)) # 元素不存在,产生ValueError
# 计算次数
print(t1.count(3))
print(t1.count(6))
# "修改" 不推荐
t = (0, 1, '26', 3, 0, 1, '26', 3)
# tuple --> list
list_t = list(t)
print(list_t)
list_t[1] = 'one'
print(list_t)
# tuple <--- list
t = tuple(list_t)
print(t)
场景:
参数传递
存储固定数据
练习:
输入年月日,计算输入的日期为当年的第多少天?
# 输入年月日,计算输入的日期为当年的第多少天?
'''
分析:
1、输入年月日-->赋值给3个变量
2、判断闰年/平年
3、循环月份,累加天数
'''
date = input('请输入一个日期【xxxx/xx/xx】:')
year, month, day = [int(data) for data in date.split('/')]
total_days = 0
# 循环月份,累加天数
tuple_days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30)
# for m in range(month-1): # 12/5 11月+5 range(11) --> 0 - 10
# total_days += tuple_days[m]
total_days += sum(tuple_days[:month-1])
total_days += day
# 判断闰年/平年
isleap = year%4==0 and year%100!= 0 or year%400==0
if isleap:
if month > 2:
total_days += 1
print(f'{year}/{month}/{day}是{year}年的第{total_days}天')
2 字典 dict
存储一系列键值对结构的可变数据容器【离散空间】
1、字典的键唯一(值无要求)
2、字典的键只能使用不可变数据类型
3、字典的存储是无序
表达方式:
{键1:值1, 键2:值2, ..}
创建方式:
1、键只能是不可变数据类型
2、键唯一
3、键值对无序
# 字典
# 1、创建
# 空
d = {}
print(d, type(d))
d = dict()
print(d, type(d))
# 非空
# 不可变数据类型:int/float/complex/bool/str/tuple
# 键对应的是哈希值:唯一
d1 = {1: 'one', 3.14: '圆周率', False: ['', ()],
'name': 'python', (4, 6): 'tuple'}
print(d1, type(d1))
构造函数
dict(可迭代对象)
基本操作
1、查看
dict[key]
key in/not in dict
dict.get()
dict.items()
2、修改与增加
键值对存在 --> 修改
dict[key] = value
键值对不存在 --> 增加
dict[key] = value
3、删除
del dict[key]
dict.pop(key)
返回删除的key对应的值
# print({[5, 7]:(5, 7)}) # 可变数据类型不能作为字典的键
# 键不能重复
d2 = {'name': 'java', 'name': 'python'}
print(d2)
# 无序
d2 = {'name': 'java', 'age': 22}
d3 = {'age': 22, 'name': 'java'}
print(d2 == d3)
# 构造函数
# d4 = dict(range(5)) # 可迭代对象:满足键值对的组合方式
d4 = dict([('name', 'python'), ('age', 33)])
d4 = dict([('name', 'python'), ('age', 33), 'AB'])
print(d4)
# 基本操作
# 1、查看
# 字典的索引:按键索引 【重点】
d = {'name': 'python', 'age': 33, 'A': 'B', 1:[3, 5, 7]}
print(d['name'])
print(d['age'])
print(d[1][1]) # [key][index]
# print(d['addr']) # 键不存在,产生KeyError
# get方法 【重点】
print(d.get('A'))
print(d.get('addr')) # 键不存在,返回为None
# in/not in --> key in/not in dict 【重点】
print('a' in d) # 区分大小写
print('addr' not in d)
# items() 【重点】
print(d.items())
# keys()
print(d.keys())
# values()
print(d.values())
# 增加或修改
d = {'name': 'python', 'age': 33}
# 增加: 键值对不存在
d['addr'] = 'beijing'
print(d)
# 修改: 键值对存在
d['addr'] = '深圳'
print(d)
# 删除
d = {'name': 'python', 'age': 33, 'addr': '深圳'}
# del
del d['addr']
print(d)
# pop()
print(d.pop('age'))
print(d)
print(d.pop('age'))
print(d)
遍历:
for key in dict:
语句
for key, value in dict.items():
语句
推导式
{键表达式:值表达式 for 变量 in 可迭代对象 [if语句]}
# 字典的遍历
d = {'name': 'python', 'age': 33, 'addr': 'beijing'}
# 长度
print(len(d))
for item in d:
print(item, d[item])
print('--》', d.items())
for key, value in d.items():
print(key, value)
# 推导式
# {1:1, 3:9, 5:25, ..., 9:81}
dict_data = {}
for i in range(1, 10):
if i % 2 == 1:
dict_data[i] = i**2
print(dict_data)
# 推导式
print({i:i**2 for i in range(1, 10) if i % 2 == 1})
print({i:i**2 for i in range(1, 10, 2)})
复习列表的拷贝
# 列表的深浅拷贝
L = [1, 2, 3]
# 浅拷贝:浅浅拷贝了一层
# 拷贝了一层
# 切片
L1 = L[:]
print(L1)
# copy()
L2 = L.copy()
print(L2)
L3 = L # 创建了一个变量指向L列表绑定的对象
print(id(L), id(L1), id(L2))
L[0] = 'one' # 存储的数据内存地址不同,修改源列表对其他变量无影响
print(L, L1, L2, L3)
print('-' * 50)
# 拷贝了2+层: 对象引用了源列表的深层列表
L = [1, 2, 3, [4, 5, 6]]
L1 = L[:]
print(L1)
# copy()
L2 = L.copy()
print(L2)
print(id(L), id(L1), id(L2))
L1[-1][1] = 'five'
print(L, L1, L2)
print(id(L[-1]), id(L1[-1]), id(L2[-1]))
print('=' * 50)
# 深拷贝:完全独立拷贝一份
from copy import deepcopy
L = [1, 2, 3]
L1 = deepcopy(L)
L[0] = '11'
print(L1, L)
print(id(L), id(L1))
L = [1, 2, 3, [4, 5]]
L2 = deepcopy(L)
L[-1][1] = '55'
print(L2, L)
print(id(L), id(L2))
print(id(L[-1]), id(L2[-1]))
# 浅拷贝:
# 1、保存数据
# 2、数据保持关联
# 列表的删除元素
L = [2, 4, 6, 6, 6, 4, 6]
# 拷贝的新对象
for item in L[:]:
if item == 6:
L.remove(item) # 删除的是源列表
print(L)