Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6
For Loop python
A for loop in Python is used to iterate over a
sequence (like a list, tuple, string, or range) and execute a block of code once for each item in the sequence. It's a fundamental control flow structure in Python and is often used when you know how many times you want to loop through a block of code.
Lets go through The Syntax
for item in sequence:
for keyword: This keyword is used to initiate the
loop. It signals the start of a loop that will iterate over a sequence of items.
item: This is a variable name chosen by the
programmer to represent the current value of each item in the sequence during each iteration of the loop. In each iteration, the item variable will hold the value of the current item being processed. sequence: This refers to the collection of items over which you want to iterate. It could be a list, tuple, string, range, or any other iterable object in Python.
Colon (:): At the end of the for statement, you
use a colon (:) to indicate the beginning of the code block that will be executed in each iteration of the loop. It's a required part of Python syntax for starting a code block.
Lets go through the Example :
for i in range(1, 11): print(i)
for i in range(1, 11):
This line initiates a for loop. Here's what each
part does: for: This keyword indicates the start of a loop. i: This is a variable name chosen by the programmer to represent the current value of each item in the sequence during each iteration of the loop. in: This keyword is used to specify the collection of items over which you want to iterate. range(1, 11): The range() function generates a sequence of numbers from the starting value (1 in this case) up to, but not including, the ending value (11 in this case). So, range(1, 11) generates numbers from 1 to 10. The loop will iterate over each of these numbers, and i will take on each value in turn. print(i)
This line is the body of the loop, indicated by the
indentation. It contains the instructions or operations that you want to perform for each item in the sequence. In this case, it simply prints the value of i to the console. During each iteration of the loop, i will take on the value of each number generated by the range() function, starting from 1 and ending with 10.
Add 1 to 11 number In python
total = 0
for num in range(1, 12):
# Add each number to the total total += num
print("The sum of numbers from 1 to 11
is:", total);
find out the Square of Each Element
for i in range(1, 11): square = i ** 2 print("Square of", i, "is:", square);
print reversed number :
for i in range(10, 0, -1): print(i)
lets Explain : We use a for loop to iterate through numbers from 10 to 1 using the range() function.
This line explains that a for loop is being used to
iterate through a sequence of numbers from 10 down to 1. The range() function takes three arguments: the starting value (10), the ending value (0, not inclusive), and the step size (-1).
Here, the explanation details the arguments
passed to the range() function, which defines the sequence of numbers. In this case, it starts from 10, ends at 0 (but does not include 0), and decrements by 1 in each step. So, the loop starts at 10, decrements by 1 in each iteration, and stops when it reaches 1.
This line clarifies how the loop behaves, starting
from 10 and decreasing by 1 in each iteration until it reaches 1. Inside the loop, we simply print the current value of i.
This part explains the action taken inside the
loop. It states that within each iteration, the current value of i is printed.