0% found this document useful (0 votes)
16 views12 pages

CCD Module11-PL101 01

PROGRAMMING LANGUAGE

Uploaded by

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

CCD Module11-PL101 01

PROGRAMMING LANGUAGE

Uploaded by

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

INFORMATION SHEET PL 101-11.1.

1
“Python if...else Statement”

In this lesson, we will learn to create decisions in a Python program using different
forms of if..else statement.

References:
• Python Programming for Beginners
INFORMATION SHEET PL 101-11.1.1
“Python Operator Part 2”

What is if...else statement in Python?

Decision making is required when we want to execute a code only if a certain condition
is satisfied.

The if…elif…else statement is used in Python for decision making.


Python if Statement Syntax

if test expression:

statement(s)

Here, the program evaluates the test expression and will execute statement(s) only if
the test expression is True.
If the test expression is False, the statement(s) is not executed.
In Python, the body of the if statement is indicated by the indentation. The body starts with an
indentation and the first unindented line marks the end.
Python interprets non-zero values as True. None and 0 are interpreted as False.
Python if Statement Flowchart
Flowchart of if statement in Python programming
Example: Python if Statement

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")

When you run the program, the output will be:

3 is a positive number
This is always printed
This is also always printed.

In the above example, num > 0 is the test expression.


The body of if is executed only if this evaluates to True.
When the variable num is equal to 3, test expression is true and statements inside the
body of if are executed.
If the variable num is equal to -1, test expression is false and statements inside the body
of if are skipped.
The print() statement falls outside of the if block (unindented). Hence, it is executed
regardless of the test expression.

Python if...else Statement

Syntax of if...else

if test expression:
Body of if

else:

Body of else

The if..else statement evaluates test expression and will execute the body of if only
when the test condition is True.
If the condition is False, the body of else is executed. Indentation is used to separate
the blocks.
Python if..else Flowchart

Flowchart of if...else statement in Python


Example of if...else

# Program checks if the number is positive or negative


# And displays an appropriate message

num = 3

# Try these two variations as well.


# num = -5
# num = 0

if num >= 0:
print("Positive or Zero")
else:
print("Negative number")

Output

Positive or Zero

In the above example, when num is equal to 3, the test expression is true and the body
of if is executed and the body of else is skipped.
If num is equal to -5, the test expression is false and the body of else is executed and
the body of if is skipped.
If num is equal to 0, the test expression is true and the body of if is executed
and body of else is skipped.

Python if...elif...else Statement

Syntax of if...elif...else

if test expression:

Body of if

elif test expression:

Body of elif

else:

Body of else

The elif is short for else if. It allows us to check for multiple expressions.
If the condition for if is False, it checks the condition of the next elif block and so on.
If all the conditions are False, the body of else is executed.
Only one block among the several if...elif...else blocks is executed according to the
condition.
The if block can have only one else block. But it can have multiple elif blocks.
Flowchart of if...elif...else

Flowchart of if...elif....else statement in Python


Example of if...elif...else

'''In this program,


we check if the number is positive or
negative or zero and
display an appropriate message'''

num = 3.4

# Try these two variations as well:


# num = 0
# num = -4.5

if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

When variable num is positive, Positive number is printed.


If num is equal to 0, Zero is printed.
If num is negative, Negative number is printed.

Python Nested if statements

We can have a if...elif...else statement inside another if...elif...else statement. This is


called nesting in computer programming.
Any number of these statements can be nested inside one another. Indentation is the
only way to figure out the level of nesting. They can get confusing, so they must be avoided
unless necessary.

Python Nested if Example

'''In this program, we input a number


check if the number is positive or
negative or zero and display
an appropriate message
This time we use nested if statement'''

num = float(input("Enter a number: "))


if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")

Output 1

Enter a number: 5
Positive number
Output 2

Enter a number: -1
Negative number

Output 3

Enter a number: 0
Zero

Other sample programs use if...else Statement

1. Largest of Three

# using elIf Statement


