60 likes | 174 Vues
Python lists are sequential collections of values that are mutable and can contain elements of varying types. Each element has a unique index ranging from 0 to n-1, where n is the length of the list. Lists can include sublists and are defined using square brackets, such as `myList = [3, 'a', True]`. You can access elements using positive or negative indexes, modify them through assignment, and determine the length with the `len()` function. Lists facilitate diverse data manipulation in Python programming.
E N D
Lists = arrays • A list is a sequential collection of values, it is a data structure • Each value has a location (an index) • Indexes range from 0 to n-1 (where n is the length of the list) and from -1 to -n • Lists are heterogeneous = values can be of any type (strings are homogeneous because their elements are characters)
List syntax • Values are enclosed in [], myList = [3, ‘a’, True] • One list can contain another • Empty list = [] • Any type of data can be in the list • You usually refer to a list by elements, that is with the []. You can refer to a list by its name (as one whole thing) when passing it as an argument to a function.
List semantics • Lists are mutable, that is, elements can be changed • Individual elements can be changed the same way any variable can be changed, with an assignment statement • myList= [1,9, ‘a’, 4, 7] • m = 3 • myList[m] = 99 • myList[m+1] = 88
Length function • The same as with the string type, len (lst) will return an integer, the number of elements in the list, from zero up to some max int value • If a list contains a sublist, it counts as one element L = [1, [3, 4], 7] has 3 elements • You call it as part of another statement, for example, print(len(mylist))
Accessing elements of a list • The index starts at 0 just like with a string and goes up to n-1 if there are n elements in the list • They also can use negative subscripts, -1 is the last element on the right, -n on the left end