You are on page 1of 11

0MODULE 13 : BOOLEAN

OBJECTIVES
Boolean Content Standards:
MODULE 13 The learners demonstrate an
understanding of the use of Boolean
in python programming.
Performance Standards:
Create a program applying Boolean
expression in python programming.
Most Essential Learning
Competencies:
13.1 Identify the different Boolean
conditions.
13.2 Set up a condition using an if -else
statement.

INTRODUCTION

The Python Boolean type is one of Python's built-in data types. It's used to represent the
true value of an expression.
In general, a Boolean variable can have only two values - True or False. Or in other
words, if a variable can have only these two values, we say that it’s a Boolean variable.
It’s often used to represent the True value of any given expression.

113
0MODULE 13 : BOOLEAN

DATA INPUT

Boolean Values
In programming you often need to know if an expression is True or False.You can evaluate
any expression in Python, and get one of two answers, True or False.When you compare two
values, the expression is evaluated and Python returns the Boolean answer:

Example:
print(8 > 6)
print(8 == 7)
print(9 < 8)

Output:
True
False
False

When you run a condition in an if statement, Python returns True or False:


Example:

Print a message based on whether the condition is True or False:


a = 150
b = 45

if b > a:
print("b is greater than a")
else:
print("b is not greater than a")

Output:
b is not greater than a

Evaluate Values and Variables


The bool( ) function allows you to evaluate any value, and give you True or False in return,

Example:
Evaluate a string and a number:
print(bool("Hello"))
print(bool(17))

Output:
True
True

114
0MODULE 13 : BOOLEAN

Example:
Evaluate two variables:
x = "Hello"
y = 25

print(bool(x))
print(bool(y))

Output:
True
True

Most Values are True


Almost any value is evaluated to True if it has some sort of content.
Any string is True, except empty strings.
Any number is True, except 0.
Any list, tuple, set, and dictionary are True, except empty ones.

Example:
The following will return True:

bool("cde")
bool(456)
bool(["oranges", "grapes", "melon"])

Output:
True

Some Values are False


In fact, there are not many values that evaluate to False, except empty values, such
as (), [], {}, "", the number 0, and the value None. And of course the value False evaluates
to False.

Example:
The following will return False:

bool(False)
bool(None)
bool(0)
bool("")
bool(())
bool([])
bool({})

Output:
False

115
0MODULE 13 : BOOLEAN

One more value, or object in this case, evaluates to False, and that is if you have an object
that is made from a class with a __len__ function that returns 0 or False:

Example:
class myclass():
def __len__(self):
return 0

myobj = myclass()
print(bool(myobj))

Output:
False

Functions can Return a Boolean


You can create functions that returns a Boolean Value:

Example:
def myFunction() :
return True
print(myFunction())

Output:
True

You can execute code based on the Boolean answer of a function:


Example:
Print “YES!” if the function returns True, otherwise print “NO!”.

def myFunction() :
return True

if myFunction():
print("YES!")
else:
print("NO!")

Output:
YES

Python also has many built-in functions that return a boolean value, like
the isinstance() function, which can be used to determine if an object is of a certain data type:

Example:
Check if an object is an integer or
not:

x = 100

116
0MODULE 13 : BOOLEAN

print(isinstance(x, int))

Output:
True

Evaluation of Boolean Expressions in Python


Mostly, if any value has some type of content in it, it is finally evaluated to True when we use
the bool() method on it. In fact, except for empty strings, all the strings evaluate to True. Any
number except 0, evaluates to True. Moreover, apart from the empty ones, all the sets, lists,
tuples, and dictionaries also evaluate to True.

Examples:
bool(["Welcome", "to", "Python"])
bool(123)
bool("Welcome")

Output:
True

Usually, empty values such as empty strings, zero values, and None, evaluate to False.
Example:
bool("")
bool([])
bool(0)
bool(None)
bool()

Output:
False

You can use the bool method to cast a variable into Boolean datatype. In the following
example, we have cast an integer into boolean type.
Example:
a=0
b = bool(a)
print(b)

Output:
False

You can also use the bool method with expressions made up of comparison operators. The
method will determine if the expression evaluates to True or False.

Example:
bool (957.34 > 957.32)

Output:
True

117
0MODULE 13 : BOOLEAN

Example:
bool (0==1)

Output:
False

