Dark Mode
Image

Python for loop

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

 for value in sequence:  
 
   {loop body}   

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

# Code to find the sum of squares of each element of the list using for loop    

# creating the list of numbers  

numbers = [3523651298]  

# initializing a variable that will store the sum  

sum_ = 0  

# using for loop to iterate over the list  

for num in numbers:  

sum_ = sum_ + num ** 2   

print("The sum of squares is: ", sum_)  

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

 my_list = [35684]  
 
for iter_var in range( len( my_list ) ):  
 
   my_list.append(my_list[iter_var] + 2)  
 
print( my_list )  

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

 # Code to find the sum of squares of each element of the list using for loop    
# creating the list of numbers  
numbers = [3523651298]  
  # initializing a variable that will store the sum  
sum_ = 0  
  # using for loop to iterate over list  
for num in range( len(numbers) ):       
sum_ = sum_ + numbers[num] ** 2  
print("The sum of squares is: ", sum_)  

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.

Comment / Reply From