Latest posts

10/recent/ticker-posts

For Loop in Python Programming | Use of For Loop in Python | How to use for Loop in Python

So, in the last post of the Python Programming course, we have seen Looping statements of Python Programming. In that, we have learned about different types of loops in python.
Now in this post, we are going to learn about For loops of Python Programming in Detail.

What is For Loop ?

In python programming, a for loop is used a looping statement used to iterate a block of code in a given range or over a sequence of items.
It reduces the rewriting task at the time of programming.

The for loop in Python is used to iterate over a sequence of elements, such as a list or a tuple. It is a type of loop that is commonly used in Python programs, and it is very useful for processing data and performing repetitive tasks.

Syntax of for-loop in case of range :
for variable in range(start, end):
    # code block to be executed

Syntax of for-loop in case of sequence :
for variable in sequence:
    # code block to be executed

Use of range() function with for loop :

Firstly to use a range() function with for loop, we have to initialize a variable to iterate our code block in between the range.
Basically, the range() function with for loop is used to specify a range for iterations. It takes 3 arguments first for the start point, second for the endpoint, and third for the step to ignore and it is optional.

Example :
for i in range(1,11):
    print(i)

'''
output :
1
2
3
4
5
6
7
8
9
10
'''
So in this example, we have used a variable ' i ' to iterate our code block inside the range of 1 to 11, which means this for-loop iterates our block of code 10 times. Inside the block of the for-loop, we have printed the value of variable ' i '. Value of variable ' i ' is incremented at every iteration by 1, because we have defined a range between 1 to 11, so it will increment their value by 1 with every iteration.

Use of sequence with for loop :

This type of for-loops requires a data structure or a sequence to iterate the block of code inside this sequence. It is mostly used with Data structures like Lists, Tuples, and Dictionaries. By using it we can able to retrieve the data from the data structure to get it in the use.

It also requires a variable to catch the elements from the data structure. It will work according to indexing, starting from 0 to end.

One of the advantages of using a for loop in Python is that it makes it easy to process large amounts of data. For example, suppose we have a list of names, and we want to print each name with a greeting, then it will help us to reduce the rewriting work.

Example :
names = ['Alice', 'Bob', 'Charlie', 'Dave']

for i in names:
    print('Hello, ' + i + '!')


In this example, the for loop iterates over the "names" list, and each name in the list is assigned to the " i " variable. The "print" statement then prints a greeting to each name. So it is a basic example of for loop used with lists as well as tuples.

In addition to iterating over lists and ranges, for-loops can also be used to iterate over other types of data structures, such as dictionaries and sets. For example, suppose we have a dictionary of student grades, and we want to print each student's name and grade, then we have to declare 2 variables, one for Student Name and another for their Makrs.

Example :
grades = {'Alice': 90, 'Bob': 85, 'Charlie': 95, 'Dave': 80}

for name, grade in grades.items():
    print(name + ' scored : ' + str(grade))

'''
Output :

Alice scored : 90
Bob scored : 85
Charlie scored : 95
Dave scored : 80

'''


In this example, the for loop iterates over the "grades" dictionary, and each key-value pair in the dictionary is assigned to the "name" and "grade" variables. The "print" statement then prints each student's name and grade to the console.

In conclusion, the for loop is a powerful tool in Python that allows you to iterate over a sequence of elements. It can be used to process large amounts of data, iterate over ranges of numbers, and iterate over other types of data structures. By understanding how to use the for loop in Python, you can write more efficient and effective programs.

So this is a basic use of for-loop in python programming !

I hope it will help you to learn python more...!

Thank You...!!!




Also Visit


Post a Comment

0 Comments