LOOPS IN PYTHON

Share via:
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 :

Example :

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 :

Example 2 :

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 :

Example 2 :

Example 3 :

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 :

Example 2 :

Continue Statement : Continue Statement is used to stop the current iteration continuing from the next
Example 1 :

Example 2 :

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

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Add Comment