In this Python Tutorial we want to learn about Creating Tuples in Python, so first of all what are tuples ? tuples are a collection of data value which is immutable, it means that we can not change a tuple and it is unchangeable. you can use parenthesis for creating tuples in python.
These are some key points to keep in mind about tuples in Python:
- To create tuple, you can use parentheses to enclose the elements, separated by commas.
- Tuples are indexed, meaning you can access individual elements using their index, starting from 0.
- Tuples are immutable, which means that you cannot add, remove, or modify elements after the tuple has been created.
- Tuples can contain elements of any data type, including other tuples.
- Tuples can be used as keys in dictionaries, which is not possible with lists.
Tuples are useful in situations where you need to represent a fixed set of values that will not change. Because tuples are immutable, they can be more efficient than lists in certain situations, such as when working with large datasets or when you want to ensure that the data cannot be accidentally modified.
While tuples have some limitations compared to lists, they have a number of useful features that make them an important data type in Python. By understanding how tuples work and how to use them effectively, you can take advantage of their strengths and improve the quality and efficiency of your code.
OK now let’s create our first Tuple in Python.
1 2 3 |
languages = ("Python", "Java", "C++", "C#", "Ruby", "Kotlin") print(type(languages)) print(languages) |
In the above code we have created a simple tuple of programming language, also we have printed the type, you can see that we are receiving a tuple class for the type.
Run the code and this is the result.
![Python Tuple - Creating Tuples in Python](https://geekscoders.com/wp-content/uploads/2020/10/python-tuple-creating-tuples-in-python.jpg)
Accessing a Tuple Item
You can access a tuple item by using the index of that item, and indexing starts from 0, for example in here i want to access item 0 and 2 from the tuple.
1 2 3 |
languages = ("Python", "Java", "C++", "C#", "Ruby", "Kotlin") print(languages[0]) print(languages[2]) |
This is the result.
![Python Tuple](https://geekscoders.com/wp-content/uploads/2020/10/python-tuple.jpg)
If you want to access the last item you can use negative indexing.
1 2 3 |
languages = ("Python", "Java", "C++", "C#", "Ruby", "Kotlin") print(languages[-1]) print(languages[-2]) |
Also you can do ranging index.
1 2 |
languages = ("Python", "Java", "C++", "C#", "Ruby", "Kotlin") print(languages[1:4]) |
Now this will start from index 1 and end at index 4 but it will not include index 4 item.
This is the result.
![Tuples in Python](https://geekscoders.com/wp-content/uploads/2020/10/tuples-in-python.jpg)
Converting Python Tuple to Python List
As i have already said that you can not change tuples, for example if you want to add a new item in python tuples, you can not do that, because tuples are immutable, but there is a ways that you do this. you can convert your tuples to Python List, as you know you can change a list in python, after that add your item and than you can convert list to tuple.
1 2 3 4 5 6 7 8 9 |
lanlist = list(languages) print(type(lanlist)) lanlist.insert(0, "Inserted") print(lanlist) tuplelist = tuple(lanlist) print(type(tuplelist)) print(tuplelist) |
This is the result.
![Convert Tuple to List](https://geekscoders.com/wp-content/uploads/2020/10/convert-tuple-to-list.jpg)
Joining Python Tuples
You can join two tuples in Python.
1 2 3 4 5 |
languages = ("Python", "Java", "C++", "C#", "Ruby", "Kotlin") lang2 = ("PHP", "HTML", "CSS") tuple3 = languages + lang2 print(tuple3) |