240 likes | 260 Views
This chapter covers the use of for loops to iterate through sequences, working with strings as sequences, understanding tuples, and using sequence functions and operators. It also includes examples of word jumble game, counting with for loops, and indexing and slicing strings. The chapter concludes with an introduction to creating and working with tuples.
E N D
Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael Scherger Department of Computer Science Kent State University ICP: Chapter 4: For Loops, Strings, and Tuples
Contents • Constructing for loops to move through a sequence • Use the range() function • Strings as sequences • Tuples • Sequence functions and operators • Index and slice sequences ICP: Chapter 4: For Loops, Strings, and Tuples
The Word Jumble Game • Example: Word Jumble Game ICP: Chapter 4: For Loops, Strings, and Tuples
Using For Loops • A “for” loop is another control structure for looping • It uses “counter-controlled repetition” to determine when the loop is to terminate • It will repeat the loop body for each element in a sequence • Example: Loopy String ICP: Chapter 4: For Loops, Strings, and Tuples
Understanding For Loops • For loops iterate over every element in a sequence • A string is sequence of character elements • Example: for letter in anyword: print letter ICP: Chapter 4: For Loops, Strings, and Tuples
Counting With A For Loop • Often programs need to count and use the current counter value for computation • For loops are best for counting • Example: Counter • The range() function returns a sequence of elements ICP: Chapter 4: For Loops, Strings, and Tuples
Counting Forward range(5) range(10, 20) Counting Backward range(20, 0, -1) range(20, 10, -1) Counting by Different Amounts range(0, 50, 5) range( 50, 0, -5) for ii in range(10, 20) print ii for ii in range( 20, 0, -1) print ii for ii in range(0, 50, 5) print ii Range Function Examples ICP: Chapter 4: For Loops, Strings, and Tuples
Sequence Operators and Functions With Strings • Remember, a string is a type of sequence • The function len() returns the length of a sequence (or string) • The operator “in” test if an element is in a sequence • Example: Message Analyzer • How could this be written with a loop? ICP: Chapter 4: For Loops, Strings, and Tuples
Indexing Strings • Looping through a sequence or string character by character is an example of sequential access • The index operator [] allows for random access of a sequence • Syntax: anyword[ii] • Example: Random Access ICP: Chapter 4: For Loops, Strings, and Tuples
Indexing Strings • An index is a position in the sequence • In Python position can be positive or negative • anyword[1] == anyword[-4] • Run-time error if the index is out of range! 0 1 2 3 4 anyword “A” “B” “C” “D” “E” -1 -5 -4 -3 -2 ICP: Chapter 4: For Loops, Strings, and Tuples
Indexing Strings • Here is a code fragment to access a random sequence/string element anyphrase = “I’d like to have an argument” high = len(anyphrase) position = random.randrange(0, high) print anyphrase[position] ICP: Chapter 4: For Loops, Strings, and Tuples
String Immutability • Sequences are either mutable or immutable • Mutable means changeable • Immutable means unchangeable • Strings are immutable sequences ICP: Chapter 4: For Loops, Strings, and Tuples
String Immutability • Consider the following example >>> name = “John Cleese” >>> print name John Cleese >>> name = “Michael Palin” >>> print name Michael Palin • Did we change the string? • No, we just assigned a new string to name ICP: Chapter 4: For Loops, Strings, and Tuples
#Runtime Error in Python word = “game” word[0] = “n” runtime error!!!!!!! String Immutability X name John Cleese Michael Palin ICP: Chapter 4: For Loops, Strings, and Tuples
Building A New String • If your program needs to build a new string it must do so using the string concatenation operator (either + or +=) • Example: No Vowels • Note the use of the CONSTANT in the program ICP: Chapter 4: For Loops, Strings, and Tuples
Slicing Strings • Indexing allows access to a single element “slice” from a sequence (string) • A slice is any consecutive part of a sequence (string) • Example: Pizza Slicer • Do More Examples!!!!!!!!!!!!! ICP: Chapter 4: For Loops, Strings, and Tuples
Slicing Strings • Slicing is similar to indexing except you can get a sub-string from the string • use two index values • anystring[low:high] 0 5 1 2 3 4 “A” “B” “C” “D” “E” -1 -5 -4 -3 -2 ICP: Chapter 4: For Loops, Strings, and Tuples
Creating Tuples • A tuple is a sequence that can hold elements of any type • Tuples can hold integers, real numbers, strings, lists, dictionaries, and other tuples all at the same time • Tuples are immutable • Example: Hero’s Inventory ICP: Chapter 4: For Loops, Strings, and Tuples
Creating Tuples • To create a empty tuple anytuple = () • an empty tuple is considered to be False if not anytuple print “The tuple is not empty” • To create a tuple with elements days = (“Mon” , “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun” ) ICP: Chapter 4: For Loops, Strings, and Tuples
More on Tuples • To print a tuple print anytuple • Python will display each element of the tuple surrounded by parenthesis • To loop through each element in a tuple for item in anytuple print item ICP: Chapter 4: For Loops, Strings, and Tuples
Using Tuples • Example: Hero’s Inventory 2.0 • Using the in operator with tuples • This will test “membership” if an element is in a tuple if “Mon” in days print “Monday is a days of the week” ICP: Chapter 4: For Loops, Strings, and Tuples
Using Tuples • Remember tuples are immutable • days[1] = “MON” will give a runtime error • Tuples can be constructed or modified by using concatenation (just like strings) • Use the + or += operator to add new elements to the end ICP: Chapter 4: For Loops, Strings, and Tuples
Using Tuples • Indexing tuples • Just like indexing strings • Example: • print days[1] • print days[4] • Slicing tuples • Just like slicing strings • print days[2:3] • print days[4:] • print days [-4:-1] ICP: Chapter 4: For Loops, Strings, and Tuples
Word Jumble Game - Again • Example: Word Jumble Game ICP: Chapter 4: For Loops, Strings, and Tuples