AppDividend
Latest Code Tutorials

Python Tuple Example Tutorial | Complete Introduction On Python Tuples

0

Python Tuple Example Tutorial | Complete Introduction On Python Tuples is today’s topic. In Python programming language, the tuple is similar to a list with the difference is that we cannot change the items of a tuple once it is assigned whereas, in the list, items can be modified. The Tuple is a collection of Python objects separated by commas.

Python tuples are written with round brackets. Since tuples are immutable, iterating through a tuple is faster than a list. So there is a slight performance boost. If you have data that doesn’t change in time then, implementing it as tuple will guarantee that it remains write-protected.

Python Tuple Example Tutorial

A tuple is a collection which is ordered and unchangeable. Let us take a simple example of Tuple in Python.

deserts = ('oreo', 'cupcake', 'nougat', 'jellybean', 'pie')

Above is the simple example of Tuple in Python language.

Creating a Tuple

A tuple is formed by placing all the elements inside the parentheses (), separated by a comma. The parentheses are optional but are a good practice to write it. A tuple can have any number of items, and they may be of different types. Let us see the following example of different types of items in one tuple.

// app.py

deserts = ('oreo', 1, True, ['Android', 'iOS'], 'pie')
print(deserts)

In the above example, we have taken string, integer, list datatype to create a tuple and it is a valid tuple.

Let us write a tuple without parentheses.

deserts = 'oreo', 1, True, ['Android', 'iOS'], 'pie'
print(deserts)

Above code is also valid but, it does not best practice to write that is why we add the parentheses around the items.

Access Tuple Items

You can access tuple items by referring to the index number, inside square brackets. Let us see the following example.

// app.py

deserts = ('oreo', 1, True, ['Android', 'iOS'], 'pie')
print(deserts[3])

See the following output.

 

Python Tuple Example Tutorial | Complete Introduction On Python Tuples

Loop Through a Tuple

We can iterate For loop through a tuple. Let us see the below example.

# app.py

deserts = ('oreo', 'nougat', 'jellybean', 'pie')
for item in deserts:
    print(item)

The output is following.

 

Complete Introduction On Python Tuples

Try To Change Tuple Values

Let us change the values in a tuple and see the result.

# app.py

deserts = ('oreo', 'nougat', 'jellybean', 'pie')
deserts[1] = 'kitkat'
print(deserts)

I am using Visual Studio Code, and I have installed Python extension called pylint. That is why my editor throws me an error saying that ‘deserts’ does not support item assignment. That means we can not modify it. But still, let us run the file and see the output in the console.

 

Try To Change Tuple Values in Python

Tuple Length

If we want to determine how many items a tuple has, use the len() method.

Related Posts
1 of 42
# app.py

deserts = ('oreo', 'nougat', 'jellybean', 'pie')
print(len(deserts))

 

Tuple Length in Python

Remove Items in Tuple

Again, the items inside the tuple are unchangeable. But, we can delete the entire tuple. See the below example.

// app.py

deserts = ('oreo', 'nougat', 'jellybean', 'pie')
del deserts
print(deserts)

We have used the del keyword to delete the entire tuple. In the above code’s output, we will get an error because the tuple is already removed. So there is nothing to print because the compiler does not find any deserts tuple. So it throws an error.

 

Remove Items in Tuple

The tuple() Constructor

We can also use the tuple() constructor to make a tuple.

# app.py

icecreams = tuple(('vanila', 'american nuts', 'soft serve', 'choc ice'))
print(icecreams)

The output will be the same.

 

The tuple() Constructor in Python

Basic Tuples Operations

Tuples respond to the + and * operators much like strings in python; they mean concatenation and repetition here too, except that a result is the new tuple, not a string.

Python Expression Results Description
len((1, 2, 3, 4, 5, 6)) 6 Length
(11, 21, 31) + (41, 51, 61) (11, 21, 31, 41, 51, 61) Concatenation
(‘App’,) * 4 (‘App’, ‘App’, ‘App’, ‘App’) Repetition
21 in (11, 21, 31) True Membership
for z in (11, 21, 31): print(z), 11 21 31 Iteration

Nesting of Tuples

Let us see the following example of nested tuples.

# app.py

deserts = ('oreo', 'nougat', 'jellybean', 'pie')

icecreams = tuple(('vanila', 'american nuts', 'soft serve', 'choc ice'))

nestedTuples = (deserts, icecreams)
print(nestedTuples)

 

Nesting of Tuples in Python

Finally, Python Tuple Example Tutorial | Complete Introduction On Python Tuples is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.