Python For Loop

Python for loop concept is most useful in programming which saves a lot of time in writing unnecessary blocks of code rather than using the same code multiple times.

When you want to repeat a code execution in a loop to execute a functionality we can make use of python for loop

Specify the condition and and increment it to traverse through the loop.

A for loop in Python is used to iterate over a sequence of elements, such as a list, tuple, or string.

Python for loop usage :

A for loop is a type of loop that is commonly used in programming to repeat a block of code a certain number of times, or to iterate over a collection of items.

Here’s what each part of the for loop does:

initialization: This is where you set up the loop variable(s) and initialize them to a starting value. This step is optional, and you can use variables that have already been defined outside of the loop as well.

condition: This is a Boolean expression that is evaluated before each iteration of the loop. If the condition is true, the loop will continue to run; if it is false, the loop will terminate.

increment: This is the operation that is performed at the end of each iteration of the loop. It is used to update the loop variable(s) in some way, such as by incrementing or decrementing their values.

Python For Loop Video Tutorial :

In this video tutorial let us try to see the detailed implementation of python For Loop Functionality.

Let’s us see python for loop implementation

consider a variable x = “computer”

We will try to print each alphabet individually using for loop.

for i in x :
  print( x )

Output :

c
o
m
p
u
t
e
r

Now let us try to print a list of different data types.This is a very dynamic way of list utilisation.

x = ["computer",  'L',  1, True]

output :

computer
L
1
True

Range in Python For Loop :

Now we will try to see a different way of using python for loop with the help of range and try to print numbers from 1 to 10.

In Python, range() is a built-in function that generates a sequence of numbers. It is commonly used in for loops to iterate over a sequence of values.

for i in range( 10 )
print( i )

output :

0
1
2
3
4
5
6
7
8
9

We can specifically specify the range from a number to another number as below.

for i in range ( 1..11 ):
  print( i )

try to implement the above code and find output may refer to video tutorial specified.

Continue :

Now you can skip a certain number in the flow of numbers using continue.

if( i == 5 )
  continue

break :

Stop the loop when a certain number is detected using break keyword.

if( i == 7 )
  break

You can add a else condition when you want to know the end of the loop in this scenario only.

else :
print ( "End of the loop")

Nested For Loop :

Want to print for loop inside a for loop ?

x = [ 1, 2, 3 ]
y = [ a, b, c ]

now loop them and try to print i and j such that values are printed accordingly

for i in x :
   for j in y:
      print( i )

Try to run the above code or refer python for loop video tutorial.

If you have any query in this tutorial on python for loop do let us know in the comment section below. If you like this tutorial do like and share us for more interesting updates.

python for loop