a = float(input("Please Enter the First value: "))
b = float(input("Please Enter the First value: "))
c = float(input("Please Enter the First value: "))

if (a > b and a > c):


print("{0} is Greater Than both {1} and {2}". format(a, b, c))
elif (b > a and b > c):
print("{0} is Greater Than both {1} and {2}". format(b, a, c))
elif (c > a and c > b):
print("{0} is Greater Than both {1} and {2}". format(c, a, b))
else:
print("Either any two values or all the three values are equal")

Sample display:

Please Enter the First value: 23


Please Enter the First value: 45
Please Enter the First value: 8
45.0 is Greater Than both 23.0 and 8.0

2. Odd or Even

number = int(input(" Please Enter any Integer Value : "))

if(number % 2 == 0):
print("{0} is an Even Number".format(number))
else:
print("{0} is an Odd Number".format(number))

Sample display:

Please Enter any Integer Value: 7


7 is an Odd Number

3. Python Program to Calculate Profit or Loss

# Python Program to Calculate Profit or Loss

actual_cost = float(input(" Please Enter the Actual Product Price: "))


sale_amount = float(input(" Please Enter the Sales Amount: "))

if(actual_cost > sale_amount):


amount = actual_cost - sale_amount
print("Total Loss Amount = {0}".format(amount))
elif(sale_amount > actual_cost):
amount = sale_amount - actual_cost
print("Total Profit = {0}".format(amount))
else:
print("No Profit No Loss!!!")

Sample display:

Please Enter the Actual Product Price: 1000


Please Enter the Sales Amount: 900
Total Loss Amount = 100.0

4. Past or Failed

marks = int(input(" Please Enter Your Subject Marks: "))


if marks >= 50:
print(" Congratulations ")
print(" You cleared the subject ")
else:
print(" You Failed")
print(" Better Luck Next Time")
Sample display:

Please Enter Your Subject Marks: 75


Congratulations
You cleared the subject

5. Eligible to work if he is 18 years old or above.


age = int(input(" Please Enter Your Age Here: "))
if age < 18:
print(" You are Minor ")
print(" You are not Eligible to Work ")
else:
if age >= 18 and age <= 60:
print(" You are Eligible to Work ")
print(" Please fill in your details and apply")
else:
print(" You are too old to work as per the Government rules")
print(" Please Collect your pension!")

Sample display:

Please Enter Your Age Here: 14


You are Minor
You are not Eligible to Work

Please Enter Your Age Here: 27


You are Eligible to Work
Please fill in your details and apply

STUDENT NAME: __________________________________ SECTION: __________________

PERFORMANCE TASK PL 101-11.1.1


WRITTEN WORK TITLE:

WRITTEN TASK OBJECTIVE:


MATERIALS:
 Pen and Paper
TOOLS & EQUIPMENT:
 None
ESTIMATED COST: None
Instruction:

PRECAUTIONS:
 Do not just copy all your output from the internet.
 Use citation and credit to the owner if necessary.
ASSESSMENT METHOD: WRITTEN WORK CRITERIA CHECKLIST
STUDENT NAME: _____________________________ SECTION: __________________

PERFORMANCE OUTPUT CRITERIA CHECKLIST PL 101-11.1.1

CRITERIA SCORING
Did I . . .
1 2 3 4 5
1. Focus - The single controlling point made with an awareness of a
task about a specific topic.
2. Content - The presentation of ideas developed through facts,
examples, anecdotes, details, opinions, statistics, reasons, and/or
opinions
3. Organization – The order developed and sustained within and
across paragraphs using transitional devices and including the
introduction and conclusion.
4. Style – The choice, use, and arrangement of words and sentence
structures that create tone and voice.
5. .
6. .
7. .
8. .
9. .
10. .
TEACHER’S REMARKS:  QUIZ  RECITATION 
PROJECT
GRADE:

5 - Excellently Performed
4 - Very Satisfactorily Performed
3 - Satisfactorily Performed
2 - Fairly Performed
1 - Poorly Performed

_______________________________
TEACHER

Date: ______________________

You might also like