0% found this document useful (0 votes)
11 views2 pages

Loops Notes

The document explains loops in Python, specifically the for loop and while loop, detailing their syntax and usage. It provides examples of how to implement these loops to repeat actions, such as printing numbers and calculating sums. Additionally, it includes tasks for practice to reinforce the concepts learned.
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)
11 views2 pages

Loops Notes

The document explains loops in Python, specifically the for loop and while loop, detailing their syntax and usage. It provides examples of how to implement these loops to repeat actions, such as printing numbers and calculating sums. Additionally, it includes tasks for practice to reinforce the concepts learned.
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
You are on page 1/ 2

4.

Loops in Python - For Loop and While Loop


WALT:

• Understand the concept of loops in Python.


• Learn how to use the for loop and the while loop.
• Write programs using loops to repeat actions.
Keywords:
1. Loop
2. For Loop
3. While Loop
4. Range()
1. For Loop:
A for loop is used to iterate (go through) a sequence like a list or a range of numbers. It repeats the block of
code for a specific number of times.
Syntax
for variable in range(start, stop, step):
# code to execute
• start (optional): The starting point.
• stop: The point where the loop ends (exclusive).
• step (optional): The amount to increase or decrease at each step.
Example:
Write a program to print numbers from 1 to 5 using for loop:
Input code:
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
2. While Loop:
A while loop continues as long as a given condition is true. It is useful when we do not know in advance
how many times we want the loop to run.
Syntax:
while condition:
# code to execute

• condition: The condition to check before each iteration.


• The loop runs as long as the condition is true.
Example:
Write a program to print numbers from 1 to 5 using while loop.
Input Code:
i=1
while i <= 5:
print(i)
i += 1
Output:
1
2
3
4
5
Tasks:
Task 1:
a) Use a for loop to print numbers from 1 to 10.
b) Write a program using a while loop to display the numbers 5 to 15.
Task 2:
a) Use a while loop to print even numbers between 2 and 20.
b) Write a for loop to print the numbers from 10 to 1 in reverse order.
Task 3:
a) Write a for loop to print the multiplication table of 7.
b) Create a program using a while loop to calculate the sum of the first 10 natural numbers.

You might also like