【Python_10】Python基础语法(数据容器之元组详解)

概述

  • 数据容器是用来存储和组织数据的数据结构和对象。
  • 可以以不同的方式存储和操作数据,提供了对数据的访问、添加、删除和修改等操作。
  • 数据容器可以是简单的数据类型(如整数、浮点数、字符串)或复杂的数据结构(如列表、元组、字典、集合等)
  • 不同的数据容器具有不同的特点和用途,可以根据具体的需求选择合适的数据容器来存储和操作数据。

Python中的数据容器

在Python中,常用的数据容器主要有列表,元组,字典,集合

元组

特点:

  • 元组是不可变的,可以作为字典的键或集合的元素。
  • 元组的元素可以是不同类型的数据。
  • 元组的长度是固定的,不能被修改。

1.1 元组定义

元组(tuple):是一种有序的不可变容器,可以保存任意类型的数据。元组使用圆括号()来表示,每个元素之间用逗号(,)分隔。
语法:my_tuple=()
也可以省略小括号,使用,号分割

示例:

# 定义元组
tuple1 = ()  # 空元组
tuple2 = (1, 2, 3, 4)
tuple3 = ('a', 'b', 'c')
tuple4 = 1, 'hello', [1, 2, 3], {
   1, 2, 3}
print(type(tuple1))  # <class 'tuple'>
print(type(tuple2))  # <class 'tuple'>
print(type(tuple3))  # <class 'tuple'>
print(type(tuple4))  # <class 'tuple'>

1.2 元组取值,切片

在这里插入图片描述
取值:my_tuple[索引值] 返回一个当前索引的元素
说明:索引值必须在元组长度范围,否则报错IndexError: tuple index out of range
切片:my_tuple[startIndex:endIndex:step] 返回一个新元组,不改变原元组
startIndex: 表示索引值从哪里开始,不写表示从索引0开始;
endIndex: 表示截取到索引end 位置结束,不写表示截取至最后一个元组
step: 指的是从 startIndex索引处的字符开始,每 step 个距离获取一个字符,直至 endIndex索引出的字符。step 默认值为 1,当省略该值时,最后一个冒号也可以省略。
注意:startIndex<endIndex才能截取到字符或字符串,startIndex>=endIndex截取拿到永远是空元组;step可省略可为正数负数,但不可为0 ,否则报异常错误:ValueError: slice step cannot be zero

# 定义元组
my_tuple = 1, 2, 'hello', 'python', 1, 3
print(my_tuple)  # (1, 2, 'hello', 'python', 1, 3)
# 取值
print(my_tuple[0])  # 1
print(my_tuple[-3])  # python
# print(my_tuple[10]) # IndexError: tuple index out of range
# 切片取子集元祖
print(my_tuple[0:3])  # (1, 2, 'hello')
print(my_tuple[0:5:3])  # (1, 'python')
# print(my_tuple[0:5:0])  # ValueError: slice step cannot be zero
# 翻转元组
print(my_tuple[::-1])

1.3 元组遍历

1.3.1 for循环遍历

# 定义一个元组
my_tuple = 1, 2, 3
# for 循环遍历
for item in my_tuple:
    print(item)

输出结果:

1
2
3

1.3.2 while循环遍历

# 定义一个元组
my_tuple = "study", "python", "come on"
index = 0
while index < len(my_tuple):
    print(my_tuple[index])
    index += 1

输出结果:

study
python
come on

1.3.3 enumerate遍历【索引值和元素】

# 定义一个元组
my_tuple = "study", "python", "come on"
for index, element in enumerate(my_tuple):
    print(index, element)

输出结果:

0 study
1 python
2 come on

1.4 元组常用方法

1.4.1 len方法

len() 返回元组元素个数

# 定义元组
my_tuple = 'hello', 'python', 'hello', 'world', 'learning'
# len() 返回元组元素个数
len_tuple = len(my_tuple)
print(len_tuple)  # 5

1.4.2 index方法

index(element):返回元素的索引下标值,元素必须存在于元组中,否则会报错

# 定义元组
my_tuple = 'hello', 'python', 'hello', 'world', 'learning'
# index(element):返回元素的索引下标值,元素必须存在于元组中,否则会报错
# index_err = my_tuple.index('你好世界')  # ValueError: tuple.index(x): x not in tuple
hello_index = my_tuple.index('hello')
print(hello_index)  # 0

1.4.3 count方法

count(element): 返回元素在元组中出现的次数,元素必须存在于元组中,否则会报错

# 定义元组
my_tuple = 'hello', 'python', 'hello', 'world', 'learning'
# count(element): 返回元素在元组中出现的次数,元素必须存在于元组中,否则会报错
hello_count = my_tuple.count('hello')
print(hello_count)  # 2

1.5 元组运算符计算

1.5.1 加法运算

运算符加法:连接两个元组,返回新的元组

# 定义两个元组
my_tuple1 = (1, 2, 3, 4)
my_tuple2 = (1, 1, 1, 1)

# 运算符加法:连接两个元组,返回新的元组
my_tuple3 = my_tuple1 + my_tuple2
print(my_tuple3)  # (1, 2, 3, 4, 1, 1, 1, 1)

1.5.2 乘法运算

运算符加法:重复的元组内容,返回新的元组
注意:元组*正整数得到重复的一个新元组,元组*小于等于0的数返回空元组,只能元组*数字,不可以元组*元组

# 定义两个元组
my_tuple1 = (1, 2, 3, 4)
# 运算符乘法:重复的元组内容,返回新的元组
my_tuple2 = my_tuple1 * 3
print(my_tuple2)  # (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)

相关推荐

  1. Python-详解

    2023-12-30 03:56:04       34 阅读
  2. python记录

    2023-12-30 03:56:04       8 阅读
  3. 5.7Python

    2023-12-30 03:56:04       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-30 03:56:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-30 03:56:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-30 03:56:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-30 03:56:04       18 阅读

热门阅读

  1. PAT 乙级 1037 在霍格沃茨找零钱

    2023-12-30 03:56:04       36 阅读
  2. GPT技术:人工智能的语言革命

    2023-12-30 03:56:04       39 阅读
  3. idea 如何开启mybatis控制台SQL日志打印

    2023-12-30 03:56:04       44 阅读
  4. C++ 多态详解(14)

    2023-12-30 03:56:04       48 阅读
  5. Bun 安装

    2023-12-30 03:56:04       53 阅读
  6. 鸿蒙Harmony(十)动画

    2023-12-30 03:56:04       33 阅读
  7. 编程笔记 html5&css&js 002 一些基本概念

    2023-12-30 03:56:04       30 阅读
  8. 送你一台云电脑

    2023-12-30 03:56:04       36 阅读
  9. 系列十一、解压文件到指定目录

    2023-12-30 03:56:04       25 阅读
  10. 面向对象进阶-继承

    2023-12-30 03:56:04       36 阅读
  11. Linux模块编译

    2023-12-30 03:56:04       44 阅读