Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

Similar documents
Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

COMP1730/COMP6730 Programming for Scientists. Strings

CMPT 120 Lists and Strings. Summer 2012 Instructor: Hassan Khosravi

List Processing Patterns and List Comprehension

Introduction to String Manipulation

List Processing Patterns and List Comprehension

TUPLES AND RECURSIVE LISTS 5

Compound Data Types 1

Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Python for Non-programmers

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Programming in Python

Iteration Part 1. Motivation for iteration. How does a for loop work? Execution model of a for loop. What is Iteration?

Basic Python Revision Notes With help from Nitish Mittal

CS 115 Lecture 13. Strings. Neil Moore. Department of Computer Science University of Kentucky Lexington, Kentucky

Chapter 6: List. 6.1 Definition. What we will learn: What you need to know before: Data types Assignments

Introduction to Python Part I

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

Lecture #12: Immutable and Mutable Data. Last modified: Mon Feb 22 16:33: CS61A: Lecture #12 1

Strings. Genome 373 Genomic Informatics Elhanan Borenstein

The Practice of Computing Using PYTHON

CS1 Lecture 11 Feb. 9, 2018

Lists, loops and decisions

Working with Sequences: Section 8.1 and 8.2. Bonita Sharif

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

Slicing. Open pizza_slicer.py

CS1 Lecture 12 Feb. 11, 2019

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Fundamentals of Python: First Programs. Chapter 4: Strings (Indexing, Slicing, and Methods)

18.1. CS 102 Unit 18. Python. Mark Redekopp

Introduction to Computational Models Using Python

CSCA20 Worksheet Strings

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Statements 2. a operator= b a = a operator b

University of Texas at Arlington, TX, USA

COMP1730/COMP6730 Programming for Scientists. Sequence types, part 2

Lecture 10: Strings. CS1068+ Introductory Programming in Python. Dr Kieran T. Herley 2018/19. Department of Computer Science University College Cork

Lecture #11: Immutable and Mutable Data. Last modified: Sun Feb 19 17:03: CS61A: Lecture #11 1

Unit 2. Srinidhi H Asst Professor

Booleans, Logical Expressions, and Predicates

CSCE 110 Programming I Basics of Python: Lists, Tuples, and Functions

CSCE 110 Programming I

CIS192 Python Programming

Announcements COMP 141. Accessing Characters Review. Using len function 10/30/2017. Strings II. Reminders: Program 6 - due 11/2

MUTABLE LISTS AND DICTIONARIES 4

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

For strings (and tuples, when we get to them), its easiest to think of them like primitives directly stored in the variable table.

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han

Iterators & Generators

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

Accelerating Information Technology Innovation

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

Working with Lists 4

Introduction to programming using Python

STATS Data Analysis using Python. Lecture 8: Hadoop and the mrjob package Some slides adapted from C. Budak

Flow Control: Branches and loops

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Using Scala in CS241

Compsci 101 Fall 2015 Exam 1 Rubric. Problem 1 (24 points)

Lecture #10: Sequences. Last modified: Mon Feb 22 16:33: CS61A: Lecture #10 1

CS 1110: Introduction to Computing Using Python Lists and Sequences

The Pyth Language. Administrivia

Python: common syntax

Accelerating Information Technology Innovation

UNIT 5. String Functions and Random Numbers

Comp 151. Control structures.

Introduction to Python

ENGR/CS 101 CS Session Lecture 12

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Python Lists. What is not a Collection. A List is a kind of Collection. friends = [ 'Joseph', 'Glenn', 'Sally' ]

Introduction to: Computers & Programming: Strings and Other Sequences

Lecture 7: Python s Built-in. in Types and Basic Statements

Topic 7: Lists, Dictionaries and Strings

Python Programming: Lecture 2 Data Types

Sequence of Characters. Non-printing Characters. And Then There Is """ """ Subset of UTF-8. String Representation 6/5/2018.

Chapter 10: Strings and Hashtables

ENGR 102 Engineering Lab I - Computation

In addition to numbers and strings, Python also lets us represent lists of things (just like in AppInventor).

Script language: Python Data structures

Lecture 7. Memory in Python

THE AUSTRALIAN NATIONAL UNIVERSITY Mid Semester Examination September COMP1730 / COMP6730 Programming for Scientists

for loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

CS205: Scalable Software Systems

1 Strings (Review) CS151: Problem Solving and Programming

Review 4. Lists and Sequences

CS S-02 Python 1. Most python references use examples involving spam, parrots (deceased), silly walks, and the like

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS 1803 Spring 2011 February 14, 2011 Exam 1 Name: Section: Grading TA:

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

CSC A20H3 S 2011 Test 1 Duration 90 minutes Aids allowed: none. Student Number:

Test #2 October 8, 2015

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Python List built-in methods, accessing elements, selection, slicing, recursive functions, list comprehension

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Transcription:

