Skip to main content

Over and Over again!

PART TWO: ITERATIONS

                    It's been a super long time since I did programming. But I crammed a lot of stuff last week for the exam which I believe went well. So I think in last blog I was telling about the baby steps of starting Python. 

           Honestly if you can talk and write English week 1 should have been easy but if you didn't THATS TOTALLY OKAY coz it's something new. It was for me especially being from a biology background. I kind of missed learning about "Human stuff" or " things which I could relate". However I thought of another narrative.

           Technology is also a part of our lives and trust me if it disappears I think we would go into crazy mode. So to all those who used to think like me that our job isn't meaningful or something. IT IS. Period. We are making people's lives easier than it used to be. okay enough of the ranting talks.

Let's go straight into the topic which is Iterations.

                  Iterations are nothing but asking the computer to do the stuff until the condition is satisfied. Now you could ask why are we doing this? It's totally simple. 

There are lot of problems where we need to keep doing the same process again and again. And being humans we woud get tired of it whereas computers don't. So there's this concept called looping. And we're using some already existing functions called " FOR" and "WHILE".

I won't be getting technical for even I'm the process of learning it. However I will share what made learning it easier. First learn the basic syntax coz you can't play a game without knowing the rules.

So if you're following some book or tutorial there will be loads of basic problems. Try taking one for example let's say: Printing the numbers upto n ( that's the value the user gives). You can try to understand the flow of the code and substitute the values and try to picture it in your head or paper.

Another thing which helped me understand the flow  was flowcharts.

 Eg: 



This is an example of while. The two loops have a lot of difference between them. When you know your range or the number of times you want the action to be repeated "For" loop would be more suitable. 

However the while loop is used when you don't know the range. so you would give a condition, until the condition is met it would be looping. If the initial values or some instruction goes unspecified there are chances of the program going into "Infinite loop".

Sample Programs:

FOR LOOPS:

a sam

So here you can see a list called colors. You might not be aware of list. It is a data type just like integers or other characters. I'll explain lists and other data types in another article. So here 4 values are passed inside it. So what are we trying to do here? We want to print these values in sequential order.

So we're using for loop here. For x? What's x? x is a variable which is going to take each value of colors one by one. First, x will be "yellow" and then the code goes to next line which is print x. This is a command to print x which nothing but "Yellow".

After this it will go to the for loop again and now x will be "black" and print "black". This action will be repeated until all the arguments inside the list colors is printed. Then we come out of the loop.

A conditional statement called "Else" is used. After colors is printed the console line goes to else and prints "done".


WHILE LOOPS:


As you can see we have initialized x in the start. Now x is a variable which keeps changing every time. Now x is 1, it goes inside the while loop.

1<=5 This is True. So it goes to next line and prints ( 1*5) --> 5

Now it goes to next line where x = 1+1 = 2      (wkt that x was 1in the start. Remember the whole initializing stuff?)

Now is x is 2. It goes inside the while loop and checks if 2<=5. This is again True. so moves forward and prints (2*5= 10)

Again x value changes as x=2+1 . Goes to while loop and checks condition and prints. This keeps going on and on until x=5.

What if x=6? So it will go into while loop checks 6<=5. This is FALSE. So it will immediate terminate  the loop and goes to next line and prints "Done".

I'll give you a small task. Comment in comment section Whats the output of this while loop code which is mentioned above. Let's see if you've understood.

I hope you could relate and it was helpful. 

These are the links I used to understand Loops:

https://www.w3schools.in/python-tutorial/loops/.



Comments

Post a Comment