AOIT Introduction to Programming Unit 3, Lesson 9 Sequences—Lists and Tuples Copyright © 2009–2012 National Academy Foundation. All rights reserved.

Slides:



Advertisements
Similar presentations
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Advertisements

Dictionaries: Keeping track of pairs
Tuples. Tuples 1 A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only difference is that tuples can't be.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
HANGMAN Project by Lori Kraus 12/3/03. WWWWe’ve played the game, now let’s see what goes on behind the scenes…. O \ | / | d b …but first, a few new.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Guide to Programming with Python
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Costas Busch - LSU1 Languages. Costas Busch - LSU2 Language: a set of strings String: a sequence of symbols from some alphabet Example: Strings: cat,
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
ActionScript: For Loops, While Loops, Concatenation and Arrays MMP 220 Multimedia Programming This material was prepared for students in MMP220 Multimedia.
1 String Processing CHP # 3. 2 Introduction Computer are frequently used for data processing, here we discuss primary application of computer today is.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Programing: An Introduction to Computer Science
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
String and Lists Dr. José M. Reyes Álamo.
Tuples and Lists.
Chapter 4 Lists In this chapter, we look at a means of structuring and accessing a collection of data. In particular, we look at a way of organizing.
CS 100: Roadmap to Computing
Chapter 4 Strings & Tuples
Languages Prof. Busch - LSU.
Languages Costas Busch - LSU.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Engineering Innovation Center
Siti Nurbaya Ismail Senior Lecturer
Introduction to Strings
Introduction to Python
Lists in Python.
CHAPTER THREE Sequences.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Data types Numeric types Sequence types float int bool list str
8 – Lists and tuples John R. Woodward.
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS1110 Today: collections.
CHAPTER 4: Lists, Tuples and Dictionaries
Introduction to Computer Science
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Python Review
CS 100: Roadmap to Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Class code for pythonroom.com cchsp2cs
Introduction to Computer Science
Presentation transcript:

AOIT Introduction to Programming Unit 3, Lesson 9 Sequences—Lists and Tuples Copyright © 2009–2012 National Academy Foundation. All rights reserved.

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?

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

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?

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.

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.

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

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?

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.

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

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