+91 81900 98289

Python Tuples

This Python Tuples lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

A tuple is an ordered collection that you do not change after creating it. Parentheses are the usual spelling. A trailing comma makes a one-item tuple.

Use a tuple for a fixed pair, such as a coordinate or a row you only want to read.

Example 1: Start here

Trying to assign point[0] = 0 raises an error. That is the point of a tuple.

point = (13.08, 80.27) # a tuple; a fixed group of values print(point[1]) lat, lng = point # unpack the values into these names print(lat)

Example 2: A fixed pair

A tuple is an ordered group you read by index, like a list.

point = (10, 24) # a tuple; a fixed group of values print(point[0], point[1]) # a comma prints each value, with a space between

Example 3: Unpack a tuple

Unpacking gives each item its own name.

city, seats = ("Chennai", 24) # a tuple; a fixed group of values print(city, seats) # a comma prints each value, with a space between

Example 4: One item needs a comma

Without the comma, parentheses only group an expression. The comma makes a tuple.

single = ("Python",) # a tuple; a fixed group of values print(type(single)) # type() shows what kind of value this is print(len(single)) # len counts the items
Trying to assign point[0] = 0 raises an error. That is the point of a tuple.
Call Now 81900 98289