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.

Learn Flutter, FlutterFlow,
Firebase & Angular.

Looking to learn how to create the production-ready apps
from scratch? Well, you are at the right place.

₹1299 or $15.56

Learn to create the Dating App using No-Code Tool FlutterFlow

Level: Advanced

Explore Curriculum
₹1299 or $15.56

Flutter Mobile App Development Course

Explore Curriculum
₹1299 or $15.56

The Complete Angular Course

Explore Curriculum
₹1299 or $15.56

Dart Programming for Absolute Beginners

Coming Soon
FREE

Next.js course

Start Watching
FREE

Fundamentals of Computer Programming Languages

Start Watching
FREE

Learn HTML, CSS & Bootstrap

Start Watching

Latest Web Stories

1

Learn how to open WhatsApp using FlutterFlow | Step by Step Guide

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

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer