EC Lecture Notes - Lecture 2: 5,6,7,8, Tuple
Document Summary
Tuples are very similar to lists, except that they are immutable (they cannot be changed). Also, they are created using parentheses, rather than square brackets. You can access the values in the tuple with their index, just as you did with lists. Trying to reassign a value in a tuple causes an error. Like lists and dictionaries, tuples can be nested within each other. Tuples can be created without the parentheses by just separating the values with commas. Tuples are faster than lists, but they cannot be changed. Tuple unpacking allows you to assign each item in a collection to a variable. Example: numbers = (1, 2, 3) a, b, c = numbers print(a) print(b) print(c) This can be also used to swap variables by doing a, b = b, a , since b, a on the right hand side forms the tuple (b, a) which is then unpacked.