Python Tuples

In this Python tutorial we will learn about Python Tuples and we will also cover different examples related to Python Tuples.

Python Tuples

In Python the tuple are same as a list in term of indexing, nested objects and it is a group of objects that are separated by commas. The tuple are immutable but the lists are mutable.

Example:

In the following example we are making the tuple which are the collection of the objects which are separated by commas.

  • tup = (“Python”, “T”, “point”) is used to create a tuple.
  • print(tup) is used to print the tuple by using print() function.
# Python Tuple
tup = ("Python", "T", "point")
# Printing tuple
print(tup)

Output:

After running the above code we get the following output in which we can see that the tuples are creating that is separated by commas.

Python Tuple
Python Tuple

Github Link

Check this code in Repository from Github and you can also fork this code.

Github User Name: PythonT-Point

Creating Python Tuples

The python tuples are created with the help of () operator and it is the collection of objects and are separated by commas.

Example:

In the following example we are creating the python tuple with the help of tuple[] statement and then print the tuple with the help of print() function.


tup : tuple[int | str, ...] = (2,4,6,"Pythontpoint")
 
print(tup)

Output:

In the following output we can see that the python tuple is created, we are defining a variable called values that can holds the tuple.

Creating Python Tuple
Creating Python Tuple

Read: Python Dictionary

Accessing values in Python Tuples

We are accessing the values in python tuples and we creating the tuple by using the square brackets, we can get the values from the tuples.

Example:

In the following example we will access the values in tuples, using the square brackets, we can get the values from the tuples.

tup = ("Python", "t", "point")
 
print("Value in tup[0] = ", tup[0])
print("Value in tup[1] = ", tup[1])
print("Value in tup[2] = ", tup[2])

Output:

After running the above code we get the following output in which we can see that the values are accessing in the python tuple.

Accessing the values in python tuple
Accessing the values in python tuple

Read: Python Strings

Slicing Python Tuples

After accessing the values in the tuple here we are slicing the python tuple, using the square brackets, we can get the values from tuple.

Example:

In the following code we will creating the tuple and getting the values from the tuple by using the square bracket.

# slicing the tuple
 
tup = (1,2, 3, 4)
print(tup[2:])
print(tup[::-2])
print(tup[1:3])

Output:

After running the above code we get the following output in which we can see that the tuple can be sliced.

Slicing the python tuple
Slicing the python tuple

Read:Python Literals

Deleting Python Tuples

Here we are deleting the tuples simply by using the del statement and then printing this delete tuple by using the print() function.

Example:

In the following example we are deleting the tuple. The tuple can be deleted by using the del statement.

# deleting a tuple
 
tup = ( 1, 2)
del tup
print(tup)

Output: After running the above code we get the following output in which we can see that the error occur because after using del statement the tuple is deleted that’s why the error is occur.

Deleting the Python tuples
Deleting the Python tuples

Finding length of Python Tuple

Here we are finding the length of the Python tuple. For finding the length we are using the len() function.

Example:

In the following code we will finding the length of the python tuple by simply using the len() function and after finding the length print this length by using print() function.

# printing the length of a tuple
 
tup = ('python', 't', 'point')
print(len(tup))

Output:

After running the above code we get the following output in which we can see that the length of the tuple is printed on the screen.

Finding length of Python Tuple
Finding length of Python Tuple

So, in this tutorial, we have learned about the Python Tuples and we have covered all these topics.

  • Python Tuples
  • Creating Python Tuples
  • Accessing values in Python Tuples
  • Slicing Python Tuples
  • Deleting Python Tuples
  • Finding length of Python Tuple

Do follow the following tutorials also:

Comments are closed.