Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings, part 2 Victor Norman CS104 Calvin College.

Similar presentations


Presentation on theme: "Strings, part 2 Victor Norman CS104 Calvin College."— Presentation transcript:

1 Strings, part 2 Victor Norman CS104 Calvin College

2 Item-based vs Index-based Iteration item-based: for in : – is each item in the sequence. index-based: for in range(len( )): – code in the body has the index of what item to deal with, as someSeq[idx]).

3 Examples of each Item-based for cheese in cheeses: print(cheese) Index-based for idx in range(len(cheeses)): print(cheeses[idx])

4 When to use which? item-based: – simpler syntax, easier to read. – use when code does not need to know where the item is in the sequence. index-based: – harder to read. – accessing the item is more complicated (using indexing operator). – code can know where the item is in the sequence. – code can access other items around the item.

5 Example What if we want to print out the items from a list cheeses like this: 1.Cheddar 2.Gouda 3.Venezuelan Beaver Cheese Need to use index-based:

6 Example continued for idx in range(len(cheeses)): # idx starts at 0, but we want to # print out as if indices start at 1, # so add 1. print(str(idx + 1) + “.”, cheeses[idx])

7 Accumulator Pattern resStr = “” for ch in someStr: if somethingAbout(ch): resStr = resStr + ch Used item-based, because didn’t care about where we were in the string. someStr is a sequence, so syntax is legal. results accumulated in resStr

8 Whiteboard activity Given this string: message = “greetings from the planet zorgon” write code to print this out. (Use split().) greetings from the planet zorgon

9 Whiteboard Activity Write the following function that returns a string that is the same as s except that spaces are removed. def remove_spaces(s):

10 while Loop vs Index-Based for Loop (Suppose s is a string or a list.) for i in range(len(s)): code here uses s[i] i = 0 while i < len(s): use s[i] i = i + 1 # better: i += 1

11 in and not in very useful for searching a string to see if a sub-string is in the string or not. returns Boolean: so you know if the target is in the string, but don’t know where. if “wherefore” in hamletText: print(“art thou”)

12 Optional Parameters Terminology: – parameters may be optional in the call. – in function definition, optional params must appear on the end of the parameter list. indicated by being given a default value. Code in the function is exactly the same.

13 Examples def weird(a, b, c=3): return a + b + c print(weird(3, 7)) print(weird(3, 7, 44)) def weirder(a=3, b=4, c=5): return a + b + c print(weirder()) print(weirder(7)) print(weirder(7, 8)) print(weirder(7, 8, 9))

14 Examples def something(a, b, debug=False): res = a + b if debug: print(“something returning “ + res) return res x = something(44, -10) x = something(44, -10, True) # turn on debugging

15 Activity Write a function that removes certain letters from a given string. If no letters are given, it removes all vowels (not including y). You can assume everything is lowercase. The result is returned. def remove_chars(s, ): # remove from s

16 Activity continued Given a string s, write code to call your function on s to remove all vowels. Then, write a function call to remove all letters from a to f, inclusive. Print the results.

17 Assignment


Download ppt "Strings, part 2 Victor Norman CS104 Calvin College."

Similar presentations


Ads by Google