Computer Science 7 Module13
Computer Science 7 Module13
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
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Output:
b is not greater than a
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
Example:
The following will return True:
bool("cde")
bool(456)
bool(["oranges", "grapes", "melon"])
Output:
True
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
Example:
def myFunction() :
return True
print(myFunction())
Output:
True
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
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.
A Not A
True False
False True
Example:
not True
Output:
False
Example:
not False
Output:
True
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
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
121
0MODULE 13 : BOOLEAN
HANDS ON
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
MODULE CREATORS
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