Conditionals and Recursion. Python Part 4

Similar documents
Python for Informatics

Flow Control: Branches and loops

Conditionals: Making Choices

Functions and Recursion

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Conditional Expressions and Decision Statements

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Control Structures 1 / 17

CS 1301 Exam 1 Answers Fall 2009

Iteration. Chapter 7. Prof. Mauro Gaspari: Mauro Gaspari - University of Bologna -

CS 1301 CS1 with Robots Summer 2007 Exam 1

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

bool bool - either true or false

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

CS 115 Lecture 8. Selection: the if statement. Neil Moore

How Do Robots Find Their Way?

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS 115 Lecture 4. More Python; testing software. Neil Moore

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

Python - Loops and Iteration

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

Programming Fundamentals

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Chapter 8 Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures

The Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Variables, expressions and statements

More Coding Basics with Some Turtle

5. Control Statements

61A Lecture 3. Friday, September 5

DM536 Introduction to Programming. Peter Schneider-Kamp.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Comp 151. Control structures.

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Control Flow: Branching booleans and selection statements CS GMU

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

DM536 Introduction to Programming. Peter Schneider-Kamp.

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

CS 1301 Exam 1 Fall 2009

Intro. Scheme Basics. scm> 5 5. scm>

Fundamentals of Programming (Python) Getting Started with Programming

Chapter 4. Conditionals and recursion. 4.1 The modulus operator. 4.2 Conditional execution

Basic Syntax - First Program 1

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

PREPARING FOR PRELIM 1

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

DM502 Programming A. Peter Schneider-Kamp.

Loops and Iteration. Chapter 5. Python for Informatics: Exploring Information

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Chapter 2. C++ Basics

Python Boot Camp. Day 3

cs1114 REVIEW of details test closed laptop period

Hacettepe University Computer Engineering Department. Programming in. BBM103 Introduction to Programming Lab 1 Week 4. Fall 2018

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

(Python) Chapter 3: Repetition

CONTROL FLOW CREATING ALTERNATE PATHS OF EXECUTION USING: BRANCHING WHILE LOOPS FOR LOOPS

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Python as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada. March 6th, 2016

LOOPS. Repetition using the while statement

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Introduction to Programming

3. Logical Values. Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

Here n is a variable name. The value of that variable is 176.

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Decisions, Decisions. Testing, testing C H A P T E R 7

Getting Started with Python

CSE 115. Introduction to Computer Science I

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Control of Flow. There are several Python expressions that control the flow of a program. All of them make use of Boolean conditional tests.

REPETITION CONTROL STRUCTURE LOGO

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Text Input and Conditionals

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

CSC Software I: Utilities and Internals. Programming in Python

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

Beyond Blocks: Python Session #1

8. Control statements

Ruby: Introduction, Basics

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Comp 151. Control structures.

Basics of Programming with Python

Transcription:

Conditionals and Recursion Python Part 4

Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print (remainder) 1

Boolean expressions An expression that is either true or false Operator == >>>5==5 True >>>5==6 False

Boolean expressions Type bool True and False >>>type (True) <type bool > >>>type (False) <type bool >

Boolean expressions Other operators: x!= y # x is not equal to y x > y # x is greater than y x < y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to y

Logical operators And, or, not Semantics similar to their meaning in English x>0 and x<10 not(x>y) Any nonzero number in Python is interpreted as true >>> 17 and True True

If statement Conditional execution 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

What is a loop for? Loops 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. 10

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

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. while ( condition ): expression1 expression2 next expression 12

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

Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print Out Of LOOP! 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. OUTPUT 0 14

Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print Out Of LOOP! 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 0 1 15

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

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

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

Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print Out Of LOOP! Execution: 22. Perform test: 5 <= 5 True. So we enter loop. 23. Print n (so, we print 5) 24. Change n from 5 to 6. 25. Go back to test. OUTPUT 0 1 2 3 4 5 19

Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print Out Of LOOP! OUTPUT 0 1 2 3 4 5 Out of LOOP! Execution: 26. Perform test: 6 <= 5 False. Skip the loop. 27. Print Out of LOOP! 20

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

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. 22

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? 23

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

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

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. 26

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)

def print_n(s, n): if n <= 0: return print (s) print_n(s, n-1) Recursion - 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

End Part 4