G7_Unit 2_Coding_Revised
G7_Unit 2_Coding_Revised
Learning Objectives
Learners will learn to:
understand and use relational and logical operators
Vocabulary
operators
conditions
libraries
Let Us Start
1
Let Us Explore
We have already created an algorithm that uses a condition to check a condition and
Write an algorithm that accepts the user's age and determines if he/she is eligible to
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
Get, Set, Go
2.1 Operators
Operators in Python programming are special symbols or characters that perform
operations on variables and values. Let us look at the relational and logical operators
Relational operators
Relational operators are used to create a condition that compares two values. The
result.
2
are the same, it returns True.
!= not equal to It checks if two values are not equal. If x!=y results in
False.
> greater than It checks if the value on the left is x>y results in
returns False.
< less than It checks if the value on the left is less x<y results in
False.
>= greater than It checks if the value on the left is x>=y results in
<= less than It checks if the value on the left is less x<=y results in
3
Output
Logical operators
Logical Operators are used to combine multiple conditions and determine the overall
result.
In the expression “marks>37”, you can only check whether the marks are greater than
37. What if you require checking whether it is greater than 37 and less than 18? In such
cases, you can use logical operators to join multiple relational expressions to make a
more complex expression. Logical operators are also known as Boolean operators.
is true. True
NOT The output is True if the statement is (not x > 0) results in False
false.
4
Example: Program using logical operators
Program
Output
Activity 1
Assuming x=-20, y=20 and z=0, What will be the output of the following statements
in Python?
print (x+y==z)
based on certain conditions. They enable the program to make decisions and choose
5
Statement Description
if statement The if statement is used to test a specific condition. If the
executed.
that it also provides the block of the code for the false
Indentation in Python
In Python, indentation is used to declare a block. If two statements are at the same
indentation level, they are part of the same block. Generally, four spaces are given to
most used part of the Python language since it declares the block of code. All the
statements of one block are intended at the same level of indentation. If a command
The if statement
The if statement is used to test a particular condition and if the
6
Example 1: Program to find the number is greater than 5.
Program
Output
Output
7
Activity 2
Write a Python program that accepts a user's age and checks whether he/she is an
adult. The program will display a message if the age entered is more than 18.
in the false case of the condition. If the condition is true, then the if-block is executed.
Syntax
8
Example: Program to check whether a person is eligible to vote.
Program
Output
Activity 3
Write a Python program that accepts a number from the user and checks whether
program.
Syntax
9
Program
Output
tasks. These are also tested and ready to use. Using library routines saves time by not
having to write code from scratch. It can be included in programs by importing them.
10
Output
DIV
It returns the quotient when one number is divided by another number.
Program
output
ROUND
It rounds a number to a specified number of decimal places.
Program
11
Output
RANDOM
It generates a random number between x and n
Program
Output
Activity 3
Write a Python code for the following:
Unit Review
1. Relational operators are used to create a condition that compares two values.
2. Logical Operators are used to combine multiple conditions and determine the
overall result.
12
4. Indentation is used to declare a block. If two statements are at the same
6. The if-else statement provides an else block combined with the if statement executed
in the false case of the condition.
7. The elif statement enables us to check multiple conditions and execute the specific
block of statements depending upon the true condition among them.
8. Library routines are pre-written code that can be used in programs to perform
specific tasks.
9. Some commonly used library routines are MOD, DIV, ROUND and RANDOM.
i. True
ii. False
iii. Error
13
ii. if-else statement
iii. elif statement
d. Which logical operator will give the output True if the statement is false?
i. AND
ii. OR
iii. NOT
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
Programming.
____________________________________________________________________________________
____________________________________________________________________________________
___________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
___________________________________________________________________________________
14
c. Explain if - else statement with an example.
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
Lab Activity
1. Write a program to accept a character from the user and display whether it is
vowel or a consonant.
numbers input from the user. It then outputs the remainder after dividing the
15
3. Write a Python program that prompts the user to enter his/her score and
After determining the grade, the program should display the result by printing
Resources
https://www.javatpoint.com/python-tutorial
Reference Material
https://www.youtube.com/watch?v=ft8nK7NSLig
16