Motivation: How to count the number of vowels in a word? Sequences and Loops o You re given words like 'boston', 'wellesley', 'needham', 'lynn', etc. o Tasks: o count the number of vowels in a word. o count the number of times a certain vowel appears in a word CS111 Computer Programming Department of Computer Science Wellesley College def countallvowels(word): # body here def countvowel(word, vowel): # body here?? Slides 8-3 to 8-12 explain what we need to know/learn to solve these problems. 8-2 Old friend: isvowel Indices: accessing characters in a string Indices and their properties. def isvowel(char): c = char.lower() return (c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u') def isvowel(char): c = char.lower() return c in 'aeiou' To think: How will the function isvowel be useful for solving our counting vowels problem? 8-3 In [1]: word ='boston' In [2]: word[0] Out[2]: 'b' In [3]: word[1] Out[3]: 'o' In [4]: word[2] Out[4]: 's' In [5]: word[3] Out[5]: 't' In [5]: word[4] Out[5]: 'o' In [5]: word[5] Out[5]: 'n' Notice 0, 1, 2, etc. are the indices (plural of index). Indices start at 0. Indices go from 0 to len(word)-1. We read word[0] as word sub 0. [] is known as the indexing operator. To think: How will indices be useful for solving our counting vowels problem? 8-4

A possible solution: which is correct? Difference between independent vs. chained conditionals. New operator: += Does our solution work for all words? if isvowel(word[1]): if isvowel(word[2]): if isvowel(word[3]): if isvowel(word[4]): if isvowel(word[5]): elif isvowel(word[1]): elif isvowel(word[2]): elif isvowel(word[3]): elif isvowel(word[4]): elif isvowel(word[5]): o Do you think the right solution from 8-5 will work for all words: 'wellesley', 'needham', 'lynn', etc.? o What happens if we use an index that's greater than or equal to the length of the word? In [1]: word = 'lynn' In [2]: word[4] IndexError: string index out of range How to generate the correct indices of the string? 8-5 8-6 Creating a list of indices with range In [1]: range(0, 10) Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [2]: range(3, 7) Out[2]: [3, 4, 5, 6] In [3]: range(3, 2) Out[3]: [] In [4]: range(3, 3) Out[4]: [] Built-in function range and new type list. When the range function is given two integer arguments, it returns a list of all integers starting at the first and up to, but not including, the second. A list is a new Python type. It stores a sequence of any values, delimited by square brackets, and separated by commas. More on slide 8-9. In [5]: range(3) # missing first argument defaults to 0 Out[5]: [0, 1, 2] 8-7 Properties of the range function An optional third argument to range controls the step size between elements (which defaults to 1). In [1]: range(1, 10, 2) Out[1]: [1, 3, 5, 7, 9] In [2]: range(3, 70, 10) Out[2]: [3, 13, 23, 33, 43, 53, 63] In [3]: range(9, 0, -1) Out[3]: [9, 8, 7, 6, 5, 4, 3, 2, 1] In [4]: range(9, 0, -2) Out[4]: [9, 7, 5, 3, 1] In [5]: range(63, 0, -10) Out[5]: [63, 53, 43, 33, 23, 13, 3] To notice: With the help of the third argument of range, we can create different lists of integers. Step can be positive or negative. When step is negative, start value has to be larger than end value. 8-8

Introducing a new value type: lists range() returns values of type list In [1]: type(range(0, 10)) Out[1]: list list() converts a string into lists of characters We ll return to lists again in a few lectures. Lists are an example of mutable objects in Python, different from the immutable strings. In [2]: list("wendy Wellesley") Out[2]: ['W', 'e', 'n', 'd', 'y', ' ', 'W', 'e', 'l', 'l', 'e', 's', 'l', 'e', 'y'] We can also specify a list directly as a comma separated list of values In [3]: phrase = ["a", "lovely", "autumn", "day"] In [4]: phrase Out[4]: ['a', 'lovely', 'autumn', 'day'] 8-9 Back to our vowel counting problem if isvowel(word[1]): if isvowel(word[2]): if isvowel(word[3]): if isvowel(word[4]): if isvowel(word[5]): The combination of range and len to generate indices for a sequence. In [1]: word ='boston' In [2]: range(len(word)) Out[2]: [0, 1, 2, 3, 4, 5] In [3]: word = 'wellesley' In [4]: range(len(word)) Out[4]: [0,1,2,3,4,5,6,7,8,9] In [5]: word = 'lynn' In [6]: range(len(word)) Out[6]: [0, 1, 2, 3] range solves our indexing problem, by generating the correct list of indices. 8-10 Loops to the rescue! if isvowel(word[1]): if isvowel(word[2]): if isvowel(word[3]): if isvowel(word[4]): if isvowel(word[5]): An example of a for loop with a conditional. for i in range(len(word)): if isvowel(word[i]): Try it out in the notebook to check that we get the same result. 8-11 Iterating Over Sequences with for Loops One of the most common ways to manipulate a sequence is to perform some action for each element in the sequence. This is called looping or iterating over the elements of a sequence, and in Python is accomplished with a for loop. for var in sequence: # Body of the loop statements using var New execution kind: iteration done through loops. Generic form of a for loop 8-12

