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

Dcit 22 Lesson 05 Logical Operators

Uploaded by

jmmaandal01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Dcit 22 Lesson 05 Logical Operators

Uploaded by

jmmaandal01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LESSON 05

LOGICAL OPERATORS

OBJECTIVES

After the completion of this chapter, the students should be able to:
1. Understand how to use logical operators in programming; and
2. Construct a program that will use the different kinds of logical operations.

LOGICAL OPERATORS

Operators are used to perform operations on values and variables. These are the special
symbols that carry out arithmetic and logical computations. The value the operator operates
on is known as Operand.

In Python, Logical operators are used on conditional statements (either True or False). They
perform Logical AND, Logical OR and Logical NOT operations.

Operator Description Example


and Returns True if both x < 5 and x < 10
statements are true
or Returns True if one of the x < 5 or x < 4
statements is true
not Reverse the result, returns not(x < 5 and x < 10)
False if the result is true

Logical AND operator

• Logical operator returns True if both the operands are True else it returns False
• The AND operator checks whether two conditions are both True simultaneously:

The following table illustrates the result of the and operator when combining two conditions:

a b a and b
True True True
True False False
False False False
False True False

1|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


Example 1

Example 2

Example 3

Logical OR operator

• Logical or operator returns True if either of the operands is True.


• Similar to the and operator, the OR operator checks multiple conditions. But it returns
True when either or both individual conditions are True

2|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


The following table illustrates the result of the or operator when combining two conditions:

a b a or b
True True True
True False True
False True True
False False False

Example 1

Example 2

Example 3

Logical NOT operator

• Logical not operator work with the single Boolean value. If the Boolean value is True it
returns False and vice-versa.
• The not operator applies to one condition. And it reverses the result of that condition,
True becomes False and False becomes True.

3|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


The following table illustrates the result of the not operator:

a not a
True False
False True
Example 1

Example 2

Example 3

4|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


Precedence of Logical Operators

When you mix the logical operators in an expression, Python will evaluate them in the order
which is called the operator precedence.

The following shows the precedence of the not, and, and or operators:

Operator Precedence
not High
and Medium
or Low

Based on these precedences, Python will group the operands for the operator with the highest
precedence first, then group the operands for the operator with the lower precedence, and so
on.

In case an expression has several logical operators with the same precedence, Python will
evaluate them from the left to right:

a or b and c means a or (b and c)


a and b or c and d means (a and b) or (c and d)
a and b and c or d means ((a and b) and c) or d
not a and b or c means ((not a) and b) or c

Example Problems

1. Create a program where the user can input a grade of a students. If the grade is greater
than 70, display the grade and the message “PASSED”. However, if the grade is less
than 70, display the grade and the message “FAILED”.

5|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


2. Create a login program where the user will input his/her username and password. The
program must check if the username and the password matched. Use the ‘user123’
for username and ‘Pass_123’ for password.

3. Create a simple calculator. The user can choose what operator to be use. “Addition”
for adding of two numbers, “Subtraction” for subtracting of two numbers,
“Multiplication” for multiplying two numbers, “Division” for dividing two numbers, and
“Exit” for terminating the program. If the user entered invalid input display “Invalid
Input!” The program must accept either all lowercase, all uppercase, or sentence case.

6|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


7|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS

You might also like