Practice with loops! What is the output of each function below?

Slides:



Advertisements
Similar presentations
Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
Advertisements

This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Programming: Part II In this section of notes you will learn more advanced programming concepts such as branching and repetition as well as how to work.
Chapter 4: Control Structures: Selection
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Geography 465 Assignments, Conditionals, and Loops.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Chapter 2 - Algorithms and Design
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CS 101: Introduction to Computing Color replacements and targeted color replacement (if statement) Developed by Mark Guzdial, Georgia Institute of Technology,
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Course A201: Introduction to Programming 09/16/2010.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Python Basics.
Control Flow (Python) Dr. José M. Reyes Álamo.
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Making Choices with if Statements
Lesson 4 - Challenges.
Chapter 3: Decisions and Loops
The switch Statement, and Introduction to Looping
Selection and Python Syntax
Python: Control Structures
Topics The if Statement The if-else Statement Comparing Strings
Microsoft Visual Basic 2005 BASICS
Selection By Ramin && Taimoor
Line Continuation, Output Formatting, and Decision Structures
Selection CIS 40 – Introduction to Programming in Python
Python - Conditional Execution
Nate Brunelle Today: Repetition, A Trip To The Moon
Conditional Execution
Types, Truth, and Expressions (Part 2)
The most important decision statement
Types, Truth, and Expressions (Part 2)
Multiple Selections (ELIF Statements)
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Suggested Layout ** Designed to be printed on white A3 paper.
Three Special Structures – Case, Do While, and Do Until
Visual Basic – Decision Statements
Python Practice !!!.
Types, Truth, and Expressions (Part 2)
Class 13 function example unstring if if else if elif else
Program Flow.
Python Basics with Jupyter Notebook
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Chapter 2 - Algorithms and Design
COMPUTING.
Presentation transcript:

Practice with loops! What is the output of each function below? def stars3( ): result = "" for s in range(4, 10): result = result + "*" print result def stars4( ): for r in range(1, 4): result = "" for c in range(1, 3): result = result + "*" print result def stars5( ): for c in range(1, r+1):

Practice with loops and images! For each of the following functions, label the pixels of the input picture (shown on the right) in the order in which they are modified by the function. x y Pixel whose red value is modified

More Nested Loop Practice The following function should put a 50x100 green rectangle in a picture starting at pixel (50, 50): def greenRectangle (picture): for x in range ( ): for y in range ( ): px = getPixel (picture,x,y) setColor (px, green) Question to ponder: what if the picture is too small?

Making Decisions in Python Uses Boolean Logic (and, or, not) Also uses statement: “if” Basic format: statements in the program before the if stmt if CONDITION : statements to execute if CONDITION is True could have several statements inside the if stmts indented to same level would be executed more statements after the if stmt (always executed)

What if there are two options? For example, we want to print “off” if a variable equals 0 and “on” otherwise. statements in the program before the if stmt if CONDITION : statements to execute if CONDITION is True could have several statements inside the if else: statements to execute if CONDITION is False more statements after the if stmt (always executed)

What if there are more than two options? For example, we want to print “off” if a variable equals 0, print “on” if the variable equals 1 and “no clue” otherwise. statements in the program before the if stmt if CONDITION : statements to execute if CONDITION is True could have several statements inside the if elif 2ndCONDITION : statements to execute if 2ndCONDITION is True else: stmts to execute if all previous tests are False more statements after the if stmt (always executed)

Practice function writing Complete the following function: # return the number of pixels in a picture def numPixels return

Practice Complete the following function: # return true if the picture is "big" (has over 90,000 pixels) def isBig if return