Home » Python for loop

Python for loop

Python is a powerful, general-purpose scripting language intended to be simple to understand and implement. It is free to access because it is open-source. This tutorial will teach us how to use Python for loops, one of the most basic looping instructions in Python programming.

Introduction to for Loop in Python

In Python, the for loop is often used to iterate over iterable objects such as lists, tuples, or strings. Traversal is the process of iterating across a series. If we have a section of code that we would like to repeat a certain number of times, we employ for loops. The for-loop is usually used on an iterable object such as a list or the in-built range function. The for statement in Python traverses through the elements of a series, running the block of code each time. The for statement is in opposition to the “while” loop, which is employed whenever a condition requires to be verified each repetition or when a piece of code is to be repeated indefinitely.

Syntax of for Loop

On each iteration, the value is the parameter that gets the element’s value within the iterable sequence. If an expression statement is present in a sequence, it is processed first. The iterating variable iterating_variable is then allocated to the first element in the sequence. After that, the intended block is run. The statement block is performed until the whole sequence is completed, and each element in the sequence is allocated to iterating_variable. The for loop’s material is distinguished from the rest of the program using indentation.

Example of Python for Loop

Code

Output:

The sum of squares is:  774  

The range() Function

Because the “range” function appears so frequently in for loops, we might mistakenly believe the range is a component of the syntax of for loop. It isn’t: it’s a Python built-in method that provides a series that follows a specified pattern (usually serial integers), fulfilling the criteria of giving a series for the for expression to run over. There is no necessity to count because for can act straight on sequences most of the time. If they’re coming from some other language with distinctive loop syntax, this is a frequent novice construct:

Code

Output:

[3, 5, 6, 8, 4, 5, 7, 8, 10, 6]  

Iterating by Using Index of Sequence

Another method of iterating through every item is to use an index offset within the sequence. Here’s a simple illustration:

Code

Output:

The sum of squares is:  774  

The len() built-in method that returns the total number of items in the list or tuple and the built-in function range(), which returns the exact sequence to iterate over, came in handy here.

Using else Statement with for Loop

Python allows you to connect an else expression with a loop expression.

When the else clause is combined with a for loop, it is performed after the circuit has finished iterating over the list.

The following instance shows how to use an otherwise statement in conjunction with a for expression to find students’ marks from the record.

Code

Output:

Marks of Itika are:  90  Marks of Parker are:  There is no student of name Parker in the records  

Nested Loops

If we have a piece of script that we want to run a number of times and then another piece of script inside that script that we want to run B number of times, we employ a “nested loop.” When working with an iterable in the lists, these are widely utilized in Python.

Code

Output:

0 2 4 5 6 7 8 8 9 10  

You may also like