Python Tuples

Python · Tuples

Theory

A tuple is an ordered, immutable sequence, usually written with parentheses: coordinates = (28.6, 77.2). Like a list it supports indexing, slicing, len(), in and loops, but once created you cannot add, remove or replace items; coordinates[0] = 0 raises TypeError. It is actually the comma that makes a tuple, not the parentheses: (5) is just the int 5, while (5,) or 5, is a one-item tuple.

Immutability has practical benefits. A tuple of immutable values is hashable, so it can be a dictionary key or a set member, e.g. {(28.6, 77.2): 'Delhi'}, which a list can never be. Tuples are slightly smaller and faster to create than lists and signal 'this record will not change'. Note that immutability is shallow: a tuple holding a list cannot swap that list for another object, but the list inside can still be modified, and such a tuple is no longer hashable. Unpacking assigns the items to names in one go: lat, lon = coordinates.

Why we use it

Some data is a fixed record: a latitude/longitude pair, an RGB colour, a (name, salary) row. A tuple stores it safely so nothing changes it by accident, and it can be used as a dictionary key.

When to use it

Use a tuple for fixed-size records, function return values with several parts, and dict keys made of several values. Use a list when the collection will grow, shrink or be edited. For records with named fields, consider collections.namedtuple or a dataclass.

How it works

  1. coordinates = (28.6, 77.2) creates a tuple
  2. Python stores two references in a fixed-size block
  3. Trying to change an item, e.g. t[0] = 0? Yes: TypeError: no item assignment. No: Reading works like a list.
  4. Unpack with lat, lon = coordinates
  5. print(coordinates) shows (28.6, 77.2)

Syntax

my_tuple = (item1, item2, item3)

Example

coordinates = (28.6, 77.2)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic