Ch 5 Python Flow Control Statement Class 9 Notes
Ch 5 Python Flow Control Statement Class 9 Notes
Introduction
Till now whatever the the programs we have done in Python runs from top to bottom, line by line. But what if we want our program to make decisions or repeat certain actions automatically?
For example:
- Greet “Good Morning” or “Good Evening” depending on the time
- Check if a number is even or odd
- Printing “Hello” 10 times.
All these situations require control flow statements in Python.
In this blog, we will learn about if statements, for loops, and while loops with the help of very simple examples and questions.
What is Python Flow Control?
Flow control means choosing the order in which our code(python) statements will execute. Let we understand in simple words, In real life, we usually make decisions like what to eat, where to go, what to do next. In Python, flow control is managed by special statements given below.
- If Statement
- For Statement
- While Statement
1. If Statement
In real life we may face such situation when we need to make some decisions (e.g., “If it rains, I will take an umbrella”) and based on these decisions, we need to decide what should we do next. Decision making statement helps a program to choose what to do next.
Python supports the following types of decision making statements
a. Simple if statement
b. if–else statement
c. if–elif-else statement or if-elif ladder
d. nested if statements
a. Simple if statement
The simple if statement is used to checks a condition in Python. If the condition is True, a set of statements will execute inside the block and if the condition is False, Python skips that block.
Syntax:
if condition:
statement(s)
For example
Example-1:
num1 = 7
if num1 > 0:
print(num1, "is a positive number.") # This is inside the if block (Indented)
print("This is always printed.") # This is outside the if block (Unindented)
In the above code, num1 > 0 is the test expression (condition). The body of “if” is executed only if this evaluates to True.
When variable num1 is equal to 7, test expression is true and statement inside body of “if” is executed.
The print() statement which falls outside of the if block (unindented) will always be executed regardless of the test expression.
Output:
7 is a positive number.
This is always printed.
Do you know:
In Python, indentation (spaces) is used to define blocks of code, not curly braces { }.
Python treats all non-zero values as True and 0 or None as False!
Example-2: Accept number from user and display “Hello”, if number is greater than 9.
num = int(input("Enter number"))
if num > 9:
print("Hello")
b. if–else statement:
The if–else statement in Python allows the program to evaluates test expression and execute a set of statements if a condition is True, otherwise another set of statements will be executed.
Syntax:
if <condition>:
statement(s)
else:
statement(s)
Example-3: Write a program to accept a number and check whether it is even or odd.
n1 = int(input("Enter any number"))
if n1%2 == 0:
print("Number is Even")
else:
print("Number is Odd")
Example-4: Write a program to accept age from the user and check whether he or she can vote or not.
age = int(input("Enter your age"))
if age >= 18:
print("Eligible for Voting")
else:
print("Not-Eligible for Voting")
Example-5: Write a program to accept age from the user and check whether a user is Senior Citizen or not.
age = int(input("Enter your age"))
if age >= 60:
print("You are Senior Citizen")
else:
print("You are not a Senior Citizen")
c. if–elif-else statement:
if test expression:
body of if
elif another test expression:
body of elif
elif another test expression
body of elif
:
:
:
else:
body of else
The elif is short for else if. It allows us to check for multiple conditions. 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, body of “else” will be executed.
NOTE: It can have only one else block. But it can have multiple elif blocks.
Example-6: Write a program to accept a number from 1 to 7 and display the name of the day like 1 for Sunday , 2 for Monday and so on.
num=int(input("Enter any number between 1 to 7 : "))
if num==1:
print("Sunday")
elif num==2:
print("Monday")
elif num==3:
print("Tuesday")
elif num==4:
print("Wednesday")
elif num==5:
print("Thursday")
elif num==6:
print("Friday")
elif num==2:
print("Saturday")
else:
print("Please enter number between 1 to 7")
Example-7: Write a program to accept a number from 1 to 12 and display name of the month and days in that month like 1 for January and number of days 31 and so on.
num=int(input("Enter any number between 1 to 7 : "))
if num==1:
print("January")
elif num==2:
print("February")
elif num==3:
print("March")
elif num==4:
print("April")
elif num==5:
print("May")
elif num==6:
print("June")
elif num==7:
print("July")
elif num==8:
print("August")
elif num==9:
print("September")
elif num==10:
print("October")
elif num==11:
print("November")
elif num==12:
print("December")
else:
print("Please enter number between 1 to 12")
Example-8: Accept the percentage from the user and display the grade according to the following criteria.
- Below 25 —- D
- 25 to 45 —- C
- 45 to 50 —- B
- 50 to 60 –– B+
- 60 to 80 — A
- Above 80 –- A+
per = int(input("Enter percentage"))
if per> 80:
print("Grade is A+")
elif per >60 and per <=80:
print("Grade is A")
elif per > 50 and per <=60:
print("Grade is B+")
elif per > 45 and per <=50:
print("Grade is B")
elif per > 25 and per <=45:
print("Grade is C")
elif per <25:
print("Grade is D")
d. nested if statements
When we have an if statement inside another if statement, this is called nested if statement.
NOTE: Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting. This can get confusing, so must be avoided if it can be.
Example-9: Accept a number and check whether it is positive or negative
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
When you run the above program the output may come as follows
Output 1
Enter a number: 5
Positive number
Output 2
Enter a number: -1
Negative number
Output 3
Enter a number: 0
Zero
Practice Time – Decision Making
Q1. Write a program to check whether a number is even or odd.
Q2. Write a program to print “Pass” if marks > 33, else print “Fail.”
Q3. Write a program to display the largest of two numbers.
Q4. Write a program to check if a number is divisible by 5 or not.
Q5. Write the output of the following code if user input a) 9 b) -5 c) 0
num = int(input('Enter a number: '))
if num > 0:
print('The number is positive.')
Q6. Write the output of the following code if user input a) 92 b) -50 c) 0
num = int(input('Enter a number: '))
if num % 2 == 0:
print('Even number')
Q7. Q6. Write the output of the following code if user input a) 92 b) 50 c) 10
age = int(input('Enter your age: '))
if age >= 18:
print('You are eligible to vote.')
else:
print('You are not eligible to vote.')
2. For Statement (For Looping/Iteration)
The for … in statement is a looping statement that iterates over a sequence of objects i.e. go through each item in a sequence like a list, tuple, string or range
Syntax:
for vr in sequence:
statement(s)
Here, vr is the variable that takes the value of the item inside the sequence. Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation.
Example-10: Program to find the sum of all numbers stored in a list given numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
s = 0
for i in numbers:
s = s + i
print("The sum is", s)
OUTPUT:
The sum is 55
Example-11: Write a program to print first 10 natural numbers.
for i in range(1, 11):
print(i)
Do You Know?
A for loop can even loop through strings, not just numbers!
Example-12: Display each character of string “PYTHON” in separate line
for i in "PYTHON":
print(i)
OUTPUT:
P
Y
T
H
O
N
3. While Statement (For Looping/Repetition)
The while statement allows us to repeatedly execute a block of statements as long as a condition is true. When the condition becomes False, the loop stops. A while statement is an example of what is called a looping statement.
while condition:
statement(s)
In while loop, condition is checked first. The body of the loop is entered only if the condition evaluates to True. After one iteration, the condition is checked again. This process continues until the condition evaluates to False.
Example-13: Write a program to print sum of first 10 natural numbers.
sum = 0
i = 1
while i <= 10:
sum = sum + i
i = i + 1
print("The sum is", sum)
In the above program, the test expression will be True as long as our counter variable i is less than or equal to 10.
NOTE:
Don’t forget to update the counter variable (i = i + 1), Otherwise, the loop will never stop — called an infinite loop!
Exercise Time – Loops
Q1. Write a program to print numbers from 1 to 10 using a for loop.
Q2. Write a program to display the multiplication table of any number using a while loop.
Q3. Write a program to calculate the factorial of a number using a for loop.
Q4. Write a program to print even numbers between 1 and 20 using a while loop.
Q5. Write the output of the following code:
a) for i in range(1, 6):
print(i)
b) for k in range(1, 11):
if k % 2 == 0:
print(k)
c) sum = 0
for j in range(1, 6):
sum = sum + j
print("Sum =", sum)
d) c = 10
while c >= 1:
print(c)
c -= 1
Difference between For and While Loop in Python
| For Loop | While Loop |
| This loop is used When number of iterations is known | This loop is used When number of iterations is not known |
| It iterates over a sequence of objects i.e. go through each item in a sequence like a list, tuple, string | This loop repeatedly execute a block of statements as long as a condition is true. |
| Example: for i in [34, 45, 67] | Example: while i > 10 |
Tips for Students
- Always use indentation properly (4 spaces) in Python.
- Use int() to convert user input to integers.
- Use float() to convert user input to decimal numbers.
- Comment your code using
#for clarity. - Give different input values to see different outputs.
Disclaimer : I tried to give you the correct Notes of Ch 5 Python Flow Control Statement Class 9 (AI-417), but if you feel that there is/are mistakes in the Notes Ch 5 Python Flow Control Statement Class 9 given above, you can directly contact me at csiplearninghub@gmail.com. Also Share your valuable feedback about the above Ch 5 Python Flow Control Statement Class 9 or any other suggestion so that I can give better content to you. The Notes of Ch 5 Python Flow Control Statement Class 9 are from NCERT BOOK available on CBSE website. The content is not copied from any other site.
Important Links
Class IX A.I. Book
Class IX AI Curriculum 2025-26
Class X AI Book
Class X AI Curriculum 2025-26
Python Manual
—————————————————————————————————————————–
Chapter 1: Algorithms and Flowcharts NOTES
Chapter 2: Introduction to Python NOTES
Chapter 3 – Introduction to tools for AI NOTES
Chapter 4 – More about List and Tuples NOTES
Chapter 5 – Flow of control and Conditions NOTES
——————————————————————————————————————————
Unit 1 – A.I. Reflection Project Cycle and Ethics Class 9 Notes Important Points
Unit 1 – A.I. Reflection Project Cycle and Ethics Class 9 MCQ
Unit 1 – A.I. Reflection Project Cycle and Ethics Class 9 Question Answers
—————————————————————————————————————————–
Unit 2 – Data Literacy NOTES Important Points
Unit 2 – Data Literacy MCQ
Unit 2 – Data Literacy Question Answers
——————————————————————————————————————————
Unit 3 – Math in AI NOTES Important Points
Unit 3 – Math in AI MCQ
Unit 3 – Math in AI Question Answers
——————————————————————————————————————————
Unit 4 – Generative AI NOTES Important Points
Unit 4 – Generative AI MCQ
Unit 4 – Generative AI Question Answers
——————————————————————————————————————————