Explaining Tuples in Python

Step-by-step Python programming tutorial

Explore the key differences between lists and tuples in Python, and how they apply to real-life scenarios such as fetching data from a relational database. Understand the unique behavior of these two data structures and their role in programming and data management.

Key Insights

  • Lists in Python are mutable sequential data types, meaning their values can be reassigned. They are represented by values separated by a comma and enclosed in square brackets.
  • Tuples, on the other hand, are immutable data types, meaning once assigned, their values cannot be changed. They are also represented by values separated by a comma but are enclosed in round brackets.
  • The built-in Python function "tuple" is used to convert a list into a tuple.
  • In practical applications, tuples are commonly used when fetching data from a relational database. The fetched data is presented as a tuple.
  • Data from a tuple can be accessed either through indexing or through simultaneous assignment, which involves providing variable names.
  • Iterating through a tuple can be done using a "for" loop.

Tuples vs Lists

Behavior
Tuple Immutable. Use parentheses or commas. Hashable.
List Mutable. Use square brackets. Not hashable (can't be dict key).

Play video

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.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

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.

Topics

Learn more in these courses

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