Loops in python | For loop and while loop

 

LOOPS IN PYTHON

 



Q.What are Loops?
Loop is repetation of anything that run till the command not give to stop.This is an iteration of the anything i.e possible by loop.Loop is not just to iterate and it is also to do many tasks  in simple way .If the Loop would be not available,then we had to  the code in many lines .

Q.What are the types of Loop in Python?

There are two types of Loop i.e For loop and while Loop.

In this For loop and while loop is easy to access .

Each time ,when the loop -body is executed ,is called an iteration.

Q.How to learn For Loop in Python?

To learn for loop ,you have to understand the algorithm of the program.

So first you have to learn the line i.e you have to learn to access the for loop

i.e

for i in range(startingvalue,Endingvalue,updationvalue):

It means first understand above line

Here in palce of i you can take any variable like anything whatever example: a,x,t,etc whatever you want to write,but that variable not being used  before in this program.

Then starting value i.e from where you have to starting value but if you not write starting value then by default it start from 0 (zero).

But ending value must be required,if you have n then your for loop will go upto n-1 .

And the last i.e bvalue updation not required ,just it require in slicing other wise ,if there it require or in special case updation value require.But by default it update by 1(one).

 

Now Let us understand using example:

Testcases1:

Input:

10

Output:

0

1

2

3

4

5

6

7

8

9

Testcases2:

5

Output:

0

1

2

3

4

Because for loop by default start by 0(zero).

for i in range(n):

    print(i)

 In this n is the ending value ,where the value of i start from by default 0 (Because you have not given starting value.

Then value i start from 0 and take every value upto 10-1=9

And then use print() function to every value of i.

 

Now if you want to print value of i in a single line,then use

end=””

Let understand

for i in range(10):

    print(i,end=" ")

 In this your output will come in a single line.

 

Similarily If you want to print table of any number,

So ,

Testcases1:

Input:

10

 

Output:

10 X1=10

10 X2=20

10 X3=30

10 X4=40

10 X5=50

10 X6=60

10 X7=70

10 X8=80

10 X9=90

10 X10=100

 

n=int(input("Enter a number whose table you want to print:"))

for i in range(1,11):

    print(n,"X",i,"=",n*i)

In this first take a input of any number ,then run your loop from 1 because in table the first must be multiple of 1 and upto  multiple 10.That’s why we take 11 ending value ,then this loop run from 1 to 11-1=10.

 

Now I think you have understood that how to run for loop.

So similarily we will do sum more examples to understand this for loop:

Q,Write a program to print sum of 10 numbers.

1+2+3+4+5+6+7+8+9+10

Testcases1:

Input:

10

Output:

55

Testcases2:

Input:

20

Output:

210

n=int(input("Enter a number of terms:"))

    sum=0

    for i in range(n+1):

        sum=sum+i

    print(sum)

 

·      In this n is the number of terms it means here you have a write ending number,like sum of 10 terms its means 1 to 10.

In for loop this loop will go upto n+1-1=n it means if you have wrote n=10,then your loop will go upto 10.

Now if you want to get the sum of the odd numbers from 1 to n th term then see below:

Testcases1:

10

 

Output:

25

 

Testcases2:

20

Output:

100

n=int(input("Enter a number of terms:"))

    sum=0

    for i in range(1,n+1,2):

        sum=sum+i

    print(sum)

 

In this your loop start from 1 then go upto n term of terms,and the updation 2 it means your first value of i is 1 then i=3,i=5,i=7,i=9,i=………………..n.

 

How loop will run in above program i.e

If n=11

sum=sum+i  ,where i=1

sum=1+i ,      where i=3

sum=4+i    ,where i=5

sum=9+i ,  where i=7

sum=16+i  ,where i=9

sum=25 , your loop will stop because you was upto 11 and this loop was about to go upto 11-1=10.so 11 is out of range.

 

I think you have understood about for loop and it easy to access in any program.

And without taking ending value your for loop go upto by default ending value.

 

Q.How for loop in Python is processed:Important to read?

·      The loop variable is assigned the first value in the sequence,

·      All the the statements in the body of for loop are executed with assigned value of loop variable .

·      Once step 2 is over ,the loop -variable is assigned the next value in the sequence and the loop-body is executed(i.e step 2repeated) with the new value of loop variable.

·      This continues until all values in the sequence are processed.

·       

Ø How to learn to while loop in Python?

ü To learn while loop first if you have understood the while loop then you can easily learn while loop.

A while loop is conditional loop that will repeat the instruction within itself as long as a conditionals remains true.

In while loop you have to update the value of that element :

Let us understand the using  example ,you were assigning the value  of i upto ending number similarily here you have to increase the value of i by 1 .

Q How to print 1 to n numbers ?

Testcases:

Input:

10

 

Output:

1

2

3

4

5

6

7

8

9

10

 

N=10

i=1

while(n>0):

    print(i)

    i=i+1

    n=n-1

 In this program first if you want then you can take input number of terms from the user.

And take any variable and assign 1 to that variable.

Then take while loop and take a condition i.e your n until greater than 0 till your this while loop will run.

And then print value of i using print() function.

Then to print next number i.e 2 you have to increament the value of i by 1. And now only 9 numbers are left so you have to decrease the of value of n by -1 otherwise again your while will run 10 times.


Every element loop  has its elements that control and govern its execution.A while loop has four elements that have differents purposes.These elements are given as below:

a)Intialization Expression

b)Test Expression 

c)The body of the loop

d)Update Expression


These fours keywords must have to understand ,then you can easily make use while loop.


I hope you have understood the loops in Python.

But These loops you can use two or more two times as a nested loop also.




Example of while loop to understand how it works, if you have read whole thing while loop i. e given above and you can easily understand


Q. Write a program to reverse the given number

"""Let understand the program and here we are going to execute our program using while loop to understand the program as well as how loop work or run

"""


# Take a input of the number using input()function 

n=int(input("Enter a number:"))



rev=0 #rev is reverse is 0 now


while(n>0):

rem=n%10   #rem is remainder and modolus operator return value after decimal

rev=rev*10+rem

n=n//10

#now we print reverse of the given number using print()function

print(rev)



#eg.n=121

#then rev=0 and we use while loop where we apply condition that given number is greater than 0 , if the condition is true then it will proceed for another task.

 

No comments:

Powered by Blogger.