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 - Python Tutorial
From the course: Programming Foundations: Data Structures
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
-
-
-
-
What is an array?2m 53s
-
Create a list in Python1m 48s
-
(Locked)
Retrieve data from a list in Python3m 39s
-
(Locked)
Mutate a list in Python2m 45s
-
(Locked)
Solution: Swap items2m 34s
-
(Locked)
Multidimensional lists in Python4m 36s
-
(Locked)
Tuples in Python2m 17s
-
(Locked)
Solution: Square items1m 52s
-
(Locked)
Search array-like structures3m 47s
-
(Locked)
Sort array-like structures3m 27s
-
(Locked)
Solution: Find the second smallest item2m 2s
-
(Locked)
When to use array-like structures1m 38s
-
-
-
-
-
-
-
-