0% found this document useful (0 votes)
33 views38 pages

Activity Sheet 3

The document outlines an activity sheet focused on control flows in Python, including conditional statements and loops. It provides objectives, reading materials, illustrative examples, and programming exercises to reinforce understanding of these concepts. Additionally, it includes evaluation criteria and viva questions related to control flow in Python.

Uploaded by

ganeshg111707
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)
33 views38 pages

Activity Sheet 3

The document outlines an activity sheet focused on control flows in Python, including conditional statements and loops. It provides objectives, reading materials, illustrative examples, and programming exercises to reinforce understanding of these concepts. Additionally, it includes evaluation criteria and viva questions related to control flow in Python.

Uploaded by

ganeshg111707
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/ 38

Activity Sheet – 3

Control Flows
Objectives:
1. Understand and apply conditional statements (if, if-else, elif) in Python.
2. Use loops (for, while) to perform repeated tasks.
3. Solve basic decision-making and iterative problems using control flow constructs.

Reading:
1. Conditional Statements
o if – Executes a block if the condition is true.
o if-else – Provides an alternative when the condition is false.
o elif – Checks multiple conditions in sequence.
o Relational operators: ==, !=, <, >, <=, >=
o Logical operators: and, or, not
2. Looping Statements
o while loop – Executes a block while a condition remains true.
o for loop – Iterates over a sequence (range, list, string, etc.).
o break – Terminates the loop.
o continue – Skips the current iteration.
3. Indentation is critical – Python uses indentation to define code blocks inside control structures.

Illustrative Examples:

Example #1:
Problem Statement: Write a python program to check whether a year is a leap year or not.
Algorithm:
1. Start
2. Declare a variable year
3. Input the value of year from the user
4. Check if the year is divisible by 4.
→ If yes, then check if the year is also divisible by 100.
→ If yes, check if it is divisible by 400
o If yes → it is a leap year
o If no → it is not a leap year
o → If not divisible by 100 → it is a leap year
5. If the year is not divisible by 4, then it is not a leap year
6. Display the result
7. Stop
Flowchart:

Python Code:
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")

Sample Input:
Enter a year: 2024

Sample Output:
2024 is a leap year.

Example #2:
Problem Statement: Write a python program to input a number n and print the sum of first n
natural numbers.

Algorithm:
1. Start
2. Declare a variable n to store the input number
3. Input value of n from the user
4. Initialize sum to 0
5. Set a counter variable i to 1
6. Repeat the following steps while i is less than or equal to n:
a. Add i to sum
b. Increment i by 1
7. After the loop ends, display the value of sum
8. Stop

Flowchart:

Python Code:
n = int(input("Enter a number: "))
total = 0
for i in range(1, n + 1):
total += i
print("Sum =", total)

Sample Input:
Enter a number: 10

Sample Output:
Sum = 55
Programming Questions
Exercise: 1
Difficulty: Easy
Question: Write a Python Programs that checks if a number is even or odd using if-else statement

Flowchart

Program
a=int(input("enter a number"))
if a%2==0:
print("even")
else:
print("odd")

Output:
Exercise: 2
Difficulty: Easy
Question: Using if elif else, write a program that categorizes a student’s grade based on this score:
A for 90 and above; B for 80 to 89; C for 70 to 79; D for 60 to 69 and F for below 60

Flowchart
Program
a=int(input("enter score"))
if a>90:
print("grade A")
elif a>80 and a<90:
print("grade B")
elif a>70 and 80>a:
print("grade C")
elif a>60 and 70>a:
print("grade D")
elif a<60:
print("grade F")
else:
print("enter valid score")
Output:

Exercise: 3
Difficulty: Easy
Question: Write a python script using a for loop that prints the numbers from One to Ten
Flowchart
Program
for num in range(1, 11):
print(num)

Output:

Exercise: 4
Difficulty: Easy
Question: Create a program that uses for loop to iterate through list of numbers and multiplies
each number by 2 and prints the results.

Flowchart
Program
for i in range(5):
a=int(input(“enter a number”))
print(a*2)

Output:
Exercise: 5
Difficulty: Easy
Question: Using a while loop, write a program that starts with a number 100 and decrements by 1
until it reaches 50.

Flowchart
Program
num = 100

while num >= 50:


print(num)
num -= 1

Output:

Exercise: 6
Difficulty: Easy
Question: Using a for loop and a continuous statement, print numbers from 1 to 30 but skip any
number that is a multiple of 5.
Flowchart

Program
for num in range(1, 31):
if num % 5 == 0:
continue
print(num)

Output:

Exercise: 7
Difficulty: Medium
Question: Write a program that uses multiple if statement to find the largest of three numbers.
Flowchart

Program
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))

largest = a

if b > largest:
largest = b
if c > largest:
largest = c

print("The largest number is:", largest)

Output:

Exercise: 8
Difficulty: Medium
Question: Write a python script that takes a temperature value as input and prints “It’s a hot day”
if the temperature is over 30 degrees, “It’s a nice day” if the temperature is between 20 and 30
degrees and “Its cold” if its below 20 degrees
Flowchart
Program
a=int(input("enter temperature in degrees celsius"))
if a>30:
print("it is a hot day")
elif a>20 and a<30:
print("it is a nice day")
elif a<20:
print("it is a cold day")
else:
pass

Output:

Exercise: 9
Difficulty: Medium
Question: Write a python program using a for loop that asks the user to input five numbers and
prints the sum of those numbers
Flowchart

Program
total = 0

for i in range(5):
num = int(input("Enter a number: "))
total += num

print("Sum of the numbers:", total)

Output:
Exercise: 10
Difficulty: Medium
Question: Write a Python program using a while loop that continuously requests numbers from
the user and stops if the user enters a negative number, using a break statement
Flowchart
Program
while True:
num = int(input("Enter a number: "))
if num < 0:
break
Output:
Exercise: 11
Difficulty: Medium
Question: From a given list of integers containing both positive and negative numbers, use a loop
with the break statement to print the first positive number.
Flowchart

Program
while True:
a=int(input(“enter an integer”))
if a>0:
print(a)
break

Output:
Exercise: 12
Difficulty: Hard
Question: Write the python code to check whether the number given is prime or composite.
Flowchart

Program
num = int(input("Enter a number: "))

if num <= 1:
print("Neither prime nor composite")
else:
for i in range(2, num):
if num % i == 0:
print("Composite")
break
else:
print("Prime")
Output:

Exercise: 13
Difficulty: Hard
Question: Write a python program using nested loops that prints a multiplication table for
numbers 1 through 10.
Flowchart

Program
print("Multiplication Table (1 to 10)\n")
for i in range(1, 11):
for j in range(1, 11):
print(i * j, end=" ")
print()

Output:

Exercise: 14
Difficulty: Medium
Question: Online Shopping Discount
An e-commerce site applies discounts based on purchase amount and membership:
 If amount ≥ 5000 and membership = “Gold” → 20% discount
 If amount ≥ 3000 and membership = “Silver” → 15% discount
 If amount ≥ 2000 and no membership → 10% discount
 Else → No discount
Program should take purchase amount + membership type and print final bill.
SAMPLE OUTPUT
Enter purchase amount (₹): 6000
Enter membership type (Gold/Silver/None): Gold
------ BILL SUMMARY ------
Purchase Amount : ₹6000.00
Membership Type : Gold
Discount Applied: 20%
Final Bill : ₹4800.00
Flowchart
Program
amount=float(input("Enter purchase amount (₹): "))
membership=input("Enter membership type (Gold/Silver/None): ")
if amount >= 5000 and membership=="Gold":
discount=20
elif amount >= 3000 and membership=="Silver":
discount=15
elif amount >= 2000 and membership=="no membership":
discount=10
else:
discount=0
bill=amount-(amount*discount/100)
print("------ BILL SUMMARY ------ ")
print("Purchase Amount : ₹",amount)
print("Membership Type : ",membership)
print("Discount Applied: {}%".format(discount))
print("Final Bill : ₹",bill)
Output
Exercise: 15
Difficulty: Medium
Question: Hospital Triage System
A hospital assigns patients to wards based on conditions:
 If age > 60 and symptoms = “severe” → Emergency Ward

 If age > 60 and symptoms = “mild” → General Ward

 If age ≤ 60 and symptoms = “severe” → Special Observation Ward

 Else → OPD Consultation

Input: patient’s age + severity. Output: assigned ward.


SAMPLE OUTPUT

Flowchart
Program
age=int(input("Enter patient's age: "))
symp=input("Enter symptom severity (severe/mild):")
print("------HOSPITAL TRIAGE RESULT------")
print("Age : ", age)
print("Symptoms : ", symp)
if age>60 and symp=='severe':
print("Assigned : Emergency Ward")
elif age>60 and symp=='mild':
print("Assigned : General Ward")
elif age<=60 and symp=='severe':
print("Assigned : Special Observation Ward")
else:
print('Assigned : OPD Consultation')
Output
Exercise: 15
Difficulty: Medium
Question: College Admission
Admission criteria:
 If PCM average ≥ 90 → Eligible for Engineering + Scholarship

 If PCM average 75–89 → Eligible for Engineering (self-finance)

 If PCM average 60–74 → Eligible for Arts/Science only

 If PCM average < 60 → Not eligible

Input: Physics, Chemistry, Math marks. Output: admission category.


