Python 30天:第六天 -- 元组

元组是Python中的一种有序且不可变的数据结构,用于存储不同类型的元素。它们用圆括号表示,不支持添加、删除或修改元素。元组的方法包括创建空元组、获取元素个数和索引。可以使用索引和切片操作访问元组元素,但无法更改。元组可以与其他元组连接,也可以转换为列表进行修改后再转回元组。在处理不可变数据或需要高效数据结构时,元组是理想选择。
摘要由CSDN通过智能技术生成

<< 第五天 || 第七天 >>

第六天

元组

元组是有序且不可更改(不可变)的不同数据类型的集合。元组用圆括号 () 书写。一旦元组被创建,我们就不能改变它的值。我们不能在元组中使用添加、插入、删除方法,因为它是不可修改的(可变的)。与列表不同,元组的方法很少。与元组相关的方法:

  • tuple():创建一个空元组
  • count():统计元组中指定项的个数
  • index():查找元组中指定项的索引
  • 运算符:连接两个或多个元组并创建一个新元组

创建元组 

空元组:创建一个空元组

# syntax
empty_tuple = ()
# or using the tuple constructor
empty_tuple = tuple()

具有初始值的元组 

# syntax
tpl = ('item1', 'item2','item3')
fruits = ('banana', 'orange', 'mango', 'lemon')

元组长度 

我们使用len()方法来获取元组的长度。 

# syntax
tpl = ('item1', 'item2', 'item3')
len(tpl)

访问元组项 

 正索引与列表数据类型类似,我们使用正索引或负索引来访问元组项。

# Syntax
tpl = ('item1', 'item2', 'item3')
first_item = tpl[0]
second_item = tpl[1]
fruits = ('banana', 'orange', 'mango', 'lemon')
first_fruit = fruits[0]
second_fruit = fruits[1]
last_index =len(fruits) - 1
last_fruit = fruits[las_index]

负索引 负索引表示从末尾开始,-1 表示最后一项,-2 表示倒数第二项,列表/元组长度的负数表示第一项。

 

# Syntax
tpl = ('item1', 'item2', 'item3','item4')
first_item = tpl[-4]
second_item = tpl[-3]
fruits = ('banana', 'orange', 'mango', 'lemon')
first_fruit = fruits[-4]
second_fruit = fruits[-3]
last_fruit = fruits[-1]

切片元组

我们可以通过在元组中指定从哪里开始和从哪里结束的索引范围来切出一个子元组,返回值将是一个具有指定项的新元组。

  • 正指数范围

    # Syntax
    tpl = ('item1', 'item2', 'item3','item4')
    all_items = tpl[0:4]         # all items
    all_items = tpl[0:]         # all items
    middle_two_items = tpl[1:3]  # does not include item at index 3
    fruits = ('banana', 'orange', 'mango', 'lemon')
    all_fruits = fruits[0:4]    # all items
    all_fruits= fruits[0:]      # all items
    orange_mango = fruits[1:3]  # doesn't include item at index 3
    orange_to_the_rest = fruits[1:]
  • 负指标范围

    # Syntax
    tpl = ('item1', 'item2', 'item3','item4')
    all_items = tpl[-4:]         # all items
    middle_two_items = tpl[-3:-1]  # does not include item at index 3 (-1)
    fruits = ('banana', 'orange', 'mango', 'lemon')
    all_fruits = fruits[-4:]    # all items
    orange_mango = fruits[-3:-1]  # doesn't include item at index 3
    orange_to_the_rest = fruits[-3:]

将元组更改为列表 

我们可以将元组更改为列表,将列表更改为元组。元组是不可变的,如果我们想修改元组,我们应该将其更改为列表。

# Syntax
tpl = ('item1', 'item2', 'item3','item4')
lst = list(tpl)
fruits = ('banana', 'orange', 'mango', 'lemon')
fruits = list(fruits)
fruits[0] = 'apple'
print(fruits)     # ['apple', 'orange', 'mango', 'lemon']
fruits = tuple(fruits)
print(fruits)     # ('apple', 'orange', 'mango', 'lemon')

检查元组中的项目 

我们可以使用in检查一个项目是否存在于元组中,它返回一个布尔值。

# Syntax
tpl = ('item1', 'item2', 'item3','item4')
'item2' in tpl # True
fruits = ('banana', 'orange', 'mango', 'lemon')
print('orange' in fruits) # True
print('apple' in fruits) # False
fruits[0] = 'apple' # TypeError: 'tuple' object does not support item assignment

加入元组 

我们可以使用 + 运算符连接两个或多个元组

# syntax
tpl1 = ('item1', 'item2', 'item3')
tpl2 = ('item4', 'item5','item6')
tpl3 = tpl1 + tpl2
fruits = ('banana', 'orange', 'mango', 'lemon')
vegetables = ('Tomato', 'Potato', 'Cabbage','Onion', 'Carrot')
fruits_and_vegetables = fruits + vegetables

删除元组 

不可能删除元组中的单个项目,但可以使用del删除元组本身。

# syntax
tpl1 = ('item1', 'item2', 'item3')
del tpl1
fruits = ('banana', 'orange', 'mango', 'lemon')
del fruits

 🌕你真勇敢,你能走到这一步。您刚刚完成了第 6 天的挑战,您已经迈出了通往卓越之路的 6 步。现在为您的大脑和肌肉做一些锻炼。

练习: 第六天

练习:1级

  1. 创建一个空元组
  2. 创建一个包含你姐妹和兄弟名字的元组(想象中的兄弟姐妹都可以)
  3. 加入兄弟姐妹元组并分配给兄弟姐妹
  4. 你有几个兄弟姐妹?
  5. 修改siblings元组,加入你爸爸妈妈的名字赋给family_members

练习:2级 

  1. 从 family_members 中解压兄弟姐妹和父母
  2. 创建水果、蔬菜和动物产品元组。连接三个元组并将其分配给一个名为 food_stuff_tp 的变量。
  3. 将 about food_stuff_tp 元组更改为 food_stuff_lt 列表
  4. 从 food_stuff_tp 元组或 food_stuff_lt 列表中切出一个或多个中间项。
  5. 从 food_staff_lt 列表中切出前三项和后三项
  6. 完全删除 food_staff_tp 元组
  7. 检查一个项目是否存在于元组中:
  • 检查“Estoni”是否为北欧国家

  • 检查“Iceland”是否为北欧国家

nordic_countries = ('Denmark', 'Finland','Iceland', 'Norway', 'Sweden')

<< 第五天 || 第七天 >> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

舍不得,放不下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值