Explaining Tuples in Python

Free Video Tutorial and Guide

In this video, we're going to explain what Tuples are in Python

Video Transcription

Hi, my name is Art, and I teach Python at Noble Desktop. In this video, I'm going to explain to you two data structures: lists and tuples. Sometimes people call them tuples, but no matter what you call them, you should just understand their behavior.

Let's start with a list. Suppose we have a Python list like 10, 20, 30. Remember, a list is mutable and a sequential data type, meaning a sequence of items separated by a comma. We could grab the first item (10) and easily reassign the value to 100.

Now let's convert this list to a tuple. To do this, we use the Python built-in function called "tuple". Visually, there's no big difference because we still have the same items, but if you take a closer look, you'll see that when we initialize a list we use square brackets and when we use a tuple, we use round brackets.

Now, if we want to index the first item (which is now 100) and reassign it to 10, it won't work. That's because a tuple is immutable, meaning it cannot be changed.

In real life, tuples are used when you fetch data from a relational database. That data will come as a tuple. If you're not sure, you can use the "type" command to see that it is a tuple.

To access data from a tuple, you can either use indexing (e.g. 0, 1, 2) or you can use simultaneous assignment (e.g. providing variable names).

You can also iterate through a tuple using a "for" loop.

So, bottom line is that you need to understand the difference between a list and a tuple. A list is mutable and a tuple is immutable.

We'll be talking about other data structures in my videos, so please watch all of them and we'll be talking about strings too. Thank you, and I'll see you next time.

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram