Topic: Iterative Statements – Part 1 -> for loop

Slides:



Advertisements
Similar presentations
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Advertisements

Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Chapter 2 Writing Simple Programs
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Python Control of Flow.
Fundamentals of Python: From First Programs Through Data Structures
The University of Texas – Pan American
Fundamentals of Python: First Programs
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
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.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Chapter 2 Writing Simple Programs
String and Lists Dr. José M. Reyes Álamo.
Control Flow (Python) Dr. José M. Reyes Álamo.
Topic: Python Lists – Part 1
CMPT 120 Topic: Python’s building blocks -> More Statements
Topic: Introduction to Computing Science and Programming + Algorithm
Topic: Python’s building blocks -> Variables, Values, and Types
Topic: Functions – Part 1
Introduction to Programming
Topic: Python’s building blocks -> Statements
Topic: Conditional Statements – Part 1
Topic: Python’s building blocks -> Variables, Values, and Types
Topic: Conditional Statements – Part 2
Selection and Python Syntax
Python: Control Structures
CMPT 120 Topic: Searching – Part 1
Lesson 05: Iterations Class Chat: Attendance: Participation
Computer Programming Fundamentals
CMPT 120 Topic: Python strings.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Introduction to Programming
Topic: Functions – Part 2
Repetition: the for loop
While Loops in Python.
Topics Introduction to Repetition Structures
Outline Altering flow of control Boolean expressions
CS190/295 Programming in Python for Life Sciences: Lecture 6
Introduction to Programming
String and Lists Dr. José M. Reyes Álamo.
Topics Introduction to Repetition Structures
3. Decision Structures Rocky K. C. Chang 19 September 2018
CMSC201 Computer Science I for Majors Lecture 08 – For Loops
For loops Taken from notes by Dr. Neil Moore
Python Basics with Jupyter Notebook
Introduction to Computer Science
Repetition: the for loop
Another Example Problem
Introduction to Programming
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Topic: Iterative Statements – Part 2 -> for loop
Class code for pythonroom.com cchsp2cs
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Lecture 17 – Practice Exercises 3
Lecture 20 – Practice Exercises 4
Fundamentals of Python: First Programs Second Edition
Lecture 17 – Practice Exercise 3 SOLUTIONS
Introduction to Python
Presentation transcript:

Topic: Iterative Statements – Part 1 -> for loop CMPT 120 Topic: Iterative Statements – Part 1 -> for loop

Last Lectures Conditional statements Condition: Boolean expression if if else Nested if else Chained if (elif) Condition: Boolean expression Compound conditions Short-circuit evaluation of compound conditions

Learning outcomes At the end of this course, a student is expected to: Create (design), analyze, and explain the behaviour of simple algorithms: … Design algorithms that avoid the use of break statements to interrupt loops or to avoid the use of multiple return statements to exit a function Create (design) small to medium size programs using Python: Refactor repeated statements into for and while loops

Today’s Menu Iterative Statements for loop range( ) built-in function in operator

Iterative statements – In real world! Imagine that we are having a birthday party 8 guests (including oneself) We are serving cake 1 cake -> cut into 8 slices Everybody gets a slice While serving a piece of cake to each of our guests, are we repeating some of our actions? Which ones?

What if … … we wanted to do something many times in our program (i.e., execute a set of statements more than once)? How would we go about doing this? Solution: We could copy and paste a set of statements as many times as required We could make use of yet another Python building block: iterative statement

Python statements Categories: Assignment statement Input statement Conversion function Output statement Operational statements Mathematical/arithmetic operators String manipulation operators Conditional statements Iterative statements Some of them are built-in function or method Today

Iterative statements – In computer world! Definition: Allows us to repeat (iterate) statements in our program If we know how many times we want to repeat the statements, then we use the iterative statement called the for loop If we know there is a condition (or more than 1 condition) that will occur in our program and its occurrence will dictate when we stop repeating a set of statements, then we use the iterative statement called the while loop

Back to our Guessing Game What if we wanted to allow the user (i.e., player) 3 guesses?

Syntax of a for loop Can be a string <statement outside (before) the loop> for <iterating variable> in <sequence> : <first statement to be repeated> <second statement to be repeated> ... <last statement to be repeated> <statement outside (after) the loop> Can be a list Can be produced using range(…)

Syntax of a for loop Important – About Indentation <statement outside (before) the loop> for <iterating variable> in <sequence> : <first statement to be repeated> <second statement to be repeated> ... <last statement to be repeated> <statement outside (after) the loop> Important – About Indentation Statements inside the loop (i.e., statements executed at each iteration of the loop) are the statements indented with respect to the for keyword Statements outside the loop (before and after the loop) are the statements that are not indented with respect to the for keyword – these statements are considered to be at the same level of indentation as the for loop

Let’s have a look at a for loop # Magic_for_Loop.py # <sequence> here is a list number = 0 aList = [4, 3, 2, 1] for magic in aList: number += magic print('number = ', number)

… and let’s have a look at its in operator aList = [4, 3, 2, 1] for magic in aList: ... Here is how it works: For each element in the sequence It is assigned to iterating variable magic, and The statement(s) in the body of the for loop is/are executed Here, the sequence is the list aList Here, an element is an integer

Let’s hand trace our example … so we can figure out how it works Iteration # number aList magic

Another <sequence>: a range # Range_Example.py # <sequence> here is a range number = 0 stop = 4 for magic in range(stop): number += 2 print('number = ', number)

Built-in function range( ) Very useful in for loop Syntax: range([start,] stop [,step])) Produces a list of integers How does it work? If theLength = 5 Then range(theLength) produces [0, 1, 2, 3, 4]

Hand tracing our example Iteration # number range(stop) magic

Let’s play around with range() What will we see printed on the Python Interpreter shell window if : range(10) range(1, 11) range(0, 30, 5) range(0, 10, -3) range(0, -10, -1) range(0) To see it on the Python Interpreter shell window : list(range(...))

Yet another <sequence>: a string # Count_Letter_2.py # <sequence> here is a string # Set variables count = 0 fruit = "banana" # Loop - Spell a word and count the number of letter in the word # Running count for letter in fruit: count += 1 print("Letter %d is '%s'" %(count, letter)) # Output result print("The string '%s' has %d letters." %(fruit, count))

Hand tracing our example Iteration # count fruit letter

Let’s Practise Problem Statement: Create a Python program that spells the word the user enters and prints its length Sample Run: Please, enter a word: apple The word you entered is apple letter 1 is: a letter 2 is: p letter 3 is: p letter 4 is: l letter 5 is: e This word has 5 letters.

Solution – Word_1.py aWord = input("Please, enter a word: ") print("The word you entered is '%s'" %aWord) theLength = len(aWord) # Way 1: for index in range(theLength): print("Letter %i is: '%s'" %(index+1,aWord[index])) print("This word '%s' has %i letters." %(aWord, theLength))

Let’s hand trace Word_1.py Iteration # aWord range(theLength) index

Solution – Word_2.py aWord = input("Please, enter a word: ") print("The word you entered is '%s'" %aWord) theLength = len(aWord) # Way 2: index = 1 for letter in aWord: print("Letter %i is: '%s'" %(index,letter)) index = index + 1 print("This word '%s' has %i letters." %(aWord, theLength))

Let’s hand trace Word_2.py Iteration # aWord theLength index letter

Back to our Guessing Game What if we wanted to allow the user (i.e., player) 3 guesses? Solution: GuessingGame_5.py  Step 5 – Testing How would we test this program?

Summary Iterative Statements – Part 1 for loop range( ) built-in function in operator

Next Lecture Iterative Statements – Part 2 while loop