Download presentation
Presentation is loading. Please wait.
Published byJuliet Hicks Modified over 8 years ago
1
AOIT Introduction to Programming Unit 3, Lesson 9 Sequences—Lists and Tuples Copyright © 2009–2012 National Academy Foundation. All rights reserved.
2
Sequences are ordered collections of values Strings are one type of sequence. You already know about strings: firstname = "Emily" What is the string in this statement? But how is this string an “ordered collection of values,” and why is that important?
3
Strings can be “ordered collections of values” Sometimes you’re interested in the entire string: firstname = "Emily" Sometimes you’re interested in the individual characters: E m i l y For example, you might want to know: Does Emily have a y in it? How can you identify the l in Emily? What are the values here? How is this a collection? How is the collection ordered? Why is this important? Suppose you were playing hangman...
4
Individual letters are important in hangman Indexing lets you identify individual letters in a string: firstname = "Emily" E m i l y firstname[0] is E, firstname[4] is y What is the variable reference for m? What is the first subscript of an index? If you were writing a program to play hangman, how could you use this information?
5
Here’s how indexing might work in hangman Current scenario: mystery_word = "cat" letters = "_ _ _" current_user_guess = "a" Algorithm steps: 1.Figure out whether the mystery word has an a in it. 2.Identify the matching letter (a) in the sequence as mystery_word[1]. 3.Put the letter in the appropriate place in the letters string. Go back through the algorithm steps with a mystery word of antelope and a guess of p.
6
Indexing is also used with lists Indexing with strings: string = "antelope" Indexing with lists: numbers = [5, 9, 17, -21, 0, -99] favorite_foods = ["pizza", "hamburgers", "spaghetti"] # string[3] is "e" # numbers[5] is -99 What is the value of favorite_foods[1]? Explain how numbers and favorite_foods are lists.
7
Lists are “mutable” sequences of values Mutable means “able to be changed.” For example, here are some lists you might want to change: results_100yards = [6.5, 7, 8.5, 10, 12.3] shuffled_cards = [0, 43, 51,... 2, 3] mypets = ["hamster", "goat", "snake"] word_length = [] Example of changing: mypets[1] = "elk" Why might you want to change each of these lists? # initializes a list to null
8
Tuples are “immutable” sequences of values Immutable means “not able to be changed.” Here are some tuples you’ll be using in future programs: rank_string = ("ace","two","three",... "queen","king") suit_string = ("clubs","diamonds","hearts","spades") animal_words = ("horse","cow","dog","cat","hyena") Why are these tuples and not lists? What is one important observable difference between tuples and lists? Would you make the text art for hangman (the scaffold and stick figure) a list or a tuple? Why?
9
Noting parentheses is important with sequences Assigning a string: firstname = "Emily" Assigning a list: shuffled_cards = [0, 43, 51,... 2, 3] Assigning a tuple: suit_string = ("clubs","diamonds","hearts","spades") Indexing strings, lists, and tuples: firstname[1] is "m"; shuffled_cards[2] is 51; suit_string[3] is "spades" Explain the significance of the parentheses and square brackets in all these examples.
10
Operations and functions or methods make sequences more powerful You can concatenate lists and tuples, just as you can concatenate strings (for example, "ab" * 3). To find the first (and only the first) element: find() method s = "moose" s.find("o") results in 1 To determine length: len() function Given suits = ("clubs","diamonds","hearts","spades") Then len(suits) is 4 To test for membership: x in s (“is element x in sequence s?”) Given animal = "cat" Then "a" in animal is True
11
Sequences are useful and powerful constructs Sequences can be strings, lists, or tuples String example: firstname = "Emily" List example: shuffled_cards = [4,1,0,3,51...2] Tuple example: animals = ("cat","dog","moose") Sequences use indexing (subscripting) Positive (left-to-right) indexes start at 0 Sequences can be concatenated using + and * Functions and methods make sequences more valuable
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.