📜  Python元组

📅  最后修改于: 2022-05-13 01:55:47.431000             🧑  作者: Mango

Python元组

元组是Python对象的集合,很像列表。存储在元组中的值序列可以是任何类型,并且它们由整数索引。

元组的值在语法上由“逗号”分隔。虽然没有必要,但更常见的是通过关闭括号中的值序列来定义元组。这有助于更轻松地理解Python元组。

创建一个元组

在Python中,通过放置由“逗号”分隔的值序列来创建元组,可以使用或不使用括号对数据序列进行分组。

注意:不使用括号创建Python元组称为元组打包。

演示在元组中添加元素的Python程序。

Python3
# Creating an empty Tuple
Tuple1 = ()
print("Initial empty Tuple: ")
print(Tuple1)
 
# Creating a Tuple
# with the use of string
Tuple1 = ('Geeks', 'For')
print("\nTuple with the use of String: ")
print(Tuple1)
 
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))
 
# Creating a Tuple
# with the use of built-in function
Tuple1 = tuple('Geeks')
print("\nTuple with the use of function: ")
print(Tuple1)


Python3
# Creating a Tuple
# with Mixed Datatype
Tuple1 = (5, 'Welcome', 7, 'Geeks')
print("\nTuple with Mixed Datatypes: ")
print(Tuple1)
 
# Creating a Tuple
# with nested tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('python', 'geek')
Tuple3 = (Tuple1, Tuple2)
print("\nTuple with nested tuples: ")
print(Tuple3)
 
# Creating a Tuple
# with repetition
Tuple1 = ('Geeks',) * 3
print("\nTuple with repetition: ")
print(Tuple1)
 
# Creating a Tuple
# with the use of loop
Tuple1 = ('Geeks')
n = 5
print("\nTuple with a loop")
for i in range(int(n)):
    Tuple1 = (Tuple1,)
    print(Tuple1)


Python3
# Accessing Tuple
# with Indexing
Tuple1 = tuple("Geeks")
print("\nFirst element of Tuple: ")
print(Tuple1[0])
 
 
# Tuple unpacking
Tuple1 = ("Geeks", "For", "Geeks")
 
# This line unpack
# values of Tuple1
a, b, c = Tuple1
print("\nValues after unpacking: ")
print(a)
print(b)
print(c)


Python3
# Concatenation of tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('Geeks', 'For', 'Geeks')
 
Tuple3 = Tuple1 + Tuple2
 
# Printing first Tuple
print("Tuple 1: ")
print(Tuple1)
 
# Printing Second Tuple
print("\nTuple2: ")
print(Tuple2)
 
# Printing Final Tuple
print("\nTuples after Concatenation: ")
print(Tuple3)


Python3
# Slicing of a Tuple
 
# Slicing of a Tuple
# with Numbers
Tuple1 = tuple('GEEKSFORGEEKS')
 
# Removing First element
print("Removal of First Element: ")
print(Tuple1[1:])
 
# Reversing the Tuple
print("\nTuple after sequence of Element is reversed: ")
print(Tuple1[::-1])
 
# Printing elements of a Range
print("\nPrinting elements between Range 4-9: ")
print(Tuple1[4:9])


Python
# Deleting a Tuple
 
Tuple1 = (0, 1, 2, 3, 4)
del Tuple1
 
print(Tuple1)


输出:

Initial empty Tuple: 
()

Tuple with the use of String: 
('Geeks', 'For')

Tuple using List: 
(1, 2, 4, 5, 6)

Tuple with the use of function: 
('G', 'e', 'e', 'k', 's') 

创建具有混合数据类型的元组。

元组可以包含任意数量的元素和任何数据类型(如字符串、整数、列表等)。元组也可以用单个元素创建,但这有点棘手。括号中只有一个元素是不够的,必须有一个尾随的“逗号”才能使其成为元组。

Python3

# Creating a Tuple
# with Mixed Datatype
Tuple1 = (5, 'Welcome', 7, 'Geeks')
print("\nTuple with Mixed Datatypes: ")
print(Tuple1)
 
# Creating a Tuple
# with nested tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('python', 'geek')
Tuple3 = (Tuple1, Tuple2)
print("\nTuple with nested tuples: ")
print(Tuple3)
 
# Creating a Tuple
# with repetition
Tuple1 = ('Geeks',) * 3
print("\nTuple with repetition: ")
print(Tuple1)
 
# Creating a Tuple
# with the use of loop
Tuple1 = ('Geeks')
n = 5
print("\nTuple with a loop")
for i in range(int(n)):
    Tuple1 = (Tuple1,)
    print(Tuple1)

输出:

Tuple with Mixed Datatypes: 
(5, 'Welcome', 7, 'Geeks')

