Python Tutorial For Beginners- Part 4

The good thing about python is very easy to learn, and it can be used in almost every industry you want to develop such as developing their web apps, machine learning models. In this part of the python tutorial for beginners, we will deep dive into the while & for loops.

1. While Loops

Python has two types of loops. The first is “while” loops, and the second is “for” loops. We will start with the while loops, which will try to execute a piece of code you specify forever since the condition you given is True, but when it turns to false, it stops looping (running that piece of code) and move to the other part of your program. Let’s see an example:

while True:
    print ("Python")

When you run the code, it will print the word python forever since the condition is always true. In real applications, it will evaluate whether the condition is true or not. Based on that condition, it will complete looping or stop. Let’s see an example:

number = 0
while (number <= 2):
    print("Python")
    number = number + 1

Output:

Python
Python
Python

Let me explain what the code is doing. First, we declare the number variable and give it a zero value. Then we check whether the number is less or equal to 2, which is True for now. 

After that, the “while” loop will execute the print function to write the Python word and add a value to the number variable, and it will run again and again until the number variable is bigger than two, which mean the condition becomes false and no longer the “while” loop will execute the code inside it.

You can also break the while statement even when it runs if a condition is met inside this loop. Let’s see an example:

x = 1
while x < 5:
  print(x)
  if x == 3:
    break
  x = x + 1

Output:

1
2
3

When the loop starts executing the code inside it, it starts evaluating the if condition whether the number is three or not and keeps ignoring that until the number is three and breaks the while look using the break clause.

There is also another clause it can be used inside the while loop called the continue clause. Unlike the break clause, it returns the control to the beginning of your while loop, where it stops the whole loop. Let’s see an example:

x = 1
while x < 5:
  x = x + 1
  if x == 3:
    continue
  print(x)

Output:

2
4
5

The continue clause will reject all the remaining code and return to the beginning of the while loop.

2. For Loops

The for loops is similar to while loops, but it is used to iterate over sequences such as list items, tuples items, and more. Let’s see a simple example to understand:

capitals = ["Berlin", "London", "Moscow"]
for x in capitals:
    print(x)

Output:

Berlin
London
Moscow

You can see it loops over the list, which is named capitals, and in every iteration, it assigns the value to the x variable and prints it. The string is also iterable to the for a loop. Let’s see an example:

for x in "Berlin":
    print(x)

Output:

B
e
r
l
i
n

We can also use the break clause to stop the looping when you reach a specific item. Let us demonstrate with an example:

capitals = ["Berlin", "London", "Moscow"]
for x in capitals:
    
    if x == "London":
        break
    print(x)

It will only print the first value, Berlin, since when it reaches the second value London, the break clause stops the for loops. You can use the continue clause to avoid a specific item. Let’s see an example:

capitals = ["Berlin", "London", "Moscow"]
for x in capitals:
    
    if x == "London":
        continue
    print(x)

Output:

Berlin
Moscow

It will iterate over again after reaching the London value and not printing it, which make it useful in some situation. 

3. Range Function

Let’s suppose that you want to generate a list of numbers between 0 to 100. It will be a daunting task to write them down so you can use the for loop to perform this operation:

for x in range(5):
    print(x)

Output:

0
1
2
3
4

The range() function will generate many numbers starting from 0 as the default. The number of values you want to generate is specified inside the closed brackets. 

You can see it starts counting from 0 to the number 4, making it five numbers generated by the range(), which we’ve specified.

The range() function accepts many other arguments inside the closed brackets, and let’s see some of them:

for x in range(1, 5):
    print(x)

Output:

1
2
3
4

You can see that it starts printing from the number 1 as we’ve specified in the first option, but it stops before the five since this number is not included in the range() function.

From the previous two examples about the range() function, you can see the values are increasing by one only, but you have the option to choose whatever increment number you would like to use. Let us see an example:

for x in range(0, 41, 10):
    print(x)

Output:

0
10
20
30
40

You can see it increasing the values by ten since we’ve specified that in the third argument in the range() function.

Conclusion

Thanks for reading! Hopefully, you’ve learned something new in this python tutorial and this whole series of the python tutorial for beginners. We’ve discussed much stuff that will help you understand the basics and the syntax of writing a python code.

Scroll to Top