Featured

    Featured Posts

Nested Loops In Python



§  One loop inside another loop is called a Nested Loop. 
§  Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times.

for i in range(1,5):
    #codes inside the body of outer loop
    for j in range(1,5):
        #codes inside the body of both outer and inner loop

    #codes inside the body of outer loop

n=int(input('enter number'))
#outer loop 
for i in range(1,n+1):
    print("Outer loop iteration " , i);
    #inner loop
    for j in range(1,5):
        print("i= ",i," j=",j)

enter number 5

Outer loop iteration  1
i=  1  j= 1
i=  1  j= 2
i=  1  j= 3
i=  1  j= 4
i=  1  j= 5
=================
Outer loop iteration  2
i=  2  j= 1
i=  2  j= 2
i=  2  j= 3
i=  2  j= 4
i=  2  j= 5
=================
Outer loop iteration  3
i=  3  j= 1
i=  3  j= 2
i=  3  j= 3
i=  3  j= 4
i=  3  j= 5
=================
Outer loop iteration  4
i=  4  j= 1
i=  4  j= 2
i=  4  j= 3
i=  4  j= 4
i=  4  j= 5
=================
Outer loop iteration  5
i=  5  j= 1
i=  5  j= 2
i=  5  j= 3
i=  5  j= 4
i=  5  j= 5

Explanation of the above code
§  First, the 'i' variable is initialized to 1 and then program control passes to the i<=n.
§  The program control checks whether the condition 'i<=n' is true or not.
§  If the condition is true, then the program control passes to the inner loop.
§  The inner loop will get executed until the condition is true.
§  After the execution of the inner loop, the control moves back to the update of the outer loop, i.e., i++.
§  After incrementing the value of the loop counter, the condition is checked again, i.e., i<=n.
§  If the condition is true, then the inner loop will be executed again.
§  This process will continue until the condition of the outer loop is true.

Example: Write an Python Program to Print number 1 to 10, 5 times
for i in range(1,6):
    for j in range(1,11):
        print(j,end='')
    # similar to new line printing after each multiplication table
    print()
Output:
12345678910
12345678910
12345678910
12345678910
12345678910

Example: Write an Python Program to Print table 1 to 10,
 # program to print the first ten multiplication tables
for i in range(1, 11):
    print("i =", i, "->", end = " ")
    for j in range(1, 11):
        print("{:2d}".format(i * j), end = " ")  
    print() # similar to new line printing after each multiplication table

Output:

i = 1 ->  1  2  3  4  5  6  7  8  9 10 
i = 2 ->  2  4  6  8 10 12 14 16 18 20 
i = 3 ->  3  6  9 12 15 18 21 24 27 30 
i = 4 ->  4  8 12 16 20 24 28 32 36 40 
i = 5 ->  5 10 15 20 25 30 35 40 45 50 
i = 6 ->  6 12 18 24 30 36 42 48 54 60 
i = 7 ->  7 14 21 28 35 42 49 56 63 70 
i = 8 ->  8 16 24 32 40 48 56 64 72 80 
i = 9 ->  9 18 27 36 45 54 63 72 81 90 
i = 10 -> 10 20 30 40 50 60 70 80 90 100




While loop In Python

while loop:
§  If we want to execute a group of statements multiple times until some condition false, then we should go for while loop.
Syntax:
          while condition:
                   body


§  Write a program to display first n natural numbers.
n=int(input('enter number'))
i=1
while i<=n:
    print(i)
    i=i+1
Output:
enter number5
1
2
3
4
5

Note-
·        In implementation when we need to extract last digit from the given data than go for %10. If we require to remove last digit from the given number than go for divide by /10
a= 158%10  à 8
a=123%10 à 3
a=97%10 à 7
a=5%10 à 5
Note-
If the numerator value is less than of denominator value than if it is modular operator returns numerator value.
a= 158//10  à 15
a=123//10 à 12
a=97//10 à 9
a=5//10 à 0
Note-
If the numerator value is less than of denominator value than if it is division operator returns zero.

§  Write a program to display the sum of all digits to given number.
n=int(input('enter number'))
sum=0
while n>0:
    d=n%10
    sum=sum+d
    n=n//10
print('Sum of Digits = ',sum)
Output:
enter number123
Sum of Digits =  6

§  Write a program to display the reverse of a given number.
n=int(input('enter number'))
rev=0
while n>0:
    d=n%10
    rev=rev*10+d
    n=n//10
print('Reverse of Number = ',rev)
Output:
enter number123
Reverse of Number =  321



§  Write a program to check whether given number is an Armstrong or not.
Armstrong number is a number that is equal to the sum of cubes of its digits.
 For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.
 Let's try to understand why 153 is an Armstrong number.
153 = (1*1*1)+(5*5*5)+(3*3*3)  
where:  
(1*1*1)=1  
(5*5*5)=125  
(3*3*3)=27  
So:  
1+125+27=153  

n=int(input('enter number'))
sum=0
temp=n
while n>0:
    #Extracting the last digit from given number
    d=n%10
    sum=sum+(d**3)
    n=n//10
if temp==sum:
    print(temp," is a ArmStrong Number")
else:
    print(temp," is a ArmStrong Number")

Output:
enter number153
153  is a ArmStrong Number

For loop in Python||Part-2



Write a program to display table for given number in Python 

n=int(input('enter number'))
for i in range(1,11):
    t=n*i
    print(n," * ",i," = ",t)


 

§  Write a program Calculate the sum and average of first n natural numbers.


n = int(input("Enter Number to calculate sum and avg of first n natural number"))
sum = 0
for num in range(1, n+1):
    sum = sum+num
print("SUM of first ", n, "numbers is: ", sum )
avg=sum/n
print("Average of first ", n, "numbers is: ", avg )

Output:
Enter Number to calculate sum and avg of first n natural number 5
SUM of first  5 numbers is:  15
Average of first  5 numbers is:  3.0

Write a program to display the sum first n even and odd number in Python 


n=int(input('enter number'))

esum=0
osum=0
for i in range(1,n+1):
    if i%2==0:
        esum=esum+i
    else:
        osum=osum+i
print('Sum of First ',n,'Even number = ',esum)
print('Sum of First ',n,'Odd number = ',osum)

Output:
enter number10
Sum of First  10 Even number =  30
Sum of First  10 Odd number =  25


§  Write a program to display the factor of a given number.
n=int(input('enter number'))
print('Factor of ',n,end='')
for i in range(1,n+1):
    if n%i==0:
        print(i,sep=',',end='')
Output:
enter number 6
Factor of  6 is 1,2,3,6

§  Write a program to find the factorial of a given number.
What is a factorial?

§  In maths, the factorial of a non-negative integer, is the product of all positive integers less than or equal to this non-negative integer.

For example:

5! = 5 * 4 * 3 * 2 * 1 = 120

4! = 4 * 3 * 2 * 1 = 24

n factorial” is “
n! = n * (n — 1) * (n — 2) *…* 2 * 1,” where ’n’ is a positive integer.

n=int(input('enter number'))
f=1
for i in range(1,n+1):
    f=f*i
print('factorial of',n,' is ',f)
Output:
enter number5
factorial of 5  is  120


www.CodeNirvana.in

Powered by Blogger.

About

Site Links

Popular Posts

Translate

Total Pageviews