Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 11 1

2 Lesson objectives 1. Understand how Python stores and uses strings 2. Perform indexing and slicing operations on Python sequences 3. Traverse strings with a loop 4. Compare strings and substrings 5/02/09 Python Mini-Course: Day 3 – Lesson 11 2

3 Strings in Python A string is a sequence of characters Sequences are indexed fruit = 'banana' letter = fruit[1] print letter 5/02/09 Python Mini-Course: Day 3 – Lesson 11 3

4 Notes on indexing Python uses zero-based indexing print fruit[0] Brackets vs. parenthesis Use brackets [x] for indexing Use parenthesis (x) for function calls 5/02/09 Python Mini-Course: Day 3 – Lesson 11 4

5 Notes on indexing You can use any expression as an index, provided it has an integer value fruit = 'banana' a, b = 1, 3 print fruit[b-a] 5/02/09 Python Mini-Course: Day 3 – Lesson 11 5

6 Notes on indexing You can use any expression as an index, provided it has an integer value fruit = 'banana' a, b = 1.0, 3.0 print fruit[a-b] 5/02/09 Python Mini-Course: Day 3 – Lesson 11 6

7 Notes on indexing Negative indices count backward from the end of the sequence fruit = 'banana' print fruit[-1] print fruit[-2] 5/02/09 Python Mini-Course: Day 3 – Lesson 11 7

8 Slicing a sequence You can specify a range of indices to slice a sequence fruit = 'banana' print fruit[1:3] 5/02/09 Python Mini-Course: Day 3 – Lesson 11 8

9 Slicing a sequence For slicing, imagine the indices as pointing between the characters 5/02/09 Python Mini-Course: Day 3 – Lesson 11 9

10 Slicing a sequence To slice from the beginning of the sequence, omit the first index print fruit[:3] To slice from the end of the sequence, omit the last index print fruit[3:] 5/02/09 Python Mini-Course: Day 3 – Lesson 11 10

11 Slicing a sequence What do these do? print fruit[3:3] print fruit[:] 5/02/09 Python Mini-Course: Day 3 – Lesson 11 11

12 Mutability In Python, some types of sequences can be changed These are mutable Others cannot be changed These are immutable 5/02/09 Python Mini-Course: Day 3 – Lesson 11 12

13 A mutable sequence: list x = [1,2,3] print x x[1] = 4 print x Here, x is a list. We'll learn more about lists next week. 5/02/09 Python Mini-Course: Day 3 – Lesson 11 13

14 Are strings mutable? x = 'perrot' print x x[1] = 'a' print x 5/02/09 Python Mini-Course: Day 3 – Lesson 11 14

15 "Changing" a string x = 'perrot' x = x[:1] + 'a' + x[2:] print x The + sign is a concatenation operator for sequences NB: The above code actually creates a new string and assigns it to x 5/02/09 Python Mini-Course: Day 3 – Lesson 11 15

16 The len function Syntax len(sequence) Returns the number of items in a sequence NB: because of zero-based indexing, the last valid index is one less than the length 5/02/09 Python Mini-Course: Day 3 – Lesson 11 16

17 The len function Example length = len(fruit) last = fruit[length] print last 5/02/09 Python Mini-Course: Day 3 – Lesson 11 17

18 The len function Example length = len(fruit) last = fruit[length-1] print last 5/02/09 Python Mini-Course: Day 3 – Lesson 11 18

19 Traversing a sequence Often, we want to do something to every item in a sequence We need to traverse the sequence This can be done with a loop 5/02/09 Python Mini-Course: Day 3 – Lesson 11 19

20 Using a while loop: traverse1.py def traverse(string): index = 0 while index < len(string): letter = string[index] print letter index += 1 traverse('Monty Python') 5/02/09 Python Mini-Course: Day 3 – Lesson 11 20

21 Using a for loop: traverse2.py def traverse(string): for letter in string: print letter traverse('Monty Python') 5/02/09 Python Mini-Course: Day 3 – Lesson 11 21

22 Searching strings: find.py def find(word, letter): index = 0 while index < len(word): if word[index] == letter: return index index = index + 1 return -1 5/02/09 Python Mini-Course: Day 3 – Lesson 11 22

23 Searching strings: count.py def count(word, letter): count = 0 for item in word: if item == letter: count += 1 return count count('banana', 'a') 5/02/09 Python Mini-Course: Day 3 – Lesson 11 23

24 String comparison To compare whole strings, use the standard comparison operators == = NB: strings are compared using numeric codes (e.g., ASCII), so case is very important 5/02/09 Python Mini-Course: Day 3 – Lesson 11 24

25 String comparison (try this on the command line of IDLE) x, y, z = 'abc', 'Abc', 'aBc' x == y x < y x > y x < z x > z 5/02/09 Python Mini-Course: Day 3 – Lesson 11 25

26 The in operator When used in a for statement, in iterates through a sequence However, in is also a Boolean operator that checks membership within a sequence 'a' in 'banana' 5/02/09 Python Mini-Course: Day 3 – Lesson 11 26

27 Comparing strings: string_comp.py def compare(string1, string2): if string1 in string2: print string1 + ' is a substring of ' + string2 if string2 in string1: print string2 + ' is a substring of ' + string1 if string1 == string2: print string1 + ' equals ' + string2 elif string1 > string2: print string1 + ' comes after ' + string2 else: print string1 + ' comes before ' + string2 5/02/09 Python Mini-Course: Day 3 – Lesson 11 27

28 Comparing strings compare('apple', 'banana') compare('banana', 'Pineapple') compare('banana', 'ana') compare('banana', 'banana') 5/02/09 Python Mini-Course: Day 3 – Lesson 11 28


Download ppt "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."

Similar presentations


Ads by Google