Simple Python Loops Sec 9-7 Web Design.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Roles of Variables with Examples in Scratch
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
Do I need a robot today? You only need a robot if your team has not shown me your octagon and/or the octagon in a function.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Final Project Web Design. Final Project Your robot will be placed in a room with the red cone. Your robot will need to find the cone in the room and run.
CS0004: Introduction to Programming Repetition – Do Loops.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Intro to Robots Lab 3. Intro to Robots Basic Robot Brain Programs consist of import lines, function definitions and and a starting point. Line 1: from.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
CSE 113 Week 5 February , Announcements  Module 2 due 2/15  Exam 3 is on 2/15  Module 3 due 2/22  Exam 4 is on 2/25  Module 4 due 2/29.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Python Control of Flow.
Fundamentals of Python: From First Programs Through Data Structures
Taking Pictures Sec 9-9 Web Design. Objectives The student will: Know how command the scribbler robot to take a picture. Know how to display the picture.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
CATCH SCRATCH! Programming from Scratch. Remember Scratch?
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CPS120 Introduction to Computer Science Iteration (Looping)
How to link the robot and the computer (Bluetooth) How to turn on and off How to connect the adaptor Fluke card connection Sec Getting Started How.
Simple Functions and Names Sec 9-4 Web Design. Objectives The student will: Know how to create a simple function in Python. Know how to call a function.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
For loops in programming Assumes you have seen assignment statements and print statements.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Course A201: Introduction to Programming 09/16/2010.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
CPS120 Introduction to Computer Science Iteration (Looping)
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
GCSE Computing: Programming GCSE Programming Remembering Python.
Repetition: Definite Loops Sec 53 Web Design. Objectives The Student will: Understand loops and why they are used Understand definitive loops Know how.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Creating and Using Modules Sec 9-6 Web Design. Objectives The student will: Know how to create and save a module in Python Know how to include your modules.
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Sensor Information: while loops and Boolean Logic.
Control Flow (Python) Dr. José M. Reyes Álamo.
What you asked me to teach…
Lesson 4 - Challenges.
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.
Chapter 4 MATLAB Programming
Think What will be the output?
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Repetition: the for loop
Iterations Programming Condition Controlled Loops (WHILE Loop)
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Python Primer 2: Functions and Control Flow
CMSC 120: Visualizing Information 3/25/08
Doing things more than once
Loops CIS 40 – Introduction to Programming in Python
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Fundamentals of visual basic
For Loops (Iteration 1) Programming Guides.
FLUENCY WITH INFORMATION TECNOLOGY
Repetition: the for loop
Python While Loops.
Topic: Iterative Statements – Part 2 -> for loop
How to allow the program to know when to stop a loop.
Presentation transcript:

Simple Python Loops Sec 9-7 Web Design

Objectives The student will: Know how to program simple “for” loop in Python Understand the structure of a “while” loop in Python Know how loop for a certain period of time Know how to loop forever

For loop The simplest form of repetition is a for Loop. Remember from Alice LOOP repeated a fixed number of times. In Python that is called a for loop The basic syntax of a for-loop in Python is: for <variable> in <sequence>: <do something> ...

For Loop The loop specification begins with the keyword for which is followed by a <variable> and then the keyword in and a <sequence> followed by a colon(:). This line sets up the number of times the repetition will be repeated. What follows is a set of statements, indented (again, indentation is important), that are called a block that forms the body of the loop (stuff that is repeated).

For Loop When a for loop is executed, the <variable> (which is called a loop index variable) is assigned successive values in the <sequence> and for each of those values, the statements in the body of the loop are executed. A <sequence> in Python is a list of values To execute a loop a certain number of times you can use the range() function. To see what this function does you can start IDLE and type: >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] The range function simply returns a list of numbers from 0 to one less than the number passed to the function

For Loop Example If I have a dance function I want to repeat it 10 times for i in range(10): dance() i is a variable. Each time the loop is executed i has a different value (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Looping Forever In writing robot programs there will be times when you just want the robot to keep doing its behaviors forever. While technically by forever we do mean eternity in reality what is likely to happen is either it runs out of batteries, you decide to stop it (by hitting CTRL-C), or the period ends and you turn it off. The Python command to specify this uses a different loop statement, called a while-loop that can be written as: while True: <do something> ... We will cover more of while-loops later.

Repetition Using Time In addition to repeating by counting or looping forever, you can also specify repetition using time. while timeRemaining(<how_many_seconds>): <do something> ... If you wanted the computer to say “Doh!” for 5 seconds, you can write: while timeRemaining(5): speak("Doh!", 0)

Summary There are multiple ways to loop: Repetitions: for <variable> in <sequence>: Loop forever: while True: Loop for a certain time: while timeRemaining(secs):

Rest of Today Take your octagon program. Use a simple loop to create the octagon rather than having the commands repeat over and over. Show me the program when complete.