Python-OE-07032025
Python-OE-07032025
break statement: The break statement immediately exits the loop when
encountered, skipping all remaining iterations.
for ch in sentence:
if ch ‘aeiou’ :
print("vowel found! Stopping the loop.")
break # Stop the loop immediately
print(ch)
continue statement: The continue statement skips the current
iteration and moves to the next one, without exiting the loop.
#Skip printing even numbers
numbers = ‘12345’
for num in numbers:
if int(num) % 2 == 0:
continue # Skip this iteration if the number
is even
print(num)
Practice Examples:
1.Inflation impact(for loop): Inflation increases the price of a product by 5%
per year. Write a Python program that takes an initial price and calculates the
price for the next 5 years.
Input:
Enter initial price: 1000
Output:
Year 1: 1050.0
Year 2: 1102.5
Year 3: 1157.63
Year 4: 1215.51
Year 5: 1276.28
loan_amount = 10000
year = 0
repayment = 1500
Given a list of GDP growth rates, identify whether each year had positive
growth or a recession (negative growth).
For the GDP growth rates [-2.3, 3.5, 1.8, -0.5, 2.0], print:
"Recession" if the rate is negative
"Growth" if the rate is positive
Output:
Year 1: Recession
Year 2: Growth
Year 3: Growth
Year 4: Recession
Year 5: Growth
reports = ["GDP_Report_USA_2024",
"Inflation_Report_UK_2023", "Trade_Report_India_2025"]
Exercises:
Notes:
Practice these built-in functions:
Strings: split, join, replace, strip, len, count