Python Tuples: An Overview with Examples and Code Snippets - 2023

Article by: Manish Methani

Last Updated: October 4, 2021 at 10:04am IST
2 min 23 sec read

Tuples are an essential data structure in Python that allow you to store a sequence of immutable elements. In this tutorial, you will learn what tuples are and how to use them in Python. We will also explore some of the most commonly used methods of tuples.

Creating a Tuple in Python

To create a tuple in Python, you use parentheses () and separate the elements with commas. You can also create a tuple using the built-in tuple() function.

# Creating a tuple using parentheses
my_tuple = (1, 2, 3, 4, 5)

# Creating a tuple using the tuple() function
my_other_tuple = tuple([6, 7, 8, 9, 10])

Accessing Elements of a Tuple in Python

You can access the elements of a tuple in Python using indexing. Indexing in Python starts from 0. You can also use negative indexing to access elements from the end of the tuple.

# Accessing the first element of my_tuple
print(my_tuple[0])

# Accessing the last element of my_other_tuple
print(my_other_tuple[-1])

Commonly Used Tuple Methods in Python

Tuples in Python have several built-in methods that allow you to manipulate them. Here are some of the most commonly used methods:

  1. count() - returns the number of times a specified element appears in the tuple.
# Counting the number of times 3 appears in my_tuple
print(my_tuple.count(3))

       2. index() - returns the index of the first occurrence of a specified element in the tuple.

# Finding the index of the first occurrence of 7 in my_other_tuple
print(my_other_tuple.index(7))

In conclusion, Python Tuples are ordered and immutable collections of values, similar to lists, but with some key differences. The main advantage of using tuples over lists is that tuples are immutable, which means that once they are created, their contents cannot be changed. This property makes tuples more efficient for certain operations and also allows them to be used as keys in dictionaries.

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>