19 May Python Loops
Loops execute a block of code and this code executes while the condition is true. In this lesson, we will learn about Python Loops such as while loop, for loop, etc.
while loop in Python
In while loop, the block of code executes only when the condition is true. It executes as long as the condition is true.
Syntax
Here’s the syntax:
1 2 3 4 |
while (condition is true): code |
Example
The following is an example showing the usage of while loop in Python,
1 2 3 4 5 6 7 8 9 10 |
a = 0 print ('Studyopedia: Never Stop Learning') #while loop while (a < 3): print ('Value = ', a) a = a + 1 |
Output
The following is the output,
for loop in Python
The for loop in Python is used when you can set how many times a statement is to be executed.
Syntax
Here’s the syntax,
1 2 3 4 |
for var in sequence: code |
Example
The following is an example showing the usage of for loop in Python,
1 2 3 4 5 6 7 |
print ('Studyopedia: Never Stop Learning') #for loop for w in 'Kevin': print ('Words in my name = ', w) |
Output
The following is the output,
In this lesson we learned about the types of loops in Python.
Python Tutorial (English)
Python Tutorial (Hindi)
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments