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

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

String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
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.
ITC 240: Web Application Programming
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.
Group practice in problem design and problem solving
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
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
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.
>>> # Fibonacci series:... # the sum of two elements defines the next... a, b = 0, 1 >>> while b < 10:... print b... a, b = b, a+b WHILE.
If statements while loop for loop
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
For loops in programming Assumes you have seen assignment statements and print statements.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
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.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
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,
 See pages 65 – 67 in your book  While loops are all around us ◦ Shampoo  Rinse, Lather, Repeat  While (Condition Is True) : ◦ Execute a block of.
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
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.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
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.
Introduction to Python
Topic: Iterative Statements – Part 1 -> for loop
Chapter 3 Instructor: Zhuojun Duan
Introduction to Programming
Think What will be the output?
Repetition: the for loop
Ruth Anderson UW CSE 160 Spring 2018
Determinate Loops with the
4. sequence data type Rocky K. C. Chang 16 September 2018
Introduction to Programming
Programming Training Main Points: - Problems with repetitions.
Introduction to Programming
CHAPTER 6: Control Flow Tools (for and while loops)
Introduction to Python
Introduction to Computer Science
Repetition: the for loop
Introduction to Programming
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
CSE 231 Lab 2.
Introduction to Programming
Gavin Restifo March 1, 2019 Today: Repetition Part 2 - For Loops
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:

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

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

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

Introduction to Computing Using Python for loop execution for varName in sequence: codeBlock When a for loop is executed: 1.varName is assigned the value of the first element of sequence 2.the indented codeBlock is executed 3.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.

Introduction to Computing Using Python for loop – tuple example 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

Introduction to Computing Using Python for loop – list example 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?

Introduction to Computing Using Python Built-in function range() 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)

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