0% found this document useful (0 votes)
3 views25 pages

Python Program Flow Control

The document provides an overview of Python flow control, focusing on conditional blocks, loops, and their syntax. It includes examples of using 'if', 'else', and 'elif' statements, as well as practical applications of loops in programming. Additionally, it outlines various programming exercises related to decision-making and calculations.

Uploaded by

matthewtereza7
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 views25 pages

Python Program Flow Control

The document provides an overview of Python flow control, focusing on conditional blocks, loops, and their syntax. It includes examples of using 'if', 'else', and 'elif' statements, as well as practical applications of loops in programming. Additionally, it outlines various programming exercises related to decision-making and calculations.

Uploaded by

matthewtereza7
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

Python Program Flow Control:

Conditional Blocks
Introduction to Flow Control

• Flow control determines the order in which


individual statements, instructions, or function
calls are executed or evaluated

• Importance: Helps in decision-making and


repetitive tasks.

• Types: Sequential, Conditional, and Looping


constructs.
Conditional Blocks

• A construct used to execute specific blocks of


code based on conditions.

• Structure: Uses keywords like `if`, `else`, and


`elif` (else if).
Syntax of Conditional Blocks
Example 1 - if statement

• Checking if a number is positive.


number = 10
if number > 0:
print("The number is positive")

Output: "The number is positive"


Example 2 - if-else statement

• Checking if a number is positive or negative.

number = -5
if number > 0:
print("The number is positive")
else:
print("The number is negative")

Output: "The number is negative"


Example 3 - if-elif-else statement
Grading system based on marks
marks = 85
if marks >= 90:
print("Grade: A+")
elif marks >= 75:
print("Grade: A")
elif marks >= 60:
print("Grade: B")
else:
print("Grade: C")

Output: "Grade: A"


• Write a Python program that takes a number as input and checks whether it is positive,
negative, or zero. The program should display an appropriate message based on the
input.
• Write a Python program that takes three numbers as input and finds the largest among
them. The program should compare the numbers and print the largest one.
• Write a Python program that takes two numbers and an operator as input, then performs
the corresponding arithmetic operation (+, -, *, /). The program should handle division
by zero and invalid operators by displaying appropriate error messages.
• Write a Python program to determine a student's grade based on their marks out of 100.
The grading criteria are as follows:
• A: Marks ≥ 90
• B: 80 ≤ Marks < 90
• C: 70 ≤ Marks < 80
• D: 60 ≤ Marks < 70
• E: 50 ≤ Marks < 60
• F: Marks < 50
Write a Python program to calculate the electricity bill based on the number of
units consumed. The billing rates are as follows:
 ₹1.50 per unit for the first 100 units.
 ₹2.00 per unit for the next 200 units (101–300 units).
 ₹3.00 per unit for units above 300.
• The program should take the number of units as input and display the
total bill amount rounded to two decimal places.

1) Write a Python program to calculate the Body Mass Index (BMI) of a person based
on their weight and height, and categorize the individual into one of four health
categories: Underweight, Normal weight, Overweight, and Obesity.
• Underweight: BMI < 18.5
• Normal weight: 18.5 ≤ BMI < 24.9
• Overweight: 24.9 ≤ BMI < 29.9
• Obesity: BMI ≥ 29.9
Expt. 2 RP LAB
• Program 1: Check if a Number is Positive, Negative, or Zero
• Program 2: Find the Largest of Three Numbers
For Loops in Python
• For loops are used to iterate over a sequence (e.g., list,
tuple, dictionary, set, or string).
• Purpose: Automates repetitive tasks by executing code for
each item in a sequence.

• Iterates through each item in the sequence.


• Stops when the sequence ends.

• variable: A temporary variable that takes the value of each


item in the sequence, one at a time.
• sequence: The iterable (list, tuple, string, range, etc.) being
iterated over.
For Loops with Ranges

• The range() function generates a sequence of


numbers.
For Loops with Strings
• A string is a sequence of characters. A for loop
can iterate over each character.
For Loops with Lists
• Lists are collections of items that can be
iterated through using a for loop.
For Loops with Dictionaries

• Dictionaries store key-value pairs. You can


iterate over keys, values, or both.
Introduction to While Loops
• While loops execute a set of statements as long as a
condition is true.

• The condition is evaluated.


• If the condition is True, the code block is executed.
• After executing the code block, the condition is rechecked.
• The loop continues until the condition becomes False.
Loop manipulation using pass,
continue, break and else
pass:
• The pass statement is a placeholder and does nothing when
executed.
• It’s used to maintain the structure of code when no action
is required.
• Useful in situations where code needs to be written later.

Eg
for i in range(5):
if i == 3:
pass # Placeholder for future logic
print(i)
• break:
• Terminates the loop immediately, even if the
loop condition is still true.

for i in range(5):
if i == 3:
break # Exit the loop
print(i)
Using break to Exit a while Loop
Using continue to Skip Iterations
# Print numbers from 1 to 5, skipping 3
i=1
while i <= 5:
if i == 3: # Skip the number 3
i += 1 # Ensure the loop doesn't get stuck
continue
print(i)
i += 1
Practical Applications

• Examples:
• - Decision-making in games.
• - User authentication systems.
• - Controlling the flow of web applications.
Summary

• Key Takeaways:
• - Conditional blocks help in decision-making.
• - Syntax: `if`, `else`, and `elif`.
• - Use cases in real-world scenarios.
Q&A

• Do you have any questions?


Thank You

• For further queries, contact Prof. Mohini Naik.

You might also like