Boolean Operators
Boolean operators take Boolean values as inputs and in return, they generate a Boolean result.
Broadly, we have three boolean operators in Python that are most frequently used. These are -
Not, And, and Or operators.

The Not Operator


The Not operator takes only one argument and it just returns the opposite result. For example,
if the argument is True, it returns False and vice-versa. The truth table for Not operator is
mentioned below.

A Not A
True False
False True

Example:
not True

Output:
False

Example:
not False

Output:
True

The And Operator


It takes two input arguments and evaluates to True only if both the arguments are True.
Please note that if the value of A is False, then the value of B doesn’t matter. This is called
short-circuit evaluation. In such cases, knowing only one input is enough, and hence, the
other input is not evaluated.

A B A and B
True True True
False True False
True False False
False False False

118
0MODULE 13 : BOOLEAN

Example:
True and True

Output:
True

Example:
False and True

Output:
False

Example:
True and False

Output:
False

Example:
False and False

Output:
False

The Or Operator
The Or operator returns False only when both the inputs to the operator are False, else it
always returns True.
If the first argument is True, then it’s always true. Hence, it also uses short-circuit evaluation.

A B A or B
True True True
True False True
False True True
False False False

Example:
True or True
Output:
True

Example:
False or True
Output:

119
0MODULE 13 : BOOLEAN

True

Example:
True or False

Run: (output)
True

Example:
False or False
Output:
False

DATA CHECK

Multiple Choice
Directions: Read the statements carefully. Write the letter of your answer on the space
provided before each number.

__________1. A variable that can only have two value, True or False.
A. Boolean
B. elif
C. if
D. else

__________2. Which is the correct syntax?


A. has_dog = true
B. has_dog = Treu
C. has_dog = True
D. HAS_DOG = tRue

__________3. The result of this program:


Friday = False
If Friday;
print “jeans day!”
else:
print “Dress code”
A. Jeans Day
B. Dress code
C. jeans day
D. Dress Code

__________4. What will be the output?

120
0MODULE 13 : BOOLEAN

name = ‘Dave’
greeting = “Good morning + name”
print(greeting)
A. Good morning ‘Dave’
B. Good morning name
C. Good morning Dave
D. Good morning + Dave

__________5. In python the ' BOOLEAN data type' can be defined as...?
A. holds alphanumerical data
B. holds a number with a decimal point
C. holds whole numbers
D. holds either true or false

__________6. How do we make sure that IF statements are indented correctly?


A. Don’t need to
B. Use the shift key
C. Use the tab key
D. Use the Enter key

__________7. An if statement must evaluate to:


A. Right or Wrong
B. True of False
C. True
D. False

__________8. Which of the following is not a Boolean expression?


A. True
B. 3 == 4
C. 3 + 4
D. 3 + 4 == 7

__________9. Which of the following will evaluate to True?


I. True AND False
II. False and True
III. False AND (True or False)
A. I
B. II
C. I and II
D. II and III

__________10. True is what type of variable?


A. float
B. string
C. boolean
D. Integer

121
0MODULE 13 : BOOLEAN

HANDS ON

Performance Task 4.1 Class Schedule

Directions:
1. Create a class schedule where the class can meet online every Monday and Wednesday or
Tuesday and Thursday.
2. Create Boolean variables for each day.
3. Create Boolean expression that returns True if either Monday and Wednesday is True or if
Tuesday and Thursday is True.
4. Print the result as “The answer is <True│False>”
5. Take a screenshot of your code and output from Google Colab and attach the .jpeg or .pdf
file in the assignment folder.

Example:
Monday=True, Tuesday=True, Wednesday=True, Thursday=True=> The answer is True
Monday=True, Tuesday=False, Wednesday=True, Thursday=False=> The answer is False
Monday=True, Tuesday=True, Wednesday=False, Thursday=True=> The answer is True

Rubrics:
Readability and efficiency of code : 10 points
Correctness of output : 10 points
----------------------------------------------------------
Total 20 points

REFERENCES

Python Boolean. https://www.w3schools.com/python/python_booleans.asp

MODULE CREATORS

Module Creator/Curator : Mrs. Erlyn S. Bautista


Template & Layout Designer : Mr. Luis Philip M. Oropesa

122
0MODULE 13 : BOOLEAN

ANSWER KEY

Data Check
1. A 3. B 5. D 7. B 9. B
2. C 4. C 6. C 8. C 10. C

123

You might also like