Like while loop the for loop in python also allows you to repetition of statements according to your requirements.
In Python, the “for” loop is used to iterate over a sequence of elements.
It is a control structure that allows you to repeat a block of code for each element in a list, tuple, dictionary, set, or another iterable object.
The syntax for a “for” loop is as follows:
for item in sequence:
# Code block to be executed
The “item” variable represents each element in the sequence and the code block will be executed for each element in the sequence.
Here’s an example of how to use a “for” loop to print out each item in a list:
Code:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
In this code, the “for” loop iterates over the list of fruits and prints out each item.
The “for” loop can also be used with a range of numbers to execute a code block a specific number of times.
Code:
for i in range(5):
print(i)
Output:
0
1
2
3
4
In this code, the “for” loop runs according to a range from 0 to 4 and prints out each number.
So, here we are going to discuss some examples related to for loop.
How to calculate the total of
the integers from 0 through 1,000,000.
Using the range function and a for statement we can calculate the total of
the integers from 0 through 1,000,000.
Code:
total = 0
for number in range(1000001):
total = total + number
print(total)
Output:
500000500000
in this code, the “for” loop runs according to a given range from 0 to 1000001 and append numbers into the total.
Finally, the print statement print output is stored in total.
How to take a Factorial of any number in Python by For Loop.
Here is Python code to calculate the factorial of any number using a for loop:
Code:
n = int(input("Enter a number: "))
factorial = 1
if n < 0:
print("Factorial does not exist for negative numbers")
elif n == 0:
print("Factorial of 0 is 1")
else:
for i in range(1, n+1):
factorial *= i
print("Factorial of", n, "is", factorial)
Output:
Enter a number: 5
Factorial of 5 is 120
The user is prompted to input a number using the input() function and the value is converted to an integer using the int() function and stored in the variable n.
The factorial variable is initialized to 1.
If the user enters a negative number, the program prints a message that the factorial does not exist for negative numbers.
Enter a number: -20
Factorial does not exist for negative numbers
When we enter 0, the program prints a message that the factorial of 0 is 1.
Enter a number: 0
Factorial of 0 is 1
If the entered number is a positive integer, the program enters a for loop that iterates from 1 to n+1 (since the range function is exclusive of the upper limit).
During each iteration, the current value of i is multiplied to factorial. At the end of the loop, the final value of factorial is printed as the factorial of the entered number.
So, the “for” loop is a powerful control structure in Python that allows you to iterate over sequences of elements and execute a code block for each element. It’s a useful tool for automating repetitive tasks and processing large amounts of data.
So, in the next tutorial, we are going to learn about Loop Control Statements. So, make sure to subscribe to us to learn python in an easy way.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.