In this 100 days of code lesson, the instructor introduces the tuple, a Python data type similar to the list but unchangeable, created with round brackets.
He shows that a single value in brackets becomes an int, so a one-element tuple needs a trailing comma, which he calls the biggest confusion of Python developers.
Assigning to an item raises a TypeError saying the tuple object does not support item assignment, and he explains that tuples are useful when you want values, like a constant list in a complex program, never to change by mistake.
Indexing, negative indexing, the in keyword and slicing all work as with lists, with an out-of-range index raising an error and slicing returning a new tuple rather than altering the original.
He closes by asking viewers to revise lists using tuples and to remember that tuples and strings are immutable while lists are mutable.
Brief overview
Python tuples work like lists for indexing, slicing and membership checks, but they cannot be changed.
A one-element tuple needs a commaWithout the comma, Python treats the value in brackets as an int, not a tuple.
Tuples cannot be changedItem assignment raises a TypeError; you can only create a new tuple.
Most list operations still applyIndexing, negative indexing, the in keyword and slicing work exactly as with lists.
Questions this recording answers
8 questions, each answered where it is said
How do you create a tuple with only one element in Python?
You must add a comma after the value, like 1 comma inside the brackets. Without it, Python cannot tell whether you are writing one value in brackets or a new tuple, so it creates a class int instead. The instructor calls this the biggest confusion of Python developers.
What is the difference between a tuple and a list?
A tuple is very similar to a list, but you cannot change it. With a list, assigning a new value to index 0 works, but with a tuple Python raises a TypeError saying the tuple object does not support item assignment. Tuples are immutable, while lists are mutable.
Why would you use a tuple instead of a list?
Sometimes you intentionally want things not to change. For example, when working on a very complex program, you might want a kind of constant list that nobody changes by mistake. In that case the instructor recommends using a tuple, since its values cannot be modified.
What happens when you access a tuple index that does not exist?
Printing an index such as 34 on a short tuple causes an error saying the tuple index is out of range. The program still runs up to that line, so the earlier prints show 1, 2 and 76, the values at the 0th, 1st and 2nd index.
How does negative indexing work for tuples?
It works exactly as with lists. To resolve an index like minus 1, take the tuple's length and subtract. In the demo, printing len gives a length of 7, so minus 1 becomes index 6, counting 0, 1, 2, 3, 4, 5, 6 to find the element.
How do you check whether an item is in a tuple?
Use the in keyword with an if condition, for example checking if 342 is in the tuple. Since 342 is present, the message prints. If you check a value like 3421 that is not present, the condition evaluates to false and nothing is printed.
Does slicing a tuple change it?
No. Tuples can be sliced like lists, but slicing returns a new tuple and leaves the original unchanged. In the demo, slicing from 1 to 4 creates a new tuple containing 2, 76 and 342, following the same rules as list slicing, including blank start and end values.
What does Python assume when a slice start or end is left blank?
If the start is blank, Python automatically puts 0 there. If the end is blank, it uses the length of the tuple. The instructor notes this is exactly the same as he explained for lists, and negative indices can be converted into positive ones first.
Key Quote
“This is the biggest confusion of Python developers.”
— Instructor
Key Quote
“sometimes you'll want things to not change intentionally.”
— Instructor
Key Quote
“After slicing, a new tuple is returned. So, the tuple doesn't change.”