From the course: Programming Foundations: Data Structures

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Tuples in Python

Tuples in Python

- [Instructor] Another array like data structure in Python is the tuple. Similar to a list, a tuple allows you to store a collection of values, but with one key difference. It's immutable, meaning its contents cannot be changed. This makes tuples ideal for storing values that should remain constant, such as coordinates or RGB values. To create a tuple in Python, we use parentheses and separate each value with a comma. For example, here we store the values 5 and 2 in a tuple. Once it's created, it cannot be modified. To access individual values in the tuple, we can use indexing, just like with the list. We'll access the first value at index 0, and then the second at index 1. Here x has the value 5 and y has the value 2. Let's see that in the console. 5 and 2. Now, besides storing values, tuples are also useful for returning multiple values from a function. Let's create a function that returns both the area and perimeter of a square. To return both of these values, we can use a tuple…

Contents