Todays session What are loops in Python? while loop – repeating based on condition for loop – repeating over sequences range() function Using break and continue Nested loops
What are loops? Loops allow you to repeat a set of instructions automatically. Instead of writing the same code multiple times, you use a loop to run it many times with less code.
while loop A while loop keeps on repeating when its condition is true Python checks the condition before each repetition
Example x = 15 If x >= 9: print(x) x = x-1
for loop A for loop is used to loop through a sequence (like a list, string, or a range of numbers) `
Example for i in range(1, 6): print("Number:", i )
range() The range() function creates a sequence of numbers Example: range(5) → 0, 1, 2, 3, 4 range(1, 6) → 1, 2, 3, 4, 5 range(1, 10, 2) → 1, 3, 5, 7, 9