DEV Community

loading...
Cover image for Learning Python- basic course: day 4, The for loop

Learning Python- basic course: day 4, The for loop

Aatmaj
I'm a Computer Science enthusiast with a keen interest in Maths. I like to tinker with things around me & build projects on my own. I like to play musical instruments & practise Sahaja Yoga meditation
・4 min read

🤟Welcome all to the day 4 of our course. Today we will learn about the for loop and all it's pitfalls.😀


Loops in python
The For loop- Everyone knows that the For Loop is a type of loop which runs statements for a given number of times. Here is the for loop syntax-

for i in range (0,k):
 #statement 
#the statement will run k times
Enter fullscreen mode Exit fullscreen mode

Again, no curly braces, just indention.
Here is a sample program which prints hello five times. Note the mistakes often made by beginners.

>>> for i in range (0,5) #don't forget the semicolon
  File "<stdin>", line 1
    for i in range (0,5)
                        ^
SyntaxError: invalid syntax
>>> for i in range (0,5):
... print("hello") #don't forget the indent
  File "<stdin>", line 2
    print("hello")
    ^
IndentationError: expected an indented block
>>> for i in range (0,5):
...  print("hello")
...
hello
hello
hello
hello
hello
Enter fullscreen mode Exit fullscreen mode

Okay, the running variable i is incremented by 1. So until i equals five, the program keeps on running? Well, actually no. 😲 This happens in Java and C. But Python differs a bit over here.

In Python, the running variable is assigned a new value and not incremented. 😮 What happens is something like this- When you say "for i in range(5)", a list of numbers from zero to four [0, 1, 2, 3, 4] will be generated. (Note 0-4 and not 0-5). Then i will be assigned all the values from that list, in order, one by one. Even if we change the value of i in the middle, it doesn't affect the loop as i is just assigned the next value. more about it here I know, loss of flexibility.😢 But we can always convert the for loop into a while loop. 🙃

>>> for i in range (0,5):
...  i=0
...  print("hi")
...
hi
hi
hi
hi
hi
Enter fullscreen mode Exit fullscreen mode

Although we cannot change the running variable, there is no restriction against using it. If we want to change the default value of increment of i in the list, we can add another parameter in the for loop, as shown below.

>>> for i in range (0,10,2): #here 2 represents the increment values.
...  print(i)
...
0
2
4
6
8
Enter fullscreen mode Exit fullscreen mode

Here is a sample program to find factors of a number. There are two bugs in the program. Can you fix them? 🥳

n=int(input("Enter a number"))
for i in range (0,n):
  if n%i==0:
    print(i)
Enter fullscreen mode Exit fullscreen mode

If you have already spotted them, comment the answers below👇. For those who havent, the answer is here

The range() function makes a list of values, and the running variable is assigned the values one by one. We can also make a list manually, but that part is to be covered when dealing with Python lists, later 📅.

One thing to note here is that any changes in the variable indicating the end values doesn't affect the main program. 😐Example, in the above program if we change the value of n in the indent, it wont affect the number of times the statements are executed. This is because the range list is made already using the value we have given at the time of generating the for lop. Here is a program in which the values of end variable are changed, but the program won't get affected. 🤓

#Program to find the factorial of a number
a=int(input("Enter a number "))
for i in range (1,a):
    a=a*