Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Introduction to Computing Science and Programming I
Introduction to C Programming
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Chapter 2: Algorithm Discovery and Design
Structured programming
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 4: Conditionals and Recursion
Copyright © Texas Education Agency, Computer Programming For 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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Python Control of Flow.
Python quick start guide
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.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Programming for Linguists An Introduction to Python 24/11/2011.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
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.
PhD, Senior Lecturer, Baimuratov Olimzhon A LGORITHMS & P ROGRAMMING (P YTHON ) Lecture 3 From SDU:
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
CS101 Computer Programming I Chapter 4 Extra Examples.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
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.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.
Controlling Program Flow with Looping Structures
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Learning Javascript From Mr Saem
Chapter 6 Controlling Program Flow with Looping Structures.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
More about Iteration Victor Norman CS104. Reading Quiz.
Python Basics.
Control Flow (Python) Dr. José M. Reyes Álamo.
Python Review 1.
Module 3.
Python: Control Structures
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.
Python - Loops and Iteration
Logical Operators and While Loops
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Loops CIS 40 – Introduction to Programming in Python
CISC101 Reminders All assignments are now posted.
A LESSON IN LOOPING What is a loop?
Logical Operators and While Loops
Python While Loops.
CS 1111 Introduction to Programming Spring 2019
Presentation transcript:

Python – Part 4 Conditionals and Recursion

Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition -Header -Indented block -No limit on number of statements in the body (but at least one)

Alternative execution if – else statement if x%2==0: print (‘x is even’) else: print (‘x is odd’) -Exactly one of the alternatives executed -Alternatives are called branches

Chained conditionals if-elseif statement if x<y: print (‘x is less than y’) elif x>y: print (‘x is greater than y’) else: print (‘x and y are equal’) - Exactly one branch executed (no limit on number of elseif stmts). If there is else must be at the end

Loops What is a loop for? – To repeat a piece of code over and over. – Examples: Iterating through an array (sum, search, print, etc.) Run the main program loop (i.e. keep asking for user input until the program is over.) Etc. 5

While Statement New keyword while Syntax while ( condition ): expression1 expression2 … 6 MUST end with colon. MUST have indentation.

While execution: – Perform test – If test true, go to body. Execute body expressions. At the end of the block, go back to test. – If test is false, go on. 7 while ( condition ): expression1 expression2 … next expression

The ingredients of a loop: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” 8 1.Initialize loop variable outside of the loop. 2.Define loop condition. 3.Do loop work. 4.Change the loop variable.

Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” 9 Execution: 1.Loop variable, n, set to 0 outside the loop. 2.Perform test: 0 <=5  True. So we enter loop. 3.Print n (so, we print 0) 4.Change n from 0 to 1. 5.Go back to test. 0 OUTPUT

Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” 10 Execution: 6.Perform test: 1 <=5  True. So we enter loop. 7.Print n (so, we print 1) 8.Change n from 1 to 2. 9.Go back to test OUTPUT

Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” 11 Execution: 10.Perform test: 2 <=5  True. So we enter loop. 11.Print n (so, we print 2) 12.Change n from 2 to Go back to test OUTPUT

Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” 12 Execution: 14.Perform test: 3 <=5  True. So we enter loop. 15.Print n (so, we print 3) 16.Change n from 3 to Go back to test OUTPUT

Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” 13 Execution: 18.Perform test: 4 <= 5  True. So we enter loop. 19.Print n (so, we print 4) 20.Change n from 4 to Go back to test OUTPUT

Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” 14 Execution: 22.Perform test: 5 <= 5  True. So we enter loop. 23.Print n (so, we print 5) 24.Change n from 5 to Go back to test OUTPUT

Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” 15 Execution: 26.Perform test: 6 <= 5  False. Skip the loop. 27.Print “Out of LOOP!” Out of LOOP! OUTPUT

Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Blast off!” 16 Key points: 1.Initialize loop variable outside of the loop. 2.Determine loop condition. 3.Do loop work. 4.Change the test value.

Example 1: n = 1 while ( n <= 5 ): print n # n = n + 1 print “Blast off!” – What would happen if we didn’t change the loop variable? The loop condition would never become false. 17

Infinite loop – When the test condition never has the chance to become False, you have an infinite loop. – World’s simplest infinite loop: while ( True ): print “hi” Other possible infinite loops? 18

Infinite loop n = 5 while ( n < 6 ): print n n = n - 1 print “Blast off!” – n must always be less than

Infinite loop n = 5 while ( n != 0 ): print n n = n - 2 print “Blast off!” – n will never reach the value 0: 9, 7, 5, 3, 1, -1, -2, etc. 20

Infinite loop n = 5 while ( n >= 0 ): print n n = n - 2 print “Blast off!” – Not an infinite loop. When n reaches -1, the test wil no longer be true. 21

Recursion One function calls itself def countdown(n): if n <= 0: print ('Blastoff!' ) else: print (n) countdown(n-1) -What happens if we call ->>> coundown (3)

Recursion def print_n(s, n): if n <= 0: return print (s) print_n(s, n-1) -return statement exits the function -Base case -Recursive (general) case

Infinite recursion Recursion never reaches a base case def recurse(): recurse()

Keyboard input Built-in function called input (previous versions raw_input) Program stops and waits for the user to type something Value pressed returned to program as a string Good idea to print a prompt telling user what to input

Keyboard input >>>name =input (‘What is your name?\n’) Arthur, King of the Britons! >>>print (name) Arthur, King of the Britons! \n represents a newline

Keyboard input >>> prompt = 'What is the velocity?\n' >>> speed = input(prompt) What is the velocity? 17 >>> int(speed) 17

Part 4 End