for loop model example for i in range(len(word)): if isvowel(word[i]): [0,1,2,3,4,5] 6 word counter 0 i 0 Modeling how code in a loop is executed. 'boston' 1 2 1 2 3 4 5 To notice: - There are three variables in the code: i, word, counter. - The variables i and counter change values from one iteration to the next. - Because counter is within a conditional, it s only updated when the condition is true. 8-13 for loops without range Mode 1 o The range function provides a list of indices phrase = ["an", "autumn", "day"] for i in range(len(phrase)): print phrase[i] + '!' an! autumn! day! Unless there is a need for the index (see slide 8-15), we will prefer the Mode 2 of writing the loop to Mode 1. o We can also loop directly over any list. The code below produces the same output. Mode 2 phrase = ["an", "autumn", "day"] for word in phrase: print word + '!' Two different ways of looping over a sequence. 8-14 When is it better to use range instead of directly looping? Strings and lists are both sequences o Let's modify the previous example to print both the index and the item for each item in the list. for i in range(len(phrase)): print i, phrase[i], '!' 0 an! 1 autumn! 2 day! In [1]: In [2]: word[2] Out[2]: 's' In [3]: len(word) Out[3]: 6 In [4]: word + ' globe' Out[4]: 'boston globe' In [5]: 'o' in word Out[5]: True In [1]: digits = range(5,10) In [2]: digits[2] Out[2]: 7 In [3]: len(digits) Out[3]: 5 In [4]: digits + range(5) Out[4]: [5, 6, 7, 8, 9, 0, 1, 2, 3, 4] In [5]: 10 in digits Out[5]: False o Notice this would NOT be possible if we directly looped over the list. 8-15 A sequence is an abstract type, which serves as template for concrete types such as string or list. 8-16

Looping over a string for loop model example #2 for i in range(len(word)): if isvowel(word[i]): o Can we avoid range in this code as we did in 8-15? It turns out yes, in the same way. for char in word : if isvowel( char ): ['b','o','s','t','o','n'] for char in word: if isvowel(char): word counter 0 char 'boston' 1 2 'b' 'o' 's' 't' 'o' 'n' To notice: - There are again three variables in the code: word, counter, char. - The variables char and counter change values from one iteration to the next. - Notice that char takes as values the elements of the string sequence. Getting Started 8-17 8-18 Operations in Sequences The Slicing operator [:] Operation Result x in seq True if an item of seq is equal to x x not in seq False if an item of seq is equal to x seq1 + seq2 The concatenation of seq1 and seq2 seq*n, n*seq n copies of seq concatenated seq[i] i th item of seq, where origin is 0 seq[i:j] slice of seq from i to j seq[i:j:k] slice of seq from i to j with step k len(seq) length of seq min(seq) smallest item of seq max(seq) largest item of seq In [1]: In [2]: word[2] Out[2]: 's' In [3]: word[2:4] Out[3]: 'st' In [4]: word[:3] Out[4]: 'bos' In [5]: word[3:10] Out[5]: 'ton' In [6]: word[3:] Out[6]: 'ton' In [7]: word[0:6:2] Out[7]: 'bso' In [8]: word[::-1] Out[8]: 'notsob' In [1]: digits = range(5,10) In [2]: digits[2] Out[2]: 7 In [3]: digits[2:4] Out[3]: [7, 8] In [4]: digits[:3] Out[4]: [5, 6, 7] In [5]: digits[3:10] Out[5]: [8, 9] In [6]: digits[3:] Out[6]: [8, 9] In [7]: digits[0:5:2] Out[7]: [5, 7, 9] In [8]: digits[::-1] Out[8]: [9, 8, 7, 6, 5] 8-19 8-20

How do indices work? 0 b -6 word = "boston" digits = range(5, 10) 1 2 3 4 o -5 s -4 t -3 o -2 5 n -1 5 0 1 2 3 4 6 7 8 Indices in Python are both positive and negative. Everything outside these values will cause an IndexError. In [7]: word[::-1] Out[7]: 'notsob' -5-4 -3-2 9-1 This means: start at 0 until the end of sequence with step -1. And it works because of the negative indices. 8-21 Summary 1. Strings and lists are examples of sequences, ordered items that are stored together. Because they are ordered, we can use indices to access each of them individually and sequentially. 2. The indexing operator [] takes index values from 0 to len(sequence)-1. However, negative indices are possible too in Python, see 8-21. 3. If we can access each element of a sequence (string or list) one by one, then we can perform particular operations with them. 4. To access each element we need a loop, an execution mechanism that repeats a set of statements until a stopping condition is fulfilled. 5. When we loop over a sequence, the stopping mechanism is the arrival at the last element and not having where to go further. 6. We use the built-in function range to generate indices for sequences. 7. Python makes it easy for us to iterate over a sequence elements even without the use of indices. In fact we can write: for item in sequence: and that will access each item of the sequence. 8. In addition to accessing one element at a time with [], one can use [:] (slicing) to get a substring or a sublist. 8-22