Tuple with nested tuples: 
((0, 1, 2, 3), ('python', 'geek'))

Tuple with repetition: 
('Geeks', 'Geeks', 'Geeks')

Tuple with a loop
('Geeks',)
(('Geeks',),)
((('Geeks',),),)
(((('Geeks',),),),)
((((('Geeks',),),),),)

元组的访问

元组是不可变的,通常它们包含一系列异构元素,这些元素通过解包或索引(或者在命名元组的情况下甚至通过属性)访问。列表是可变的,它们的元素通常是同质的,可以通过遍历列表来访问。

注意:在解包元组时,左侧的变量数量应等于给定元组 a 中的值的数量。

Python3

# Accessing Tuple
# with Indexing
Tuple1 = tuple("Geeks")
print("\nFirst element of Tuple: ")
print(Tuple1[0])
 
 
# Tuple unpacking
Tuple1 = ("Geeks", "For", "Geeks")
 
# This line unpack
# values of Tuple1
a, b, c = Tuple1
print("\nValues after unpacking: ")
print(a)
print(b)
print(c)

输出:

First element of Tuple: 
G

Values after unpacking: 
Geeks
For
Geeks

元组的连接

元组的连接是连接两个或多个元组的过程。连接是通过使用 '+'运算符完成的。元组的连接总是从原始元组的末尾开始。其他算术运算不适用于元组。

注意 -只有相同的数据类型可以与串联组合,如果组合列表和元组会出现错误。

Python3

# Concatenation of tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('Geeks', 'For', 'Geeks')
 
Tuple3 = Tuple1 + Tuple2
 
# Printing first Tuple
print("Tuple 1: ")
print(Tuple1)
 
# Printing Second Tuple
print("\nTuple2: ")
print(Tuple2)
 
# Printing Final Tuple
print("\nTuples after Concatenation: ")
print(Tuple3)

输出:

Tuple 1: 
(0, 1, 2, 3)

Tuple2: 
('Geeks', 'For', 'Geeks')

Tuples after Concatenation: 
(0, 1, 2, 3, 'Geeks', 'For', 'Geeks')

元组切片

对元组进行切片是为了从元组中获取特定范围或子元素的切片。也可以对列表和数组进行切片。列表中的索引导致获取单个元素,而切片允许获取一组元素。

注意 -负增量值也可用于反转元组的序列。


Python3

# Slicing of a Tuple
 
# Slicing of a Tuple
# with Numbers
Tuple1 = tuple('GEEKSFORGEEKS')
 
# Removing First element
print("Removal of First Element: ")
print(Tuple1[1:])
 
# Reversing the Tuple
print("\nTuple after sequence of Element is reversed: ")
print(Tuple1[::-1])
 
# Printing elements of a Range
print("\nPrinting elements between Range 4-9: ")
print(Tuple1[4:9])

输出:

Removal of First Element: 
('E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S')

Tuple after sequence of Element is reversed: 
('S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G')

Printing elements between Range 4-9: 
('S', 'F', 'O', 'R', 'G')

删除元组

元组是不可变的,因此它们不允许删除其中的一部分。使用 del() 方法删除整个元组。

注意 -删除后打印元组会导致错误。

Python

# Deleting a Tuple
 
Tuple1 = (0, 1, 2, 3, 4)
del Tuple1
 
print(Tuple1)

内置方法

Built-in-MethodDescription
index( )Find in the tuple and returns the index of the given value where it’s available
count( )Returns the frequency of occurrence of a specified value

内置函数

Built-in FunctionDescription
all()Returns true if all element are true or if tuple is empty
any()return true if any element of the tuple is true. if tuple is empty, return false
len()Returns length of the tuple or size of the tuple
enumerate()Returns enumerate object of tuple
max()return maximum element of given tuple
min()return minimum element of given tuple
sum()Sums up the numbers in the tuple
sorted()input elements in the tuple and return a new sorted list
tuple()Convert an iterable to a tuple.

最近关于元组的文章

元组程序

  • 打印给定布尔字符串中的唯一行
  • 从给定字符串生成所有可能的有效 IP 地址的程序
  • Python字典在字符串中查找镜像字符
  • 根据Python输入字符串中出现的字符生成两个输出字符串
  • Python groupby 方法删除所有连续重复
  • 将字符列表转换为字符串
  • 从列表中删除空元组
  • 反转元组
  • Python设置 symmetric_difference()
  • 将元组列表转换为字典
  • 按其浮动元素对元组进行排序
  • 计算元组中元素的出现次数
  • 计算列表中的元素,直到元素是元组
  • 按任意键对元组进行升序排序
  • Python中的命名元组

有用的链接:

  • Python程序的输出
  • 最近关于Python元组的文章
  • 多项选择题 - Python
  • Python分类中的所有文章