Beginners Tutorial For loops in Python| Practical Example | Happy Learning

In this blog on “Python For Loops”, you will get details of everything you need to know about using for loops in Python with the help of examples.

Moreover, after reading the entire blog, you will learn the following:

What is a For Loop?

A for loop used in a programming language helps a method, a statement, or a set of statements iterate over a given number of times. You can use a list, tuple, dictionary, set, frozenset, or string to execute a for loop. Moreover, Python for loops are similar to the looping statements used in other programming languages. It can iterate using various types of sequences as listed above. It is one of the common features found in most of the languages including object-oriented programming languages.

Syntax:

list = [ "Welcome", "to", "Python", "Programming"]

for i in list:

print(i)

Output:

Welcome
to
Python
Programming

Further, let us try to understand why we use for loop.

Why do we use for loop?

To understand why do we use for loop, let us look at an example. Suppose you visited an ATM to withdraw money. For this, you have to put in your ATM card, type the password, and then follow the instructions. Further, when you enter the amount, for example, Rs. 5800, the machine will break the amount as Rs. 5500, Rs. 200, and Rs. 100 for dispensing the notes.

Moreover, note that the distribution may vary from machine to machine.

Therefore, after the distribution of the amount, the machine will calculate the number of notes it has to dispense by dividing 5500 by 500, 200 by 200, and 100 by 100 as it has to dispense the corresponding notes of Rs 500, Rs 200, and Rs 100. Furthermore, three different for loops will iterate one by one to generate eleven Rs 500 notes and one note each of Rs 200 and Rs 100, yielding a total of Rs 5800.

I hope you are clear on the concept of the for loop used in real-life applications.

Moreover, let us have a glance at the types of Python for loops.

Types of Python For Loops

There are two types of for loops in Python and they are:

Nested for loops: You can nest one for loop inside another for loop.

In addition, each iteration of the outer for loop the inner loop runs for a specific number of times as per the given condition.

Example:

for(i=1, i<6,i++):

      for(j=0,j<i,j++):

              printf("*")

Else Statement in for loop: Python enables one to use the else declaration with the for loop, which can only be performed until all iterations are exhausted.

In addition, note that if there is a break statement, the else statement will not be executed.

Example:

for (i=0,i<n,i++):

       print(i)

else

       print("Successfully executed for loop in Python")

Till now we have learned about the types of python for loops.

Further, let us see the use of the range() function in for loop.

Range function in for loop

The range() function is used in Python for loops to generate a sequence of numbers by giving a specific range as a parameter inside the range() function.

Key points of the range() function:

  1. By default, it starts from 0 and increments by 1.
  2. You can increment the starting point, ending point, and increment size.
  3. You can use a list with a range() function to output all the elements in the form of a list.

Example:

for i in range(5):

         print("Welcome to Python for loops")

Output:

Welcome to Python for loops

Welcome to Python for loops

Welcome to Python for loops

Welcome to Python for loops

Welcome to Python for loops

Likewise, implement the range() function using start, stop, and the gap between iterations.

for i in range(1,10,2):

         print("Welcome to for loops in Python")

Output:

Welcome to for loops in Python

Welcome to for loops in Python

Welcome to for loops in Python

Welcome to for loops in Python

Welcome to for loops in Python

Furthermore, let us learn about implementing for loops using specific data types.

Implementing Python for loops using the following data types

Below are the different data types using which we will implement Python for loops.

1. Strings

Every programming language consists of character data types. Further, Strings are collections of characters. Also, strings in Python programming are declared by enclosing them in single or double quotes. Below is the program that demonstrates the implementation of for loops using strings.

Program:

S="for loops"

for x in S:

        Print(i)

Output:

f

o

r

l

o

o

p

Secondly, you will see the implementation on the dictionary and for loops.

2. Dictionary

Dictionaries in Python help us to store data in the form of key:value pairs. Below is the program that implements for loops using the dictionary:

Program:

dictionary= dict()

dictionary["Value1"]= 100

dictionary["Value2"]= 200

for x in dictionary :

    print("%s  %d" %(x, dictionary[x]))

Output:

Value1    100

Value2    200

Moreover, let us see how to implement lists and for loops.

3. List (Lists are mutable)

Lists data type in Python can have multiple values as shown in the below program while demonstrating Python for loops:

Program:

list=["Welcome","to","Thought of AI"]

for x in list:

          print(x)

Output:

Welcome

to

Thought of AI

In addition to lists, you can also use tuples with python for loops.

4. Tuples (Tuples are mutable)

Unlike lists, tuples are a group of objects stored in a single variable that is immutable. It means that the values inside a tuple cannot be changed. Here is a program implementing for loops using tuples:

Program:

Tuples=("Welcome","to","Thought of AI")

for x in Tuple:

         print(x)

Output:

Welcome

to

Thought of AI

Furthermore, you will see the use of sets with for loops.

5. Set

A set is an unordered collection of multiple objects stored in a single variable. Now, let us implement loops using sets.

Program:

sets=set("Unique")

for x in sets:

         print(x)

Output:

U

i

e

q

n

u

Finally, you have learned a lot about python for loops. But without practice, you may fall short of remembering the concepts for a long time. So, below are two problem statements that you can try out yourself.

Problem Statement

  1. Using Python for loops, write a program to create the pattern of numbers shown below:

5   4   3   2   1 

4   3   2   1 

3   2   1

2   1

1

Secondly, the below program is similar to the 1st program. The only difference is you have to print ‘@’ instead of printing numbers.

2. Using for loops in Python, create a pattern of numbers as given below:

@

@    @

@    @    @

@    @    @    @

@    @    @    @    @

Finally, you have gone through a lot of concepts related to python for loops.

To conclude, I would suggest practicing programs of Python because it will help you build logic for programs. In addition, practicing will give you a sharp edge in building Python applications.

This was all about for loops in Python. I hope this article was helpful to you. Also, you can check the Python’s official documentation – Python Developer’s Guide to get more knowledge.

Stay tuned to thoughtofai.com for more informative blogs!

If you want to learn Data Science, you can visit my blog to know, How to learn Data Science.

1 thought on “Beginners Tutorial For loops in Python| Practical Example | Happy Learning

Comments are closed.