× About Us How it works Pricing Student Archive Subjects Blog Contact Us

Enrich your knowledge with our informative blogs

How do I use “for” loops in Python?

To keep a computer system performing useful work, we need repetition. Repetition is performed by looping back over the same code block again and again.

What is For Loop?

For loop is used to iterate over the sequence of elements that are often used when you have a code, which you want to repeat for “n” number of times. In short, it works like “for all elements in a list, do this.”

A for loop in Python is used for iterations or, say, iterating over the same sequence. It is less like the “for a keyword like used in other programming languages and works more like an iterator method than found in the Object-Oriented Programming languages.

Using a for loop in the Python code, we can execute the set of statements once for each item in a collection, tuple, or list.

For instance,

Print each color in a color list:

Colors = [“Red”, “Yellow”, “Black”, “Blue”]

For x in colors:

Print (x)

Here the “for loop” doesn’t require an indexing variable to put beforehand.

  • Looping through a string

Strings are also iterable objects that contain the characters’ sequence. For instance, let us consider the loop through the letters present in the word “banana.” The looping statement for this will be

For x in “banana”:

Print x

  • Using The Break Statement

By using the break statement, we can stop the ongoing look before it has looped through the entire item set. For instance,

You need to exit the loop when x is “banana”:

Fruits = [“orange”, “apple”, “banana”, “cherry”]

For x in fruits:

Print (x)

If x == “banana”:

Break

This will break the look once it encounters the fruit banana instead of searching for the whole item set.

Taking it another way

Exit the loop when “x” is banana, but this time break comes before the print:

Fruits = [“apple”, “orange”, “banana”, “cherry”]

For x in fruits:

If x == “banana”:

Break

Print (x)

  • Using The Continue statement

Using the continue statement, we can stop the loop’s current iteration and continue with the next one.

For instance,

Fruits = [“apple”, grapes”, banana”, “cherry”]

For x in fruits:

If x == “banana”:

Continue

Print (x)

The for loop is also applicable in the range function.

  • Using the range () function

We can use the range () function to loop through a code set a specified number of times. The range function returns the numbers sequence beginning from 0 by default and then further increments by one till some specified number.

For instance,

Using the range function:

For x in range (6):

Print (x)

Here if you execute the code, the range will move from 0 to 5, not 0 to 6.

  • Using Else in For loop

The use of the else keyword in a for loop indicates that a block of a code needs to be executed when the for loop is finished.

For instance,

Print all the numbers from 0 to 5 and print the message when the loop ends.

For this, we need to perform

For x in range (6):

Print (x)

Else:

Print (“Finally Finished!”)

Now this will iterate from range 0 to 5 and then prints the message “Finally Finished!”

 

Read More – Python Questions

View More – Useful links for Your Child’s Development 

Tel Guru
Tel Guru

Register For The Demo Class

[footer-form]