October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
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.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Computer Science 111 Fundamentals of Programming I Sequences: Strings.
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.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 7 Strings, Lists.
October 17, 2005ICP: Chapter 5: Lists and Dictionaries 1 Introduction to Computer Programming Chapter 5: Lists and Dictionaries Michael Scherger Department.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
I Power Higher Computing Software Development High Level Language Constructs.
Lists CS303E: Elements of Computers and Programming.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
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.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Chapter 10 Loops: while and for CSC1310 Fall 2009.
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.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 9 For Loops.
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.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
String and Lists Dr. José M. Reyes Álamo.
Generating Random Numbers
Tuples and Lists.
Chapter 4 Strings & Tuples
Section 6: Sequences Chapter 4 and 5.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
Lists in Python.
4. sequence data type Rocky K. C. Chang 16 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Sun Mon Tue Wed Thu Fri Sat
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS1110 Today: collections.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Introduction to Computer Science
Sun Mon Tue Wed Thu Fri Sat
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Python Review
Sequences Sequences are collections of data values that are ordered by position A string is a sequence of characters A list is a sequence of any Python.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
By Himanshi dixit 11th ’B’
Sun Mon Tue Wed Thu Fri Sat
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Presentation transcript:

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael Scherger Department of Computer Science Kent State University

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 2 Contents Constructing for loops to move through a sequence Use the range() function Strings as sequences Tuples Sequence functions and operators Index and slice sequences

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 3 The Word Jumble Game Example: Word Jumble Game

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 4 Using For Loops A “for” loop is another control structure for looping –It uses “counter-controlled repetition” to determine when the loop is to terminate It will repeat the loop body for each element in a sequence Example: Loopy String

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 5 Understanding For Loops For loops iterate over every element in a sequence –A string is sequence of character elements Example: for letter in anyword: print letter

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 6 Counting With A For Loop Often programs need to count and use the current counter value for computation –For loops are best for counting Example: Counter The range() function returns a sequence of elements

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 7 Range Function Examples Counting Forward –range(5) –range(10, 20) Counting Backward –range(20, 0, -1) –range(20, 10, -1) Counting by Different Amounts –range(0, 50, 5) –range( 50, 0, -5) for ii in range(10, 20) print ii for ii in range( 20, 0, -1) print ii for ii in range(0, 50, 5) print ii

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 8 Sequence Operators and Functions With Strings Remember, a string is a type of sequence The function len() returns the length of a sequence (or string) The operator “in” test if an element is in a sequence Example: Message Analyzer –How could this be written with a loop?

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 9 Indexing Strings Looping through a sequence or string character by character is an example of sequential access The index operator [] allows for random access of a sequence –Syntax: anyword[ii] Example: Random Access

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 10 Indexing Strings An index is a position in the sequence –In Python position can be positive or negative –anyword[1] == anyword[-4] Run-time error if the index is out of range! “A”“B”“C”“D”“E” anyword

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 11 Indexing Strings Here is a code fragment to access a random sequence/string element anyphrase = “I’d like to have an argument” high = len(anyphrase) position = random.randrange(0, high) print anyphrase[position]

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 12 String Immutability Sequences are either mutable or immutable –Mutable means changeable –Immutable means unchangeable Strings are immutable sequences

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 13 String Immutability Consider the following example >>> name = “John Cleese” >>> print name John Cleese >>> name = “Michael Palin” >>> print name Michael Palin Did we change the string? –No, we just assigned a new string to name

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 14 String Immutability #Runtime Error in Python word = “game” word[0] = “n” runtime error!!!!!!! name John Cleese Michael Palin X

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 15 Building A New String If your program needs to build a new string it must do so using the string concatenation operator (either + or +=) Example: No Vowels Note the use of the CONSTANT in the program

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 16 Slicing Strings Indexing allows access to a single element “slice” from a sequence (string) A slice is any consecutive part of a sequence (string) Example: Pizza Slicer Do More Examples!!!!!!!!!!!!!

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 17 Slicing Strings Slicing is similar to indexing except you can get a sub-string from the string –use two index values –anystring[low:high] “A”“B”“C”“D”“E”

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 18 Creating Tuples A tuple is a sequence that can hold elements of any type –Tuples can hold integers, real numbers, strings, lists, dictionaries, and other tuples all at the same time –Tuples are immutable Example: Hero’s Inventory

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 19 Creating Tuples To create a empty tuple anytuple = () –an empty tuple is considered to be False if not anytuple print “The tuple is not empty” To create a tuple with elements days = (“Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun” )

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 20 More on Tuples To print a tuple print anytuple –Python will display each element of the tuple surrounded by parenthesis To loop through each element in a tuple for item in anytuple print item

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 21 Using Tuples Example: Hero’s Inventory 2.0 Using the in operator with tuples –This will test “membership” if an element is in a tuple if “Mon” in days print “Monday is a days of the week”

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 22 Using Tuples Remember tuples are immutable –days[1] = “MON” will give a runtime error Tuples can be constructed or modified by using concatenation (just like strings) –Use the + or += operator to add new elements to the end

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 23 Using Tuples Indexing tuples –Just like indexing strings –Example: print days[1] print days[4] Slicing tuples –Just like slicing strings print days[2:3] print days[4:] print days [-4:-1]

October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 24 Word Jumble Game - Again Example: Word Jumble Game