1 / 2

Sequences and Indexing

Sequences and Indexing. Sequences are powerful because they let you refer to an entire collection , as well as the items in the collection, using a single name . You can still get to the items (aka elements ) in the collection, by indexing : colors = [ 'red' , 'white' , 'blue ' ]

nyoko
Download Presentation

Sequences and Indexing

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. Sequencesand Indexing Sequences are powerful because they let you refer to an entire collection, as well as the items in the collection, using a single name. • You can still get to the items(aka elements)in the collection, by indexing: • colors =['red', 'white', 'blue'] • colors[0]has value 'red' • colors[1]has value 'white' • colors[2]has value 'blue' ] colors [ 1 Indexing starts at zero, not at one The number (or variable) inside the square brackets is called the index.

  2. Avoid this Gotcha – an “off by one” error in referring to the LAST element of a sequence cool_words =['aplomb', 'eviscerate', 'pataflafla', 'tmesis'] last_word= cool_words[3] # Correct! last_word = cool_words[len(cool_words) - 1] # Correct! last_word = cool_words[4] # WRONG!!! last_word = cool_words[len(cool_words)] # WRONG!!! Note! The wrong statements above generate an error message: IndexError: list index out of range Cool words taken from:www.vocabula.com/vrbestwords.asp

More Related