Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists in Python.

Similar presentations


Presentation on theme: "Lists in Python."— Presentation transcript:

1 Lists in Python

2 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)

3 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.

4 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

5 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))

6 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


Download ppt "Lists in Python."

Similar presentations


Ads by Google