From the course: Programming Foundations: Data Structures (2023)

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Tuples in Python

Tuples in Python

- [Instructor] Another array-like data structure in Python is a tuple. A tuple is similar to a list, but it's immutable. This is helpful for storing a collection of values that should not be changed. For example, coordinates or RGB values. To create a tuple in Python, we use parentheses and separate each value with a comma. We'll call this a point. Now that it's been created, we cannot modify it. To access each value in a tuple, we use indexing just like a list. We'll say X is at index zero, and Y is at index 1. In addition to storing values, tuples can also be used to return multiple values from a function. Let's create a function that returns the area and perimeter of a square. We'll call it calculate_square_properties and it'll take in a side length. With this length, we can compute the area and the perimeter. To return them both, we can use the tuple. We don't need to include the parentheses if we don't want to but…

Contents