Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There.

Slides:



Advertisements
Similar presentations
LOOPS Loops are lines of code that can be run more than one time. Each time is called an iteration and in for loops there is also an index that is changing.
Advertisements

Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.
Programming Logic and Design Eighth Edition
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
Registers and Ranges. Register – Compared to a Calculator If there are only 9 digits available on display how long can the number displayed be? ANS: 9.
Week 5 - Friday.  What did we talk about last time?  Repetition  while loops.
Computer Science 1620 Loops.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Nested Loops. Nesting Control Structures One if statement inside another one An if statement inside a loop A loop inside an if statement Control structures.
Hello AP Computer Science!. What are some of the things that you have used computers for?
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
A revision guide for programming with python. 1.A program is a set of instructions that tells a computer what to do. 2.The role of a programmer is to.
Control Structures FOR Statement Looping.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Chapter 2 - Algorithms and Design
Chapter 5 Repetition or loop structure. What is repetition or loop? repeat the execution of one or a group (block; instruction enclosed in a pair of braces)
Permutations, Combinations & Counting Principle
By the end of this session you should be able to...
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
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.
Working on exercises (a few notes first). Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
1 The Practice of Computing Using PYTHON William PunchRichard Enbody Chapter 3 Algorithms & Program Development Copyright © 2011 Pearson Education, Inc.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Course A201: Introduction to Programming 09/30/2010.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Working on exercises (a few notes first)‏. Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Count Controlled Loops Look at the little children … Why is the sun’s face features orange …
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
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.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Visual Basic.NET BASICS Lesson 11 List Boxes, For Next Loops, and Label Settings.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Repetition In today’s lesson we will look at: why you would want to repeat things in a program different ways of repeating things creating loops in Just.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Getting Started With Python Brendan Routledge
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
Trace Tables In today’s lesson we will look at:
Whatcha doin'? Aims: To start using Python. To understand loops.
Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There.
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.
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Decision Structures, String Comparison, Nested Structures
Count Controlled Loops
Count Controlled Loops (Nested)
Java Programming Loops
Repetition Structures
Repetition In today’s lesson we will look at:
Python programming exercise
Incremental Programming
Iteration – While Loops
Presentation transcript:

Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There are two things to note: Computers start counting at 0. This is actually better than starting at 1, but isn’t always obvious straight away. You have to finish the loop one number HIGHER than the last number you actually want - so it has repeated 10 times, but goes from 0 to

Python - Iteration You can be a bit more specific with your loop by setting a start and an end number: for loopCounter in range(5,10): print(loopCounter)

Python - Iteration Remember that loops are closed (or finished) by removing the indentation: for loopCounter in range(1,4): print(loopCounter,"potato") print("4") for loopCounter in range(5,8): print(loopCounter,"potato") print("more")

Python - Iteration You can also specify how the counter will count: for loopCounter in range(2,9,2): print(loopCounter) print("Who do we appreciate?") Or the three times table: for loopCounter in range(3,37,3): print(loopCounter)

Python - Iteration FOR Loop Challenge 1. Look closely at the three times table example on the last page. Write a similar program to calculate the four times table. 2.Write a program that will calculate the 5 times table. 3.Try writing a program that will prompt for an integer (whole number) and print the correct times table (up to 12x). Test with all of the tables up to 12.

Python - Iteration Nested Loops Sometimes you need to put one loop inside another loop. The logic can get pretty complicated, so you have to be careful - but it does work! A window cleaner has to clean all the windows in a hotel. First, he (or she) cleans all the windows on the first floor: for room in range(1,5): print(“Room”,room,”cleaned”)

Python - Iteration Then he (or she) does the 2nd floor and the 3rd, and so on: floor = 1 for room in range(1,5): print(“Room”,floor,room,”cleaned”) floor = 2 for room in range(1,5): print(“Room”,floor,room,”cleaned”) floor = 3 for room in range(1,5): print(“Room”,floor,room,”cleaned”)

Python - Iteration A better way is to say for floor in range (1,7): for room in range(1,5): print(“Room”,floor,room,”cleaned”) Think about what this program will do. Decide what you think will happen. Then try it. for x in range(1,11): for y in range(1,11): print(x,"x",y,"=",x*y )

Python - Iteration Get the user to type in how many stars across they want to display and also how many stars down. Display the stars