Tuple In Python
In the last article, we explored the list. In this article, we will talk about tuple. A tuple in python is just like a list of a sequence of immutable python objects. Tuple is similar to list. Same as list it can store different types of objects. The difference between list and tuple is that list is declared in square brackets and can be changed while tuple is declared in parentheses and cannot be changed.
Below are some examples of the tuple:
Syntax:
my_tup = ('Jan’, 2 , ’march')
We can also write
my_empty_tup = ();
For writing tuple for a single value, you need to include a comma, even though there is a single value. Also at the
my_single_value_tup = (‘Jan’,);
A tuple can also contain another tuple as an element. The tuple index starts from 0. We can access tuple in the same way as we access list.
Below are some of the example:
my_tup_1 = (1, 2, 3) my_tup_2 = my_tup_1,(4,5,6) print(my_tup_2) print(my_tup_1[2]) print(my_tup_1[-2])
Output:
((1, 2, 3), 4, 5, 6)
3
1
Apart from this, we can also have slicing operation, append two tuples to a new tuple and replicate the tuple as shown below:
tup_1 = (1, 2, 3) tup_2 = (4, 5, 6) #slicing example of tuple print(tup_1[:-1]) #Adding two tuple tup_3 = tup1 + tup2 print(tup_3) #tuple replication example print(tup_1 * 2)
Output:
(1, 2)
(1, 2, 3, 4, 5, 6)
(1, 2, 3, 1, 2, 3)
Python provides a tuple assignment feature. Let's check this by an example how it works.
#my tuple with variable values value_tup = (“Codepumpkin”, ”Python Tuple”, “Pumpkin”) #assigning these values to variables (blog_name, article_title, writer) = value_tup print(blog_name) print(article_title) print(writer)
Output:
Codepumpkin
Python Tuple
Pumpkin
Tuple also provides some of the built-in functions as shown in below table:
Function | Description |
| It gives the length of a tuple |
max(tuple) | It returns the maximum value from the tuple. |
min(tuple) | It returns the minimum value from a tuple. |
tuple(sequence) |
It converts the sequence into |
| It compares the two Tuples. |
Advantages of tuple over the list:
- Tuples that consist of immutable elements can be used as a key for the dictionary, which is not possible with the list
- If you have data that is immutable, implementing it as tuple will guarantee that it remains write-protected
- Iterating through tuple is faster than with list since tuples are immutable.
That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.
Happy Learning 🙂
If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.
We may not get time to plant a tree, but we can definitely donate â‚ą42 per Tree.
About the Author
Tags: python, Python Tutorials
Comments and Queries
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.