Previously we were discussing branching statements. Now, we will discuss the loop statements in python and see the different methods and their examples.
Let's begin,
FOR LOOP
- For is a very easy and useful statement.
- It is a single line statement, but all the conditions are implemented.
Why For Loop
The for loop simplifies the complex problems into simple ones. It enables us to adjust the flow of the program so that instead of writing the same code over and over, we can repeat similar code a limited number of times. For instance, in the event that we need to print the initial 10 common numbers, instead of using the print statement 10 times, we can print inside a loop that runs up to 10 iterations.
Benefits of Loops
- For loop provides code re-usability.
- We do not need to write the same code again and again.
- We can traverse over the elements of data structures (array or linked lists).
SYNTAX
FLOW CHART
For loop simple example
Input:
for i in "python":
print(i)
Output:
p
y
t
h
o
n
For loop using List Operations
Input:
Directions= ["East", "West", "North", "South"]
for i in Directions:
print(i)
Output:
East
West
North
South
For loop using Tuple Operations
Input:
Numbers= ("One", "Two", "Three")
for i in Numbers:
print(i)
Output:
One
Two
Three
Range Function
Input:
for i in range(10):
print(i)
output:
0
1
2
3
4
5
6
7
8
9
Range function set the starting point
Input:
for i in range(5,10):
print(i)
Output:
5
6
7
8
9
len() function using for
Input:
a="hello"
for i in range(len(a)):
print(i)
Output:
0
1
2
3
4
Increment the sequence with 2
input:
for i in range(1,10,2):
print(i)
output:
1
3
5
7
9
Increment the sequence with 5
input:
for i in range(0,30,5):
print(i)
Output:
0
5
10
15
20
25
Multiplication Tables Using For
Input:
num = int(input("Enter the multiplication table of? "))
for i in range(1,21):
print(i,'x',num,'=',num*i)
Output:
enter the multiplication table of? 5
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
Sum of first n numbers using for loop
Input:
total=0
for i in range(101):
total=total+i
print("Total of first 100 numbers is:",total)
Output:
Total of first 100 numbers is: 5050
Printing the sentence using for loop with space in between each letter
Input:
sentence="You are Amazing"
for letter in sentence:
print(letter,end=' ')
Output:
Y o u a r e A m a z i n g
Printing the number of sentences
Input:
noOfsentences=0
sentence=str(input("Enter the paragraph:"))
for letter in sentence:
if letter=='.':
noOfsentences +=1
print("No.of sentences:",noOfsentences)
Output:
Enter the paragraph:Python is important software that can be used in lots of businesses,
including companies, industries, and manufacturing units. The person who knows the basic
level can won't have any difficulties with this type of coding. You don't need to know
other coding languages to learn Python. Anyone can learn this program.
No.of sentences: 4
Printing the alphabets A to Z
Input:
for letter in range(ord('A'),ord('Z')+1):
print(chr(letter), end=" ")
Output:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Printing alphabets using ASCII Values
Input:
for letter in range(65,91):
print(chr(letter), end=" ")
Output:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Changing the value in a list using index (for loop)
Input:
alpha=['A','B','C','D']
numeric=[1,2,3,4]
both=[alpha,numeric]
print(both)
for i in range(len(both)):
for j in range(len(both[i])):
if i==1 and j==2:
both[i][j]='insert'
print(both)
Output:
[['A', 'B', 'C', 'D'], [1, 2, 3, 4]]
[['A', 'B', 'C', 'D'], [1, 2, 'insert', 4]]
Nested Loop using for
- A nested loop is a loop inside a loop in the values.
- The "inside loop" will be executed one time for every iteration of the "outside loop"
Input:
inputs = ["red", "big", "tasty"]
fruit_names = ["Sky", "banana", "cherry"]
for i in inputs:
for j in fruit_names:
print(i, j)
Output:
red Sky
red banana
red cherry
big Sky
big banana
big cherry
tasty Sky
tasty banana
tasty cherry
Now, we have learned For loop operations in python. I have provided multiple examples of for loops and various methods of using for loop operations. In the next article, we will discuss the while loop operations.