IKH

Iterative Statements

  • If we want to execute a group of statements multiple times then we should go for Iterative statements.
  • Python supports 2 types of iterative statements;
    • for loop
    • while loop

For Loop

  • If we want to execute some action for every element present in some sequence(it may be string or collection) then we should go for for loop.

Syntax

Python
for x in sequence:
  body
  • sequence can be string or any collection.
  • Body will be executed for every element present in the sequence.

Question

To print characters present in the given string

Python
s="Ajay Shukla" 
for x in s:
  print(x)

Output

PowerShell
A
j
a
y
 
S
h
u
k
l
a

Question

To print characters present in string index wise:
Python
s=input("Enter some String: ")
i=0
for x in s:
  print("The character present at ",i,"index is :",x)
i=i+1

Output

PowerShell
Enter some String: Ajay Shukla
The character present at  0 index is : A
The character present at  0 index is : j
The character present at  0 index is : a
The character present at  0 index is : y
The character present at  0 index is :  
The character present at  0 index is : S
The character present at  0 index is : h
The character present at  0 index is : u
The character present at  0 index is : k
The character present at  0 index is : l
The character present at  0 index is : a

Question

To print Hello 10 times

Python
for x in range(10):
  print("Hello")

Output

PowerShell
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello

Question

To display numbers from 0 to 10.

Python
for x in range(11):
  print(x)

Output

PowerShell
0
1
2
3
4
5
6
7
8
9
10

Question

To display odd numbers from 0 to 20.

Python
for x in range(21):
  if(x%2!=0):
    print(x) 

Output

PowerShell
1
3
5
7
9
11
13
15
17
19

Question

To display numbers from 10 to 1 in descending order.

Python
for x in range(10,0,-1):
  print(x) 

Output

PowerShell
10
9
8
7
6
5
4
3
2
1

Question

To print sum of numbers presents inside list.

Python
list=eval(input("Enter List:"))
sum=0
for x in list:
  sum=sum+x; 
print("The Sum = ",sum)

Output

PowerShell
Enter List:[1,2,3,4,5]
The Sum = 15
PowerShell
Enter List:[50,780]
The Sum = 830

While Loop

  • If we want to execute a group of statements iteratively until some condition false, then we should go for while loop.

Syntax

Python
while condition:
  body

Question

To print numbers from 1 to 10 by using while loop.

Python
x=1 
while x <=10:
  print(x)
  x=x+1

Output

PowerShell
1
2
3
4
5
6
7
8
9
10

Question

To display the sum of first n numbers.

Python
n=int(input("Enter number:"))
sum=0
i=1
while i<=n:
  sum=sum+i
  i=i+1
print("The sum of first",n,"numbers is :",sum)

Output

PowerShell
Enter number:20
The sum of first 20 numbers is : 210

Question

Write a program to prompt user to enter some name until entering ajay.

Python
name=""
while name!="ajay":
  name=input("Enter Name:")
print("Thanks for confirmation")

Output

PowerShell
Enter Name:ajay
Thanks for confirmation

Infinite Loops

Python
i=0
while True:
  i=i+1
  print("Hello",i)

Output

PowerShell
Hello 1
Hello 2
Hello 3
...
...

Nested Loops

Sometimes we can take a loop inside another loop, Which are also known as nested loops.

Example

Python
for i in range(4):
  for j in range(4):
    print("i=",i," j=",j)

Output

PowerShell
i= 0  j= 0
i= 0  j= 1
i= 0  j= 2
i= 0  j= 3
i= 1  j= 0
i= 1  j= 1
i= 1  j= 2
i= 1  j= 3
i= 2  j= 0
i= 2  j= 1
i= 2  j= 2
i= 2  j= 3
i= 3  j= 0
i= 3  j= 1
i= 3  j= 2
i= 3  j= 3

Question

Write a program to display *’s in Right angled triangled form

Python
n = int(input("Enter number of rows:"))
for i in range(1,n+1):
  for j in range(1,i+1):
    print("*",end=" ")
  print()

Output

PowerShell
Enter number of rows:10
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *

OR

Python
n = int(input("Enter number of rows:"))
for i in range(1,n+1):
  print("*"*i)

Output

PowerShell
Enter number of rows:10
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *

Question

Write a program to display *’s in pyramid.

Python
n = int(input("Enter number of rows:"))
for i in range(1,n+1):
  print(" " * (n-i),end="")
  print("* "*i)

Output

PowerShell
Enter number of rows:10
         *
        * *
       * * *
      * * * *
     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
* * * * * * * * * *

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone

Report an error