SAMPLE OUTPUT
Flowchart
Program
p=float(input('Enter Physics marks :'))
c=float(input('Enter Chemistry marks :'))
m=float(input('Enter Mathematics marks :'))
pcm=p+c+m
pcm/=3
print('------COLLEGE ADMISSION RESULT------')
print('Physics :',p)
print('Chemistry :',c)
print('Maths :',m)
print('PCM Avg :',pcm)
if pcm>=90:
print('Category : Eligible for Engineering and Scholarship',)
elif pcm>74:
print('Category : Eligible for Engineering(self-finance))',)
elif pcm>59:
print('Category : Eligible for Arts/Science',)
elif pcm<60:
print('Category : Not Eligible',)
Output
Exercise: 16
Difficulty: Hard
Question: Airline Baggage Check
Rules:
 If baggage ≤ 15 kg → Free

 If 15 < baggage ≤ 30 → ₹500 charge

 If baggage > 30 → Not allowed

 Special case: If passenger is business class, extra 10 kg allowed free

Input: baggage weight + travel class. Output: baggage status.

SAMPLE OUTPUT
Flowchart
Program
p=float(input('Enter baggage weight (kg):'))
c=input('Enter travel class (Economy/Business) :')
print('------AIRLINE BAGGAGE RESULT------')
print('Travels Class:',c)
print('Baggage (kg) :',p)
if p<=15 or (c=='Business' and p<=25):
print('Status : Free')
elif p<=30 or (c=='Business' and p<=40):
print('Status : ₹500 charge')
elif p>30 or (c=='Business' and p>40):
print('Status : Not allowed ')
Output

Exercise: 17
Difficulty: Medium
Question: Banking Loan Eligibility
Loan approval depends on salary and credit score:
 If salary ≥ 50,000 and credit score ≥ 750 → Loan Approved

 If salary ≥ 30,000 and 650 ≤ credit score < 750 → Loan Approved with high interest

 If salary < 30,000 or credit score < 650 → Loan Rejected

Input: salary, credit score. Output: loan decision.


SAMPLE OUTPUT

Flowchart
Program
p=int(input('Enter monthly salary ($):'))
c=int(input('Enter credit score:'))
print('------LOAN ELIGIBILITY RESULT------')
print('Salary :',p)
print('Credit Score :',c)
if salary >= 50,000 and credit score >= 750:
print('Decision : Loan Approved')
elif salary >= 30,000 and 650 <= credit score < 750:
print('Decision : Loan Approved with High Interest')
elif salary < 30,000 or credit score < 650:
print('Decision : Loan Rejected ')
Output

Exercise: 18
Difficulty: Hard
Question: Smart Home Thermostat
Smart AC decisions:
 If temperature ≥ 35 and humidity ≥ 70 → AC ON (High Power)

 If 28 ≤ temperature < 35 and humidity ≥ 50 → AC ON (Normal Power)

 If temperature < 28 → AC OFF

 Special case: If user sets “Eco Mode”, AC runs only when temperature ≥ 30

Input: temperature, humidity, eco mode (Yes/No). Output: AC status.


SAMPLE OUTPUT
Flowchart
Program
t=float(input("Enter temperature (°C): "))
h=float(input("Enter humidity (%):"))
e=input("Is Eco Mode ON? (Yes/No):")
print("------ SMART THERMOSTAT RESULT -------")
print("Temperature:",t)
print("Humidity:",h)
print("Eco Mode:",e)
if (t>=35 and h>=70) and (e=="Yes"and t>=30):
print("AC Status: AC ON(High Power)")
elif (28<=t<35 and h>=50) and (e=="Yes"and t>=30):
print("AC Status: AC ON(Normal Power)")
elif t<28:
print("AC Status: AC OFF")
Output

Exercise: 19
Difficulty: Medium
Question:
Positive Number Collector: Write a program that uses a while loop to collect numbers from the user
until they enter '0'. Use a continue statement to skip and not count any negative numbers, printing out the
sum of only positive numbers entered once the loop ends.

Flowchart
Program
total = 0

while True:
num = int(input("Enter a number (0 to stop): "))
if num == 0:
break
if num < 0:
continue
total += num

print("Sum of positive numbers:", total)

Output
Exercise: 20
Difficulty: Easy
Question:
Count to Ten: Write a Python script using a for loop that prints numbers from 1 to 10.

Flowchart

Program
for num in range(1, 11):
print(num)

Output
Assignment Evaluation:
0: Not Done [ ] 1: Incomplete [ ] 2: Late Complete [ ]
3: Needs Improvement [ ] 4: Complete [ ] 5: Well Done [ ]

Viva Questions
1. What is control flow in Python?
2. Explain the structure of an if-elif-else block with an example.
3. How does Python decide which block of code to execute in an if-else statement?
4. What is the difference between = and == in a condition?
5. What happens if you forget to indent inside an if block?

You might also like