Python Tuples

Tuples are immutable and typically contain a sequence of dissimilar elements that can be accessed through unboxing or indexing (or even by attribute in the case of named tuples). Lists are mutable, and their elements are usually uniform and accessible by iterating over the list.

Creating a Tuple

In Python, tuples are created by placing a sequence of values, separated by commas, with or without parentheses to group the data sequence. Tuples can contain any number of elements and any data type (e.g. strings, integers, list, etc.). Tuples can also be created with a single element, but this is a bit tricky. Having one element in parentheses is not enough, there must be a comma to make it a tuple.

Note. Creating a Python tuple without using parentheses is called Tuple Packing.

# Python program for demonstration
# Adding items to the Set

 
# Create an empty tuple

Tuple1 = ()

print ( "Initial empty Tuple:" )

print (Tuple1)

 
# Create a tuple with
  # using strings

Tuple1 = ( `Geeks` , ` For` )

print ( "Tuple with the use of String:" )

print (Tuple1)

 
# Create a tuple with
# use a list

list1 = [ 1 , 2 , 4 , 5 6 ]

print ( "Tuple using List:" )

print ( tuple (list1 ))

 
# Create a tuple
# using a loop

Tuple1 = ( `Geeks` )

n = 5

print ( "Tuple with a loop" )

for i in range ( int (n)):

Tuple1 = (Tuple1,)

print (Tuple1)

 
# Create a tuple with
# use inline function

Tuple1 = tuple ( `Geeks` )

print ( " Tuple with the use of function: " )

print (Tuple1)

 
# Create a tuple with
# Mixed data types

Tuple1 = ( 5 , `Welcome` , 7 , `Geeks` )

print ( "Tuple with Mixed Datatypes:" )

print (Tuple1)

  
# Create a tuple
# with nested tuples

Tuple1 = ( 0 , 1 , 2 , 3 )

Tuple2 = ( ` python` , `geek` )

Tuple3 = (Tuple1, Tuple2)

print ( "Tuple with nested tuples:" )

print (Tuple3)

 
# Create a tuple
# repeat

Tuple1 = ( `Geeks` ,) * 3

print ( "Tuple with repetition:" )

print (Tuple1)

Exit:

 Initial empty Tuple: () Tuple with the use of String: (`Geeks`,` For`) Tuple using List: (1, 2, 4, 5, 6) Tuple with a loop (`Geeks`,) ( (`Geeks`,),) (((` Geeks`,),),) ((((`Geeks`,),),),) (((((` Geeks`,),),), ),) Tuple with the use of function: (`G`,` e`, `e`,` k`, `s`) 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 Concatenation

Tuple Concatenation — it is the process of combining two or more tuples. Concatenation is done using the "+" operator. Concatenation of tuples is always performed from the end of the original tuple. Other arithmetic operations do not apply to tuples. 
Note. In a union, only the same data types can be combined; an error occurs when combining list and tuple.

Exit:

 Tuple 1: (0, 1, 2, 3 ) Tuple2: (`Geeks`,` For`, `Geeks`) Tuples after Concatenaton: (0, 1, 2, 3,` Geeks`, `For`,` Geeks`) 

Tuple slicing

Tuple slicing is performed to extract a specific range or slice sub-elements from a tuple. Slicing can also be done for lists and arrays. Indexing into a list will retrieve a single item, while Slicing allows you to retrieve a set of items. 
Note. Negative increment values ​​can also be used to change the sequence of tuples.

# Concatenate tuples

Tuple1 = ( 0 , 1 , 2 , 3 )

Tuple2 = ( ` Geeks` , `For` , ` Geeks` )

  

Tuple3 = Tuple1 + Tuple2

  
# Print the first tuple

print ( "Tuple 1:" )

print (Tuple1)

 
# Print the second tuple

print ( " Tuple2: " )

print (Tuple2)

 
# Print the final tuple

print < / code> ( "Tuples after Concatenaton:" )

print (Tuple3)

# Slicing a tuple

 
# Tuple slicing
# with numbers

Tuple1 = tuple ( ` GEEKSFORGEEKS` )

 
# Remove the first element

print ( "Removal of First Element:" )

print (Tuple1 [ 1 :])

 
# Reverse tuple

print ( "Tuple after sequence of Element is reversed:" )

print (Tuple1 [:: - 1 ])

 
# Print range items

print ( "Printing elements between Range 4-9:" )

print (Tup le1 [ 4 : 9 ])

Output:

 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`) 

Deleting a tuple

Tuples are immutable and therefore do not allow part of it to be deleted. The entire tuple is removed using the del () method. 
Note. Printing a tuple after deletion results in an error.

# Removing a tuple

 

Tuple1 = ( 0 , 1 , 2 , 3 , 4 )

del Tuple1

 

print (Tuple1)

Traceback (most recent call last):
File “/ home / efa50fd0709dec0 8434191f32275928a.py ”, line 7, in
print (Tuple1)
NameError: name `Tuple1` is not defined

Built-in Methods

Built-in Function

Description
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.

< h3> Latest articles on Tuple

Program Tuples