Repetition: the for loop

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Computing Using Python Imperative Programming  Python Programs  Interactive Input/Output  One-Way and Two-Way if Statements  for Loops.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Chapter 2 Writing Simple Programs
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Python Control of Flow.
Simple Python Loops Sec 9-7 Web Design.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Introduction to Computing Using Python Straight-line code  In chapter 2, code was “straight-line”; the Python statements in a file (or entered into the.
Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.
For loops in programming Assumes you have seen assignment statements and print statements.
COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
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.
Controlling Program Structures. Big Picture We are learning how to use structures to control the flow of our programs Last week we looked at If statements.
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.
Control structure: Repetition - Part 1
Chapter 2 Writing Simple Programs
String and Lists Dr. José M. Reyes Álamo.
Introduction to Python
Chapter 6: Loops.
Topic: Iterative Statements – Part 1 -> for loop
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Computer Programming Fundamentals
Chapter 3 Instructor: Zhuojun Duan
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
CS 115 Lecture 8 Structured Programming; for loops
Think What will be the output?
Functions.
Repetition: the for loop
While Loops in Python.
Ruth Anderson UW CSE 160 Spring 2018
Determinate Loops with the
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
4. sequence data type Rocky K. C. Chang 16 September 2018
Introduction to Programming
String and Lists Dr. José M. Reyes Álamo.
More Looping Structures
Programming Training Main Points: - Problems with repetitions.
Introduction to Programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Fundamentals of visual basic
Python programming exercise
Introduction to Repetition Structures
CHAPTER 6: Control Flow Tools (for and while loops)
Introduction to Python
Introduction to Computer Science
Python While Loops.
Introduction to Programming
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
More Looping Structures
CSE 231 Lab 2.
Introduction to Programming
Gavin Restifo March 1, 2019 Today: Repetition Part 2 - For Loops
While Loops in Python.
Topic: Iterative Statements – Part 2 -> for loop
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
LOOP Basics.
Control flow: Loops UW CSE 160.
Introduction to Python
Tuple.
Presentation transcript:

Repetition: the for loop Introduction to Computing Using Python Repetition: the for loop Execution control structures for loop – iterating over a sequence range()function Tracing code

Execution control structures Introduction to Computing Using Python Execution control structures Execution control (flow control) structures are statements that control which statements in a program are executed and in what order An if/elif/else conditional statement specifies whether to execute or skip blocks of code A for loop is an repetition (iteration) structure. For every item of a sequence, it assigns the item to a variable and and then executes a block of code We are going to visualize the sequence of execution using pythontutor.org

for loop syntax A for loop always has the following six elements: Introduction to Computing Using Python A for loop always has the following six elements: for varName in sequence: codeBlock It is introduced by the keyword for varName is a variable name of your choice (for example, you might use i for a sequence of integers) The keyword in The name of a sequence (for example, a string, list or tuple) A colon (:) (in Python, an indented block of code is always introduced by a colon An indented block of code

for loop execution for varName in sequence: codeBlock Introduction to Computing Using Python for varName in sequence: codeBlock When a for loop is executed: varName is assigned the value of the first element of sequence the indented codeBlock is executed Steps 1 and 2 are repeated for each element of sequence For example: word = "apple" for letter in word: print(letter) Instant exercise: type this example into pythontutor.com to visualize its execution. Then try the example using a variable name other than letter.

for loop – tuple example Introduction to Computing Using Python for varName in aTuple: codeBlock Example: days = ('Mon','Tue','Wed','Thu','Fri','Sat','Sun') for day in days: print(day) Instant exercise: type this example into pythontutor.com to visualize its execution

for loop – list example for varName in aList: codeBlock Introduction to Computing Using Python for varName in aList: codeBlock Strings and tuples are immutable, but lists can be changed. What happens when codeBlock changes aList? Example: oneTwoThree = [1, 2, 3] for i in oneTwoThree: oneTwoThree.append(i + 3) print('i = ',i) print('oneTwoThree = ', oneTwoThree) Instant exercise: type this example into pythontutor.com to visualize its execution. Why is it a bad practice to modify the sequence you are iterating over?

Built-in function range() Introduction to Computing Using Python A for loop is often used in combination with the built-in function range() function to execute a block of code a specified number of times. The range function generates a sequence of integers. range(n) generates the sequence 0, ..., n-1 range(i, n)generates the sequence i, i+1, i+2, …, n-1 range(i, n, c)generates the sequence i, i+c, i+2c, i+3c, …, n-1 Try each of these in pythontutor: for i in range(5): print(i) for i in range(3,5): print(i) for i in range(1,6,2): print(i)

Exercise Write for loops that will print the following sequences: Introduction to Computing Using Python Write for loops that will print the following sequences: 0, 1, 2, 3, 4, 5, 6, 7, 8 , 9, 10 1, 2, 3, 4, 5, 6, 7, 8, 9 0, 2, 4, 6, 8 1, 3, 5, 7, 9 20, 30, 40, 50, 60