For Loop:
A for loop is used to iterate through a series (that is either a list, a tuple, a dictionary, a set, or a string).
This functions more like an iterator method in other object-oriented programming languages than the for keyword in other programming languages.
The for loop allows one to execute a series of statements once for each item in a list, tuple, set, and so on.
For Loop in Python
- For loop
- Iterating the string using for loop
- Using For loop and range() to print elements from 1 to 20
- Printing numbers in descending order using for loop and range() function
- Print elements in list using For Loop
- For Loop with else statement
1)For loop
A for loop in Python allows one to iterate over a set of statements several times. However, the number of times these statements are executed by a for loop is determined by a sequence.
To put it another way, a for loop in Python is useful for iterating over a sequence of elements.
Syntax:
for element in sequence: statement one statement two ....... ....... ..... ....... ...... statement n
The keyword “for” is followed by a variable, then the keyword “in,” then a sequence, and finally a colon. The suite of for loop is also known as the block of for loop after the for statement begins the suite of for loop, which is a group of statements with one indent stage.
The for loop will loop through all of the elements in a given sequence. It will allocate each element of the sequence to variable element and then execute the statements in-suite, i.e., statements in the block. These statements may now make use of the element variable, which includes a sequence element for that event.
2)Iterating the string using for loop
# given_string given_string = "BTechGeeks" # Traverse the string in for loop for character in given_string: print("Character = ", character)
Output:
Character = B Character = T Character = e Character = c Character = h Character = G Character = e Character = e Character = k Character = s
As a sequence, we used a string. Then, using a for loop, we looped through all of the characters in the string series.
3)Using For loop and range() to print elements from 1 to 20
The range() function returns a sequence of numbers from start to -1 . We can then iterate over each element of this sequence using the for loop.
Below is the implementation:
# using for loop and range to print elements from 1 to 20 for i in range(1, 21): print(i)
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
The following actions were performed by the for loop for each element of the sequence,
- It assigned the variable i to the element.
- It execute the lines in the block, i.e. the suite of for loops.
4)Printing numbers in descending order using for loop and range() function
The range() function returns a sequence of numbers with the specified range size from start to end -1.
range(20,0,-1)
# using for loop and range to print elements from 1 to 20 in descending order for i in range(20, 0, -1): print(i)
Output:
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
The following actions were performed by the for loop for each element of the sequence,
- It assigned the variable i to the element.
- It execute the lines in the block, i.e. the suite of for loops.
5)Print elements in list using For Loop
We can traverse the list using for loop as shown below.
Below is the implementation:
# given_list givenlist = ["hello", "this", "is", "BTechGeeks"] # using for loop to traverse the list for element in givenlist: # print the element print(element)
Output:
hello this is BTechGeeks
6)For Loop with else statement
We may have an optional else block in addition to a for loop.
When the for loop has finished executing the statements in the for block, it then executes the statements in the else block. It is important to note that statements in the else block can only be executed in the last position and only once.
Below is the implementation:
# given_list givenlist = ["hello", "this", "is", "BTechGeeks"] # using for loop to traverse the list for element in givenlist: # print the element print(element) # else block else: print("For loop is ended")
Output:
hello this is BTechGeeks For loop is ended
Related Programs:
- numpy where explained with examples
- for loop with 2 variables in c and java
- python while loop with examples
- python open a file using open with statement and benefits explained with examples
- python break keyword explained with examples
- python list comprehension vs generator expression explained with examples
- python iterator iterable and iteration explained with examples