1 / 6

Lists in Python

Lists in Python. 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

Download Presentation

Lists in Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

More Related