Python

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.

Abhishek

Share
Published by
Abhishek

Recent Posts

5G and 6G Technology Advancements: The Future of Connectivity

5G and 6G Technology AdvancementsWhat is 5G, and How Has It Transformed Connectivity?1. Understanding 5G…

3 days ago

HTML: The Evolution and Power of Unleashed Web Language and Modern too

IntroductionInitial StagesEvolutionChallenging Other LanguagesCurrent TrendsAI and HTMLConclusion Introduction HTML, or HyperText Markup Language, is the…

3 months ago

Increase in 80C in Budget 2024 ?

Increase in 80CDemands and Discussions: Increase in 80C Section 80C of the Income Tax Act…

4 months ago

ChatGPT 4o Unleashing the Power of GPT A Comprehensive Guide

IntroductionWhat is ChatGPT-4?Key Features of ChatGPT-4Enhanced Natural Language UnderstandingImproved Response GenerationVersatilityApplications of ChatGPT-4Customer SupportContent CreationEducational…

6 months ago

APJ Abdul Kalam Biography: A Tribute to India’s Powerful Missile Man

APJ Abdul Kalam Biography :Childhood :Academics :Professional Career :Achievements : APJ Abdul Kalam Biography :…

9 months ago

Srinivasa Ramanujan Biography: Exploring the Genius

We value your feedback! Please share your thoughts on this blog on Srinivasa Ramanujan Biography…

9 months ago

This website uses cookies.