Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Loop in Python

Uploaded by

Orin
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)
22 views

Loop in Python

Uploaded by

Orin
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/ 6

Student’s Name:

Teacher’s Name: Jahanara Taznina Orin Class: STD-VII

Chapter 7
Loops in Python

Introduction:
By default, programs in any programming language are executed in a sequential order
where each statement is executed one after the other. However, there may be situations
where a block of code needs to be repeated multiple times.
To address this, programming languages provide various types of loops that allow a set of
instructions to be repeated until a specific condition is met.
Here, we will discuss the different types of looping statements available in Python, the
syntax of different looping statements in Python along with examples for a better
understanding

Loop:
Execute a set of statements repeatedly until a particular condition is satisfied.
Q. How many types of loops are available in Python?
Python offers three types of looping Statements in Python:
1. For loop
2. While loop
3. Nested loop

Q. What is a for loop? Explain with an example.


For loop: The for loop iterates over a given sequence (it may be a list, tuple, or string).
itself allows for this.
Syntax of for Loop
for var in sequence:
Body of for

Example:
fruits = ["apple", "kiwi", "banana", "pears"]
for fruit in fruits:
print(fruit)
Output:
apple
kiwi
banana
pears
The range( ) function:
It generates a list of numbers, which is generally used to iterate over with for loop. range( ) function
uses three types of parameters, which are:
• start: Starting number of the sequence.
• stop: Generate numbers up to, but not including last number.
• step: Difference between each number in the sequence.
Python use range( ) function in three ways:
a. range(stop)
b. range(start, stop)
c. range(start, stop, step)

a. range(stop): By default, It starts from 0 and increments by 1 and ends up to stop, but
not including stop value.

Example:
for x in range(4):
print(x)
Output:
0
1
2
3
b. range(start, stop): It starts from the start value and up to stop, but not including stop value.

Example:
for x in range(2, 6):
print(x)
Output:
2
3
4
5
c. range(start, stop, step): The third parameter specifies to increment or decrement the
value by adding or subtracting the value.

Example:
for x in range(3, 8, 2):
print(x)
Output:
3
5
7

Explanation of output: 3 is starting value, 8 is stop value and 2 is step value. First print 3 and increase
it by 2, that is 5, again increase is by 2, that is 7. The output can’t exceed stop-1 value that is 8 here. So,
the output is 3, 5, 8.
Q. What is a while loop? Explain with an example.
While loop: With the while loop we can execute a set of statements as long as a condition is true. It
requires to define an indexing variable.
Syntax of while Loop in Python
while test_expression:
Body of while

Example: To print 1 to 10 numbers


num = 1
while num <= 10:
print(num)
num += 1
Output:
1
.
.
.
10
Nested Loop :
A loop inside another loop is known as a nested loop.
Syntax:
for <variable-name> in <sequence>:
for <variable-name> in <sequence>:
statement(s)
statement(s)

Example:

1. What is a for loop? Explain with an example.


2. What is a while loop? Explain with an example.
3. How many parameters are in the range( ) function? what are they?
4. Explain 3 types of range() with examples.
Program:

1. Write a Python program to print each item in the sequence until you reach
the end of the given list:
fruits = ["apple", "kiwi", "banana", "pears"]
2. Write a Python program to print the First 10 natural numbers using a while
loop.
3. Write a program to print a multiplication table of a given number
4. Write a Python program to print all even numbers from 1 to 100 natural
numbers using a while loop.
5. Correct the following program to print the following pattern.
Code:
rows = 7
for i in range(3, rows + 1):
print("+" * i)
Output:
*
**
***
****
*****

You might also like