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

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Lists Introduction to Computing Science and Programming I.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Guide to Programming with Python
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
An Introduction to Textual Programming
Introduction to Python
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
Built-in Data Structures in Python An Introduction.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lists CS303E: Elements of Computers and Programming.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Files Tutor: You will need ….
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
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.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
31/01/ Selection If selection construct.
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
GCSE Computing: Programming GCSE Programming Remembering Python.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
CMSC201 Computer Science I for Majors Lecture 08 – Lists
String and Lists Dr. José M. Reyes Álamo.
Tuples and Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
Engineering Innovation Center
Example Programs.
Introduction to Strings
Python I/O.
Bryan Burlingame Halloween 2018
Creation, Traversal, Insertion and Removal
CS190/295 Programming in Python for Life Sciences: Lecture 6
Data Structures – 1D Lists
4. sequence data type Rocky K. C. Chang 16 September 2018
ARRAYS 1 GCSE COMPUTER SCIENCE.
IST256 : Applications Programming for Information Systems
String and Lists Dr. José M. Reyes Álamo.
Remembering lists of values lists
CMSC201 Computer Science I for Majors Lecture 09 – While Loops
Topics Sequences Introduction to Lists List Slicing
15-110: Principles of Computing
Topics Sequences Lists Copying Lists Processing Lists
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Hint idea 2 Split into shorter tasks like this.
Arrays & Loops.
Arrays & Loops.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Presentation transcript:

More Sequences

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…

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

List Functions  You can also sort lists:  Count occurrences: More Sequences18 of 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

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