Download presentation
Presentation is loading. Please wait.
Published byRonald Hawkins Modified over 8 years ago
1
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional, where blocks of instructions are executed only if some condition is met 3. repeating, where blocks of instructions are repeated a number of times
2
Computer Program Flow Block of instructions
3
The for Loop … syntax Loops are used to repeat an operation or to process something more than once. A Python for loop: iterates through each value in a sequence. a count controlled loop that always executes a specified number of times The syntax is: for variable in sequence: # for clause statement # code executed every statement(s) # time the loop iterates
4
The for Loop … behaviour for variable in sequence: statement statement(s) variable takes on the 1st value in the sequence; statements in the loop body are executed variable takes on the 2 nd value in the sequence; statements in the loop body are executed variable takes on the 3 rd value … … repeat until all values in the sequence handled a sequence holds multiple items of data, stored one after the other
5
The for Loop … sequence (1) using the range function => range([start,] limit [[, increment]]) read as: from start (defaults to 0) up to but not including limit in steps of increment (defaults to 1) for var in range(5): for var in range(0,5): for var in range(0,5,1):
6
The for Loop … example # Assignment 1 Question 2 – do 3 tests for count in range (3): for clause course = input("Enter favourite course: ") mark = input("Enter grade you hope to get: ") print() print("You hope to get", mark, "in your favourite course", course, ".")
7
The for Loop … sequence Defining the sequence *: (2) you can explicitly list the numbers in the sequence, e.g. for number in [0,1,2,3,4,5]: print(number) where: [0,1,2,3,4,5] is a Python list * Python has various sequence type objects that we have not introduced yet, e.g. strings, tuples, and more about lists.
8
The for Loop … sequence (3) the sequence items are handled in the order they appear e.g. for number in [123, 5, -27, 15.5]: print(number) for course in ["MA100", "MA110", "MA103"]: print(course) for name in ["Peter", "Paul", "Mary"]: print(name)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.