Lecture

Immutable Data Type, Tuple

A tuple is a data type in Python used to store multiple values in a specific order, similar to a list. However, unlike lists, tuples are immutable, meaning their contents cannot be changed after creation.

This immutability makes tuples ideal for storing fixed data, such as coordinates of a specific location or color values.


How do you define a tuple?

A tuple is created by enclosing elements in parentheses () and separating each element with a comma ,.

Tuple Declaration Example
my_tuple = (1, 2, 3) print(my_tuple) # (1, 2, 3)

You can also define a tuple without parentheses by simply separating the values with commas.

Tuple Declaration without Parentheses Example
my_tuple = 1, 2, 3 print(my_tuple) # (1, 2, 3)

How do you use a tuple?

Tuples are used to safely store values in a program that should not change.

For example, in a map-related program, you can use a tuple to store the latitude and longitude of a specific location.

Tuple Example
# Latitude and longitude of New York coordinates = (40.7128, 74.0060) # Print the first element, 40.7128 print("Latitude of New York:", coordinates[0]) # Print the second element, 74.0060 print("Longitude of New York:", coordinates[1])
Quiz
0 / 1

What is the correct characteristic of a tuple?

It is a mutable data type

It is an immutable data type

It is an unordered data type

You can add elements to it

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help