Lecture-6-Lists and Tuples
Lecture-6-Lists and Tuples
▪ Sequence:
▪ An object that contains multiple items of data
▪ The items are stored in sequence one after another
▪ Python provides different types of sequences,
including lists and tuples
▪ The difference between these is that a list is mutable and a
tuple is immutable
Introduction to Lists
▪ Mutable sequence:
▪ The items in the sequence can be changed
▪ Lists are mutable, and so their elements can be changed
▪ An expression such as
list[1] = new_value can be used to assign a new
value to a list element
▪ Must use a valid index to prevent raising of an IndexError
exception
Concatenating Lists
▪ Two-dimensional list:
▪ A list that contains other lists as its elements
▪ Also known as Nested List
▪ Common to think of two-dimensional lists as having rows and
columns
▪ Useful for working with multiple sets of data
▪ To process data in a Two-dimensional list need to use two
indexes
▪ Typically use nested loops to process
Two-Dimensional Lists
Two-Dimensional Lists
Subscripts for each element of the scores list
Tuples