Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.

Similar presentations


Presentation on theme: "More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices."— Presentation transcript:

1 More Sequences

2 Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices to display to extract a sequence: Iterate using the for loop: More Sequences2 of 19 Try these…

3 Other Types of Sequences  What about holding a sequence of numbers, strings, Booleans etc. in one variable?  You can do this with lists  Use square brackets with commas in between  You can mix up data types too More Sequences3 of 19

4 Lists  Lists are powerful; you can use all the string sequence operations and more  You can use len( ) with a list  You can concatenate lists… More Sequences4 of 19

5 Exercise 1: Task 1  Create a list to hold what you have in your school blazer pockets today  Print out the number of items in the list using the len() function  Create second list to hold three more items  Concatenate the lists together We will build this program up over the session, make sure you write clear code. Add a comment before each task e.g. # Task 1 More Sequences5 of 19

6 Lists  Just as you can print a string, you can print a list:  You can look at a single item by its index: More Sequences6 of 19

7 Exercise 1: Task 2  Print the entire list  Recalculate the length of your list and display it  Ask the user to enter an index value *  Print the item in your list at that index * Challenge task: can you tell the user the valid range of values for your current list? E.g. “Enter an index value between 0 and 5” More Sequences7 of 19

8 Lists  You can iterate over a list More Sequences8 of 19

9 Exercise 1: Task 3  Add a for loop to print out your list more nicely  Print it like this for example: I have the following items in my blazer: Pen Timetable Bus pass More Sequences9 of 19

10 Lists  You can use slicing:  You can look for occurrences of an item using in: More Sequences10 of 19

11 Exercise 1: Task 4  Print out a slice from the list  Ask the user to enter an item name and tell them whether it is in your list or not More Sequences11 of 19

12 Lists  Strings are immutable; you cannot change them  Lists are mutable 12 of 14 More Sequences12 of 19 Try this in the shell…

13 List Functions  There are some useful list functions:  You can append a new list item  And insert at a fixed position… More Sequences13 of 19

14 Exercise 1: Task 5  Ask the user to enter a new item and append it to your list  Print the list  Ask the user to enter another item and insert it in a fixed position in your list (make sure you choose a valid index)  Print the list Check the list is as you expect More Sequences14 of 19

15 List Functions  You can delete a list element using the remove function  If you try to remove something that doesn’t exist in the list, you get an error: More Sequences15 of 19

16 List Functions  You can safely remove an item like this: More Sequences16 of 19

17 Exercise 1: Task 6  Ask the user to enter an item name and safely remove it from the list  Print the list Check the list is as you expect More Sequences17 of 19

18 List Functions  You can also sort lists:  Count occurrences: More Sequences18 of 19

19 Exercise 1: Task 7  Sort your list and print it again  Count the number of an item in your list and print out the value Test your program thoroughly More Sequences19 of 19 Solution: Exercise 1.py

20 Exercise 2: Memory Game  Create a secret list of ten animals  Ask the user for ten guesses of what is in the list. Add a point for each correct guess and subtract a point for each incorrect guess. Display their score at the end. Solution: memory1.py Part 2: Display a menu with three options: Enter list: Allow the user to enter the ten secret items. Store them in a list. After the user has finished, print enough blank lines so that the input words cannot be seen. Test: as above Exit: Finish the program Extension : give the user a bonus point for entering any word in the correct order Solution: memory2.py Evaluation: Can you see a flaw in this game? How could you fix it? 20 of 19


Download ppt "More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices."

Similar presentations


Ads by Google