We are going to expand our knowledge of Tuples with a Python tuple example which will help us strengthen our knowledge of tuple data structure. When you need to list a group of objects within a list you will need a tuple to store data elements inside.
Tuples can be used to store a list of mixed data in an indexed sequence which are immutable by nature. In this blog, we are going to learn more about tuples with the help of examples.
What is a Python Tuple?
A Python tuple is an ordered, immutable and indexed collection of elements of different data types stored in a single variable. This data structure is very similar to lists in Python language. We can easily make tuples with the help of round brackets or tuple constructors.
Python Tuple Key Takeaways
- Python Tuples are arranged in a fixed order and can be accessed using index.
- Tuples in Python are immutable which once created cannot be changed i,e. No addition, subtraction, modification, removal of elements.
- You can put duplicate values in Tuples unlike sets which do not allow duplicate values.
How To Create a Python Tuple Example?
Let us understand first how to create a python tuple with simple examples and syntax overview. There are many methods of creating a Python tuple which includes defining, creating, feeding it with values. You can create a tuple using the following methods.
- Tuple Constructor
- Round Bracket Method
- Using Commas method
- Using Tuple Packing
- Tuple with a single element
1. Using tuple constructor
We can use the tuple () constructor to create a tuple with data elements inside easily. Let us check a simple example to understand this method.
tup = tuple([2,4,5, “pwskills”,6,8,9])
print (tup) |
2. Using Round Bracket
We can simply define a tuple data structure in Python by using a round bracket to enclose the elements inside. Let us understand it with the help of an example. It will give the tuple elements enclosed within a round bracket as an output.
tup = (2,4,5, “pwskills”,6,8,9)
print (tup) |
3. With Comma Separation
We can generate a tuple in python using commas and without using brackets too. We will also not use any specific keywords to define tuples here. This will also print all elements on the output screen as a tuple with bracket.
tup = 2,4,5, “pwskills”,6,8,9
print (tup) |
4. Using Tuple Packing
We can also use tuple packing to close all elements inside as a tuple. Let us create our tuple elements using the packing method.
a, b, c = 11, 12, “pwskills”
tup = (a, b, c) print (tup) |
5. Using a Single Tuple In Python
You can easily create a Python tuple with a single element or can also create an empty tuple easily using comma or python constructor.
single_element_tuple = (5,)
print(type(single_element_tuple)) not_a_tuple = (5) # Without a comma, it is an integer print(type(not_a_tuple)) # Output: <class ‘int’> |
Python Tuple Examples
Let us understand Python tuples in a better way with some of the popular examples which will help us guide us and make us familiar with the tuples in a better way.
1. How to create a Simple Python Tuple?
Let us check a simple Python tuple example on how to create a simple tuple of mixed data type.
my_tuple = (10, “Python”, 3.14, True)
print(my_tuple) |
2. How can we access elements using index value in Python tuple?
We can simply access elements using the index value in the Python tuple. Let us understand it using a simple python tuple example.
fruits = (“apple”, “banana”, “cherry”)
print(fruits[0]) print(fruits[-1]) |
3. Tell the method to slice a tuple using an example.
We can simply slice a python tuple by specifying the index value of the python tuple.
numbers = (0, 1, 2, 3, 4, 5)
print(numbers[1:4]) # Output: (1, 2, 3) |
4. What is the method to concatenate different tuples together?
We can easily concatenate tuples together. Let us understand it with a simple example.
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6) result = tuple1 + tuple2 print(result) |
5. How to check whether the following element is inside the tuple or not?
We can simply find whether or not a data element can either be an integer, strings or other data type within a tuple. We only have to use the “in” keyword to find the element, it will return a boolean value i,e. False or true. Let us understand this using a simple python tuple example.
colors = (‘red’, ‘green’, ‘blue’)
print(‘red’ in colors) print(‘yellow’ in colors) |
6. How to Find the length of a Python tuple?
Let us find the length of a simple Python tuple with the help of a Python tuple example.
numbers = (10, 20, 30, 40, 50)
print(len(numbers)) |
7. Can we modify a Python Tuple?
Let us understand how we can modify a Python tuple or whether we cannot edit it at all. Making any changes in Python tuple will produce an error message in the output.
tup = (“Ankit”, 32, “Tesla”, “Toyota”)
Tup[0] = “Swift” print (tup) |
8. Can We iterate through a Tuple?
We can use the loops to iterate over the items of tuples easily. Let us understand it with the help of a simple Python tuple example.
tup = (“Ankit”, 32, “Tesla”, “Toyota”)
for x in tup: print (x) |
9. Can we Delete a Python tuple element after declaring?
We cannot delete a Python tuple element once we declare a tuple and feed it with values. However, we can delete the entire tuple at once by using the “del” keyword.
tup = (“Ankit”, 32, “Tesla”, “Toyota”)
del tup print (tup) |
10. Can we store duplicate elements in a Python tuple?
We can store duplicate elements in Python tuples easily. You can learn it with the help of a simple Python tuple example below.
tup = (“Ankit”, 32, “Tesla”, “Toyato”, “Ankit”, 32)
print (tup) |
Read More: Difference Between Python and Tuples Data Structures
11. How to access Python tuple elements with negative index values?
Python tuples can easily be accessed by using negative index values. Let us understand it with a simple Python tuple example below.
tup = ( 2,4,6,7,9)
print (tup[ :: -1] ) |
Advantages of Python Tuples Over List
Check some of the best advantages of Tuples over the list below.
- The tuples cannot be changed and updated which means the data cannot be replaced which is very helpful in storing sensitive information. List can be changed after declaration.
- Tuples can be used to store multi valued data types while lists can only store homogeneous data types.
- You can iterate through tuples very fast as compared to the list.
- Tuples can be used with dictionary keys while it is not possible with lists.
- If you want to change the data of the tuple, it is only possible when you convert into list or other form of data structure.
Learn All About Python With PW Skills
Become a master in Python programming and Data Structures with the in-depth knowledge of this programming language and its framework. Get complete access to in-depth tutorials and practice exercise with the PW Skills Decode DSA With Python Course.
This self paced course is a complete package to help you prepare for a developer job in a top tech company. Become a certified developer with PW Skills certification and more only at pwskills.com
Python Tuple Example FAQs
Q1. What are Python tuples?
Ans: A Python tuple is an ordered, immutable and indexed collection of elements of different data types stored in a single variable. This data structure is very similar to lists in Python language.
Q3. Can we store duplicate elements in Python tuples?
Ans: We can store duplicate elements in Python tuples easily. Check the Python tuple example to know how we can store duplicate elements in tuples.
Q3. Are Python tuples immutable?
Ans: Yes Python tuples are immutable which cannot be changed once it is declared. You can easily try to manipulate or modify a tuple after declaring it, it will throw an error.
Q4. Can we access Python tuple elements using their index value?
Ans: Python tuple elements can be accessed using their index value and their indexing starts from the zero index by default. You can easily specify an index value and access elements.