0% found this document useful (0 votes)
92 views

Boolean Functions

This document discusses Boolean functions in Python. A Boolean function returns a True or False value. It is best to name Boolean functions with a question to indicate what is being checked, like "is_error()" or "within_range()". Boolean functions often contain an if statement to check for the two possible return values. Functions should only have one return statement and one exit path to avoid bugs from missing return values. A common way to write a Boolean function is to set a variable to the return value and return that variable at the end. Boolean functions can also be written concisely using expressions that directly return True or False.

Uploaded by

Thomas Adams
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Boolean Functions

This document discusses Boolean functions in Python. A Boolean function returns a True or False value. It is best to name Boolean functions with a question to indicate what is being checked, like "is_error()" or "within_range()". Boolean functions often contain an if statement to check for the two possible return values. Functions should only have one return statement and one exit path to avoid bugs from missing return values. A common way to write a Boolean function is to set a variable to the return value and return that variable at the end. Boolean functions can also be written concisely using expressions that directly return True or False.

Uploaded by

Thomas Adams
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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

You might also like