PythonPlaza - Python & AI

Python

IntroductionVariablesIf-else
Strings Functions while-loop
For-LoopListSet
DictionaryTuple Try..Except
Class/ObjectInheritancePolymorphism
File Handling

Python Tuple

Tuples are used to store multiple items in a single variable. A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets. When we say that tuples are ordered, it means that the items have a defined order, and that order will not change. Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created.

Create a Tuple

There are 2 ways to create a tuple. The easiest way is to enclose a sequence of comma-separated values within parentheses. The second way to create tuple is to pass a set, list or tuple in a tuple() function.



Code Example 1:
#Create tuple
thistuple = ("Red", "Green", "Blue")
print(thistuple)

#Output:
('Red', 'Green', 'Blue')

Code Example 2:
#Create tuple from list
myList = ["A", "B", "C"]
myTuple=tuple(myList)
print(myTuple)

#Output:
('A', 'B','C')

Code Example 3:
#Create tuple from set
mySet = {"1", "2", "3"}
myTuple=tuple(mySet)
print(myTuple)

#Output:
('3', '1','2')

Code Example 4:
#Create tuple from tuple
myTuple = ("X", "Y", "Z")
myTuple=tuple(myTuple)
print(myTuple)

#Output:
('X', 'Y','Z')



Duplicate values in Tuples

Tuple allows the duplicates values.



Code Example 5:
#Create tuple
thistuple = ("Kansas", "Nevada", "Kansas")
print(thistuple)

#Output:
('Kansas', 'Nevada', 'Kansas')


How to access Tuple Items

You can access items in tuple by referring to the index number, inside square brackets as shown below:



Code Example 6:
#Create tuple
thistuple = ("Kansas", "Texas", "Nevada")
print(thistuple[1])

#Output:
Kansas


Negative Indexing in Tuple

Negative indexing means start from the end. For example, -1 refers to the last item, -2 refers to the second last item etc.



Code Example 7:
#Print the last item of the tuple
thistuple = ("Kansas", "Texas", "Nevada")
print(thistuple[-1])

#Output:
Nevada


Range of Indexes in Tuple

You can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new tuple with the specified items.



Code Example 8:
#Return the third and fourth items
thistuple=("Green", "Blue", "Red", "Yellow")
print(thistuple[1:3])

#Output:
('Blue', 'Red')


Handling Updates in Tuple:

Tuples are unchangeable, that means items cannot be changed, added, or removed once a tuple is created. There is a workaround update, add, and remove items in the tuple. But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple..



#This will create a tuple
x = ("Red", "Green", "Blue")

#This is will convert the Tuple x to List y.
y = list(x)

#Update an item in the List 
y[1]="Yellow"

#Convert it back to Tuple
x = tuple(y)

#Print
print(x)


#Output:

("Red", "Yellow", "Blue")

Adding new items to Tuple:

To add an item to tuple, you will convert the existing Tuple to List and add the item and convert it back to Tuple.


Example Code 6

#Create a tuple. x = ("Red", "Green", "Blue") #Convert the Tuple x to List y. y = list(x) """Add another color to the tuple, you will convert the existing Tuple to List, add the item and convert it back to Tuple""" y.append("Purple") #Convert it back to Tuple x = tuple(y) #print print(x) #Output: ("Red", "Green", "Blue", "Purple")

Removing items in Tuple:

Since Tuples are unchangeable, you cannot remove items from it. But there is a workaround. You can convert Tuple to list, remove an item and convert it back to the Tuple.


Example Code 7

y = ("Red", "Green", "Blue") x = list(y) #Remove an item y.remove("Green") #Convert it back to Tuple. x = tuple(y) #print print(x) #Output: ('Red', 'Blue')

Unpacking a Tuple:

When a tuple is created, values are assigned to it. It is called "packing" a tuple..


Example Code 8

colors = ("Red", "Green", "Blue") #We extract the values back into variables. This is called "unpacking" (color1, color2, color3) = colors #print print(x) #Output: Red Green Blue