LOOPS IN PYTHON
Python language provides three types of loops, They are :
- while loop
- for loop
- nested loop
Loop : It is a sequence of statements that will be repeated continuously until a certain condition is met.
While Loop : It is used to execute a block of statements repeatedly till a given condition is True. It will check the condition before executing the loop.
Syntax :
1 2 |
while(Condition): statements |
Example :
1 2 3 4 5 6 7 8 9 10 11 12 |
1 counter = 1 2 while counter <= 5: 3 print(counter) 4 counter += 1 Output ------------- 1 2 3 4 5 |
For Loop : for loop in python is different from the for in other programming languages. In Python a for loop is used to iterate over a set of items, list of items or any sequence of items.
Example 1 :
1 2 3 4 5 6 7 8 9 |
1 Students = ["Lucy", "Ryan", "Chris"] 2 for i in Students: 3 print(i) Output ------------ Lucy Ryan Chris |
Example 2 :
1 2 3 4 5 6 |
for i in range(5): print(i, end=" ") Output -------------- 0 1 2 3 4 |
You might wonder what is the word range in the above example. Let me explain it.
Range function : Range function iterates over a piece of code a specific number of times.
It auto increments the number until the specific number of iterations are reached.
- range( 5 ) denotes the values from 0 to 4 excluding
- range( 2, 5 ) denotes the values starting from 2 to 4 excluding
- range( 3, 20, 2 ) denotes the values starting from 3 to 20 incrementing the sequence by 2 each
Example 1 :
1 2 3 4 5 6 |
for i in range(5): print(i, end=" ") Output ------------------ 0 1 2 3 4 |
Example 2 :
1 2 3 4 5 6 |
for i in range(2,5): print(i, end=" ") Output ----------------- 2 3 4 |
Example 3 :
1 2 3 4 5 6 |
for i in range (2,20, 2): print(i,end=" ") Output ---------------------- 2 4 6 8 10 12 14 16 18 |
Break Statement : Break Statement is used to terminate or stop the flow of execution of code before it reaches the stopping condition or before iteration of all the items are completed.
Example 1 :
1 2 3 4 5 6 7 8 |
for i in range(7): if i == 4: break print(i, end=" ") Output -------------------- 0 1 2 3 |
Example 2 :
1 2 3 4 5 6 7 8 9 10 |
1 counter = 1 2 while counter <= 6: 3 if counter == 4: 4 break 5 print(counter, end = " ") 6 counter += 1 Output -------------------- 1 2 3 |
Continue Statement : Continue Statement is used to stop the current iteration continuing from the next
Example 1 :
1 2 3 4 5 6 7 8 |
1 for i in range(7): 2 if i == 4: 3 continue 4 print(i, end=" ") Output -------------------- 0 1 2 3 5 6 |
Example 2 :
1 2 3 4 5 6 7 8 9 10 |
1 counter = 1 2 while counter <= 6: 3 if counter == 4: 4 continue 5 print(counter, end = " ") 6 counter += 1 Output --------------------- 1 2 3 |
Author : Prudhvi Teja
LinkedIn : http://linkedin.com/in/prudhvi-teja-nagabhyru-715052224
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTExperts is always active on social media platforms.
Facebook : https://www.facebook.com/ktexperts
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform