Tuples in python

Tuples in python

Introduction

“Never stop learning, because life never stops teaching”

In this article, we will discuss a unique data type called tuples with all its unique properties.

Tuple Definition

Tuples are the same as the list except a tuple is immutable(which means it cannot be modified).

We represent tuple with round brackets().

The syntax for creating a tuple

variable_name=(List of values separated by a comma)

Here, the tuple will be saved in a variable and can be used in the future.

Example

fruits=("apple","orange","pine-apple","grapes"])

Here, fruits is a tuple containing a list of fruits.

Properties

  • A tuple can be accessed the same as a list, with help of indexing and loops. You can check the article for the list to cover the topic in detail.
  • A tuple can also use the slicing technique.
  • Since a tuple cannot be modified, we cannot insert, update, modify, and delete the elements of a tuple.

Why use a tuple instead of a list?

This is the most common question among many. Here, is the answer,

  • Program execution is faster in a tuple than in a list. (This is probably not going to be noticeable when the list or tuple is small.)
  • You can use a tuple when you feel the values should not be changed. It avoids even accidental modifications.
  • We can use a tuple as the dictionary’s key as it is not changeable. Please note, that we cannot use a list, dictionary, or set as a dictionary’s key.
See also  Comments and Print function

Creation, packing, and unpacking

Creation

To create an empty tuple,

fruits=()
print(fruits)#()

To create a tuple with a single value,

fruits=("apple",)
print(fruits)

Please note, that a single value tuple should always end with a comma, let us check what happens if we don’t write a comma,

fruits=("apple",)
print(type(fruits))#<class 'tuple'>
fruits=("apple")
print(type(fruits))#<class ‘str’>

To create multiple values tuple,

fruits=("apple","orange","grapes")
print(fruits)#('apple', 'orange', 'grapes')

Packing

Packing is joining many elements to a single variable to form a tuple.

fruits=("apple","orange","grapes")

The pictorial representation is,

Unpacking

If we want to assign all the tuple elements to different variables, it is called unpacking.

f1,f2,f3=fruits
print(f1)#apple
print(f2)#orange
print(f3)#grapes

The pictorial representation is,

Please note, that the number of elements must be the same, or else it will throw an error.

Packed and unpacking

We can do both packing and unpacking together which will lead to multiple variable initializations in a single line.

f1,f2,f3=("apple","orange","grapes")
print(f1)#apple
print(f2)#orange
print(f3)#grapes

Swapping in python

Usually, swapping is always done with the help of ternary variables in all the other programming languages.

In python, swapping is made easy with the help of tuple packing and unpacking.

First, let us see how to swap with the ternary variable.

a=10
b=20
print("Before swapping:",a,b)
temp=a
a=b
b=temp
print("After Swapping",a,b)
Before swapping: 10 20
After Swapping 20 10

Swapping easily with python.

a=10
b=20
print("Before swapping:",a,b)
a,b=b,a
print("After Swapping",a,b)
Before swapping: 10 20
After Swapping 20 10

Practice yourself

  • Write a program to find the length of a tuple.
  • Find the maximum and minimum elements in a tuple.
  • Find a way to change a particular element in a tuple.
  • Add a tuple to a list and add a list to a tuple.
  • Write a program to find if the tuple has duplicate elements.

Conclusion

Thus, we have seen in detail the tuple. You can fork and use this repl link to explore more about the list in python. You can write your doubts in the comments and can eagerly expect a reply from your friends.

See also  List In Python

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh