Start To Learn Python Right Now EP09: Work With FOR & WHILE loops


In the previous post, we talked about Python Gates and Gate Modifiers. In this post, we are going to talk briefly about the Loops. 

What is a Loop...

   Loop is a series or an execution that come back to the beginning after finishing the mentioned task. Also, it is performing on the task until breaks the loop. In Python, we are using two common Loop types. They are FOR loops and WHILE loops.


FOR loops...

    For loops are commonly used to get a range of items from a value. It is loop through the parent value and gets its child values one by one. So it enables you to get a specific value from a value set. This is how it works. 
x = "www.softexs.com"
for i in x:
    print(i)

The output of this is characters of "www.softexs.com". Actually, you can loop through a string because it works like a list. You can learn more about it by referring the EPISODE 05 and EPISODE06. 

Also, you can loop through the integers. Integers are numbers in Python. but You can't directly loop through the numbers or the variables assigned with the numbers. because Python integers are not iterable. However, you can loop through the integers using range() syntax. This is how to do that.

for i in range(100):
      print(i)
  This function executes all numbers until the 99. You need to keep in mind the last number is not available when you loop integers using range()


These are some real-time examples for Python FOR loops. You can use FOR loops for most data types like LISTs. You can try these with data types you know. I don't forget to comment below.



WHILE loops...

   While loops are a bit different than FOR loops because every FOR loop has a value range. It means FOR loops only iterating the range of data. But WHILE loops are not limited and they can iterate continuously without stopping. These are examples of continuous looping with WHILE.
a = 1
while a == a:
      print(a)
      a = a + 1

  This is the printing value of a unlimitedly because we equal a to a and a has the same variable. so, it has the same value every time. Then the loop never stops. Also, we are incrementing the value using an operator. If you don't have good knowledge of Python Operators you can refer the EPISODE 07.

This another way to make a WHILE loop continously 
while True:
    print("www.softexs.com")

In here WHILE is True and it is always True. and it jumps to the next task print()



Now let's see how to run a WHILE loop for limited time. We also can run WHILE loops until it finish a specific command or operation. This is how to do it

a = 10
while a == 10:
    print(a)
    a = a + 1
 
This task only runnig until the vlue of a equal 10, you can also try not equl operator with this. (!=). There I increment the a value by 1 then value of a is 11. so loop stops after a one loop time. 


This is all I need to talk about Python Loops. but you guys can explore more and learn more. Also, don't forget to put your comment below. Let's learn more in the next post. 

Post a Comment

If you have any doubt, Let us know

Previous Post Next Post