While Loops in Python.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CS0004: Introduction to Programming Repetition – Do Loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Fundamentals of Python: From First Programs Through Data Structures
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Fundamentals of Python: First Programs
Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Loops ! We've seen variables change in-place before: [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range ?
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
EECS 110: Lec 10: Definite Loops and User Input Aleksandar Kuzmanovic Northwestern University
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
More about Iteration Victor Norman CS104. Reading Quiz.
FOP: While Loops.
Lesson #5 Repetition and Loops.
EECS 110: Lec 10: Definite Loops and User Input
Repetition Structures
REPETITION CONTROL STRUCTURE
Lecture 6 Repetition Richard Gesick.
Lesson #5 Repetition and Loops.
Lesson 05: Iterations Class Chat: Attendance: Participation
CS 115 Lecture 8 Structured Programming; for loops
Repetition-Counter control Loop
While Loops in Python.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Python - Loops and Iteration
Lecture 4B More Repetition Richard Gesick
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Logical Operators and While Loops
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Iteration with While You can say that again.
EECS 110: Lec 10: Definite Loops and User Input
Introduction to Python
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Lesson #5 Repetition and Loops.
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
Coding Concepts (Basics)
IST256 : Applications Programming for Information Systems
Repetition Structures
Module 4 Loops.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
3.1 Iteration Loops For … To … Next 18/01/2019.
Loop Strategies Repetition Playbook.
Flowcharts and Pseudo Code
CHAPTER 6: Control Flow Tools (for and while loops)
Logical Operators and While Loops
For loops Taken from notes by Dr. Neil Moore
Lesson #5 Repetition and Loops.
Repetition Statements (Loops) - 2
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
REPETITION Why Repetition?
Presentation transcript:

While Loops in Python

A bit unfinished business with for loops …

Two kinds of for loops sum = 0 sum = 0 for x in aList: for i in : Element-based Loops Index-based Loops sum = 0 for x in aList: sum += x sum = 0 for i in : sum += ask for two parts of the index-based loop! why is this the egocentric loop? Because we usually use the variable 'i'. Ha ha. Bad humor, indeed! i 1 2 aList = [ 42, -5, 10 ] aList = [ 42, -5, 10 ] x

Two kinds of for loops sum = 0 for x in aList: sum += x sum = 0 Element-based Loops Index-based Loops sum = 0 for x in aList: sum += x sum = 0 for i in range(len(aList)): sum += aList[i] i 1 2 aList = [ 42, -5, 10 ] aList = [ 42, -5, 10 ] x aList[i]

Run loopindex.py

Python ‘for’ loop works well for a collection of known items Python ‘for’ loop works well for a collection of known items. That is, we know the count, or we know the list of items. But what if we don’t know the count, or the list, rather we only know the condition to repeat (or not to repeat) the steps? For example, we want to repeat adding to a sum as long as the user input is a positive number.

Here is a problem to solve: The user types in a sequence of positive integers. The program is to add up the integers. The user signals the end of the input by entering a non-positive value which should not be part of the sum. sum = 0 v = int( input( ‘Enter a positive integer ( <= 0 to stop) : ‘)) while ( v > 0): sum = sum + v print( ‘Summation is : ‘, sum )

while loops: indefinite, conditional iteration while <condition>: <body> while loops: indefinite, conditional iteration x = 10 if x != 0: print(x) x = x – 1 What are printed on the screen? Python loops: definite (for loops) and indefinite (while loops) While loops have the structure: while <condition>: <body> the <condition> must be a Boolean expression (something that evaluates to true or false) and the <body> can have multiple statements (including if statements, more while loops, etc.) 10 9

while loops: indefinite, conditional iteration while <condition>: <body> x = 10 while x != 0: print(x) x = x – 1 What is the last value printed on the screen? a) 0 b) 1 c) 9 d) 10 Python loops: definite (for loops) and indefinite (while loops) While loops have the structure: while <condition>: <body> the <condition> must be a Boolean expression (something that evaluates to true or false) and the <body> can have multiple statements (including if statements, more while loops, etc.)

Patterns of while loop while <condition>: <body> Prime the condition. while <condition>: <body> <update condition> Check the condition. Update the condition. x = 10 while x != 0: print(x) x = x – 1 From logical point of view, a while loop must have three components. Prime the condition; [before] Check the condition; [in] Update the condition. [in] Missing any of the three likely makes the loop incorrect.

Extreme Looping What does this code do? print('It keeps on') while True: print('going and') print('Phew! I\'m done!')

Extreme Looping Anatomy of a while loop: print('It keeps on') while True: print('going and') print('Phew! I\'m done!') the loop keeps on running as long as this test is True “while” loop use 1==1 use cs in physics This won't print until the while loop finishes - In this case, it never prints! alternative tests?

Will the same thing appear every time this is run? Making our escape! Will the same thing appear every time this is run? import random escape = 0 while escape != 42: print('Help! Let me out!') escape = random.choice([41,42,43]) print('At last!')

Count the iterations… import random escape = 0 while escape != 42: What modifications do we need to make to count the number of iterations and report it? import random escape = 0 while escape != 42: print('Help! Let me out!') escape = random.choice([41,42,43]) print('At last!') count = 0 before the loop count += 1 inside the loop print out the count after the loop Make it harder by creating more choices? Allow an escape every nth iteration? Etc. how could we make it easier/harder to escape?

Count the iterations… import random escape = 0 count = 0 What modifications do we need to make to count the number of iterations and report it? import random escape = 0 count = 0 while escape != 42: count += 1 print('Help! Let me out!') escape = random.choice([41,42,43]) print('At last!') count = 0 before the loop count += 1 inside the loop print out the count after the loop Make it harder by creating more choices? Allow an escape every nth iteration? Etc. how could we make it easier/harder to escape? Try out escape.py

while we're here… x = 39 while x < 44: if x % 2 == 0: print(x) What numbers will this loop print out? x = 39 while x < 44: if x % 2 == 0: print(x) x += 1 Be sure to point out the shortcut assignment You can simulate any for loop with a while loop for i in range(n): <body of loop> is the same as i=0 while i < n: i = i+1 #what happens if this line is omitted? You're whiling away my time...

Exercises Write a for loop to print the first n values of multiple of 3 Write a while loop that takes a collection of positive integers and computes the sum. The loop stops when a zero or negative number is entered. Compute and print the sum and average of this collection of numbers.