Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Conditional Execution
The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a.
Python Programming Language
Selection (decision) control structure Learning objective
Conditional Statements Introduction to Computing Science and Programming I.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
The If/Else Statement, Boolean Flags, and Menus Page 180
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Decisions in Python elif. A new keyword elif A contraction of “else if” Used to tie two if statements (or more) together into one structure Syntax – elif,
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
Decisions in Python Bools and simple if statements.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Algorithm Design.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
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.
Variables, Types, Expressions Intro2CS – week 1b 1.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Decision Making CMSC 201 Chang (rev ).
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.
Conditional statements and boolean expressions Arithmetic, relational and logical operators.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
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.
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.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Follow up from lab See Magic8Ball.java Issues that you ran into.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Bool operators and, or, not
Python: Control Structures
Type boolean boolean: A logical type whose values are true and false.
Writing Functions( ) (Part 5)
Logical Operators, Boolean Data Types
Topics The if Statement The if-else Statement Comparing Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
ICS 3U Tuesday, September 21st.
Types, Truth, and Expressions (Part 2)
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Program Flow Control Selection & repetition
Logical Operations In Matlab.
CSc 110, Spring 2018 Lecture 13: Cumulative Sum and Boolean Logic
Nate Brunelle Today: Conditional Decision Statements
Types, Truth, and Expressions (Part 2)
Nate Brunelle Today: Conditional Decision Statements
ECS15 boolean.
Seating “chart” Front - Screen rows Back DOOR.
Types, Truth, and Expressions
Nate Brunelle Today: Conditional Decision Statements
Java Lessons 9 – 12 Mr. Kalmes.
CSE 190p University of Washington Michael Ernst
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Types, Truth, and Expressions (Part 2)
Presentation transcript:

Decisions in Python Boolean functions

A Boolean function This is a function which returns a bool result (True or False). The function can certainly work with any type of data as parameter or local variable, but the result is bool. It’s a good idea to name a bool function with a name that asks a question “is_Error” or “within_range”. Then the return value makes “sense” – if is_Error returns True, you would think there WAS an error. There is almost always an if statement in a Boolean function because the function can have two different results, True or False.

Use ONE Return statement Remember the rule of structured programming which says every control structure has one entrance and one exit. A function is a control structure. It is tempting to write a Bool function in a form like: def fun1 (x): if x > 0: return True else: return False Please do NOT! There are two exits to this function.

Why are two returns a bad thing? A common mistake in a function like this is to neglect one (or more) of the possible paths through the function, which means that it is possible to execute the function so that None is returned, not True or False! This is definitely a bug. Example: def yes_no (ans): if ans == ‘y’ or ans == ‘Y’: return True elif ans == ‘N’ or ans == ‘n’: return False What if the parameter had a value that was not one of ‘y’, ’Y’,’ n’, ’N’? The function would return None!

How to fix this? Just use an extra variable to hold the return value until the function is ready to return, then use that variable def yes_no(ans): result = False if ans == ‘y’ or ans == ‘Y’ : result = True return result If result did not get an initial value, the return statement will cause a crash, because of “name not defined”, which tells you that you have an error right there. Of course you can fix it by giving result an initial value of True (or False, depending on your specifications)

A Shortcut The previous function can actually be written in much shorter form def yes_no (ans): return ans == ‘y’ or ans ==‘Y’ The expression in the return statement must be evaluated before it can return. The relational operators and logical operator will combine to give a bool value – if the parameter had a ‘y’ in it, you get a True – anything else gives a False.

Calling Boolean functions – another shortcut When you call a Boolean function, you call it as part of another statement (since it returns a value). The value it returns is a Boolean value, True or False. If you are calling the function as part of an if statement, for example, you can call it this way: if is_error(p1): print(“something error”) This is the same shortcut described in a previous video. is_error(p1) is replaced by a Boolean value when it returns, so you do not have to compare it to something else, like “== True”.