Learn Pain Less

HomeOur TeamContact
Python
What is a Tuple in python | Tuple in Python
Pawneshwer Gupta
Pawneshwer Gupta
September 10, 2022
2 min
What is a Tuple in python | Tuple in Python

Understanding the Fundamentals of Tuples in Python

In Python, tuples are an essential data structure that helps organize and manage data. If you’re new to Python, it’s crucial to understand the basics of tuples and how they work. In this article, we’ll explore what tuples are, how they differ from other data structures like lists, and how you can use them effectively in your Python programs.

What is a Tuple?

A tuple is a collection of ordered and immutable elements that are enclosed in parentheses. It can store any type of data, including integers, strings, floats, and even other tuples. Once you create a tuple, you cannot modify its values. This feature makes tuples an excellent choice for storing data that should not be changed once it’s been set.

Tuples are similar to lists in many ways. The main difference between them is that tuples are immutable, while lists are mutable. This means that you can add, remove, or modify elements in a list, but you cannot do the same with a tuple. Tuples are also faster than lists when it comes to accessing elements because they use less memory.

Tuple is one among the 4 data types that Python has built in to store data collections. The other 3 are List and Set. Each type has different properties and uses.

A tuple can be described as a collection that is ordered and unchangeable .

Round brackets are used to write tulles.

Python Tuple consists of a collection separated by commas. A tuple can be compared to a list for its indexing, repetition, and nested objects. However, unlike lists that are mutable, a Tuple is immutable.

Creating Tuples in Python

To create a tuple we will use () operators.

mytuple = ("JAVA", "Python", "Kotlin", "NodeJS")
print(mytuple)

Accessing Values in Tuples in Python

Method 1: Using Positive Index

Using square brackets we can get the values from tuples in Python.

mytuple = ("JAVA", "Python", "Kotlin", "NodeJS")
print("Value in mytuple[0] = ", mytuple[0])
print("Value in mytuple[1] = ", mytuple[1])
print("Value in mytuple[2] = ", mytuple[2])
print("Value in mytuple[3] = ", mytuple[3])

Output

Value in mytuple[0] = JAVA

Value in mytuple[1] = Python

Value in mytuple[2] = Kotlin

Value in mytuple[3] = NodeJS

Method 2: Using Negative Index.

In the above methods, we use the positive index to access the value in Python, and here we will use -ve index within [].

mytuple = ("JAVA", "Python", "Kotlin", "NodeJS")
print("Value in mytuple[-4] = ", mytuple[-4])
print("Value in mytuple[-3] = ", mytuple[-3])
print("Value in mytuple[-2] = ", mytuple[-2])
print("Value in mytuple[-1] = ", mytuple[-1])

Output

Value in mytuple[-4] = JAVA

Value in mytuple[-3] = Python

Value in mytuple[-2] = Kotlin

Value in mytuple[-1] = NodeJS

Concatenation of Tuples in Python

To concatenate the Python tuple we will use plus operators(+).

# Code for concatenating 2 tuples
tuple1 = (0, 1, 2, 3)
tuple2 = ('JAVA', 'Python')
# Concatenating above two
print(tuple1 + tuple2)

Output

(0, 1, 2, 3, ‘JAVA’, ‘Python’)

Slicing Tuples in Python

# code to test slicing
tuple1 = ("JAVA", "Python", "Kotlin", "NodeJS")
print(tuple1[1:])
print(tuple1[::-1])
print(tuple1[2:4])

Output

(“Python”, “Kotlin”, “NodeJS”) (“NodeJS”, “Kotlin”, “Python”, “JAVA”) (“Kotlin”, “NodeJS”)

Deleting a Tuple

# code to test slicing
tuple1 = ("JAVA", "Python", "Kotlin", "NodeJS")
del tuple1
print(tuple1)

Output

NameError: name ‘tuple1’ is not defined

Finding Length of a Tuple

# Code for printing the length of a tuple
tuple1 = ("JAVA", "Python", "Kotlin", "NodeJS")
print(len(tuple1))

Output

4

Converting list to a Tuple

# Code for printing the length of a tuple
list1 = ["JAVA", "Python", "Kotlin", "NodeJS"]
print(tuple(list1)) #typecasting using tuple keyword

Output

(“JAVA”, “Python”, “Kotlin”, “NodeJS”)

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

tuple

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

Understanding Python deque (Double-Ended Queue)
Understanding Python deque (Double-Ended Queue)
August 23, 2023
2 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media