Python Tutorial
About Lesson

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.

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
Python Tuple – Creating Tuples in Python

 

 

 

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.

 

 

 

This is the result.

Python Tuple
Python Tuple

 

 

 

If you want to access the last item you can use negative indexing.

 

 

 

Also you can do ranging index.

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
Tuples in Python

 

 

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.

 

 

 

This is the result.

Convert Tuple to List
Convert Tuple to List

 

 

 

Joining Python Tuples

You can join two tuples in Python.