Download presentation

Presentation is loading. Please wait.

Published bySophie Dowsett Modified about 1 year ago

1
1 Quick summary: print print "Kansas" Print string plus a newline print "Kansas", Print string plus a space print "Kansas", "City" Print strings with space in between print "Kansas " + "City" Print strings right after each other

2
2 Quick summary: string indexing Create variable pointing to a string: s = "Kansas City" Strings are indexed from 0: s[0] yields 'K' Strings can be indexed from the right with negative indices: s[-4] yields 'C' Create a copy of the substring of s from index 3 to but not including 6: s[3:6] yields 'sas' Create an extended slice: s[0:6:2] yields 'Kna' s[3::-1] yields 'snaK'

3
3 Quick summary: raw_input, int, float Print query string, wait for user to type something, always returns a string: raw_input("Enter your age: ") Convert the string "37" to the integer 37: int("37") Convert the string "37.54" to the float 37.54: float("37.54") int(raw_input("Enter your age: "))

4
4 Quick summary: string formatting Using the conversion specifiers and string formatting operator: s = "City" "Kansas %s"%s yields 'Kansas City' a = 50 "USA has %d states"%a yields "USA has 50 states" "Gotham %s has %d villains"%(s, a) yields 'Gotham City has 50 villains' "%.2f dollars"%7.9362 yields '7.94 dollars'

5
5 Quick summary: string methods Can be called through any string: "Kansas".find("sas") yields 3 "Kansas".isalnum() yields True "Kansas".islower() yields False "Kansas".count('a', 3) yields 1.. Or through the identifier str : str.find("Kansas ", "sas") yields 3

6
6 Example: cutting out parentheses Task: Receive text from user, cut out everything inside matching parentheses: Things in parentheses (such as this one) should be ignored. becomes Things in parentheses should be ignored.

7
7 Solution 1 S: Don't(or do)floss(I mean it)! Don't floss New S:

8
8 parenthesis_cutter.py

9
9 Running the program Sentence: Everything in parentheses (like this one) should be ignored. Everything in parentheses should be ignored. Sentence: What about (things inside (nested) parentheses) ? What about parentheses) ?

10
10 Solution 2 S: Don't(or do)floss (I mean it) ! Don't New S: parenthesis level: 2 f l o s s !

11
11 parenthesis_cutter2.py Note for char in s:

12
12 Running the program Sentence: What about (things inside (nested) parentheses) ? What about ?

13
13 Euclid’s Greatest Common Divisor algorithm 1.Get two integers a, b > 0 from the user 2.If a and b are the same, GCD = a, exit 3.Otherwise, repeat: – If a > b: Replace a with a modulo b (a is now smaller than b). If a is 0, GCD = b, exit. – Case b >= a analogous I.e., repeat as long as a > 0 and b > 0, then output whichever one is > 0.

14
14 gcd.py Note string formatting in table built-in function max

15
15 Running the program Input first integer > 0: 81 Input second integer > 0: 45 a b 81 45 36 45 36 9 The greatest common divisor of 81 and 45 is 9

16
16.. on to the exercises - See course homepage to get to the program examples from the lectures

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google