AS we already covered the conditional statement or decision-making statement in Python now we are to Python Loops. How many types, how to use loops in python with examples.
Let’s go…
Table of Content: For Loop for loop with else While Loops While loop with else The range() function in Python Break Statement Continue Statement Nested Loop |
Python Loops:
In the python, there are two types of loops.
- For loop
- While Loop
How to use For Loop?
The for loop in Python is used to repeat over a sequence (list, tuple, string) or other reiterable objects. Iterating or repeating over a sequence is called traversal.
Syntax:
for v in sequence:
statements
Example of Prime Number by using for loop:
prime_no = [2, 3, 5, 7, 9]
for p in prime_no:
print(p)
for loop with else in Python loops
We can also use For loop with an optional else block as well. The else part is executed if the object in the sequence used in for loop is exhausted.
Example:
ds = [0, 1, 5, 8]
for i in ds:
print(i)
else:
print("No items is left.")
Output:
While Loops in Python
While loop in python used to iterate( repeat) over a block of code as long as the condition is true, we can use this when we don’t know the number of times to repetitions before.
Syntax:
while conditions:
statements
Example:
# Program to add natural
num = int(input("Enter n: "))
# initializing sum and counter
sum = 0
i = 1
while i <= num:
sum = sum + i
i = i+1
print("The sum is", sum)
Output:
While loop with else:
Like for loop with else, we can also while loop with else statement. For example:
count = 0
while count < 3:
print("Inside the loop")
count = count + 1
else:
print("Inside else")
The range() function in Python:
The Python range() function uses to generates the integer numbers between the given start number to the stop number, i.e., It returns the sequence of the numbers.
For example, if we passed a number range(11), it will generate the numbers from 0 to 10.
Syntax:
range(start, stop, step size)
- The start is the initial number.
- The stop is the last number where it will stop finding.
- step size is optional, where the step size uses to skip the specific numbers from the iteration. The default step size is 1.
Example:
for x in range(11):
print(x,end = ' ')
Output will be like this:- 0 1 2 3 4 5 6 7 8 9 10
Len() function in python uses to returns the number of items in an object.
i = len("Hello")
print(i)
Output:
The break and continue statement:
Break Statement:
The break is used to abort the current execution of the running program, and control will go to the next line after the loop. The break statement is the keyword of Python, which uses to exit from a loop.
In the nested loop, the break statement breaks the inner loop first, and then it proceeds to the outer loops. Example:
list =[1,2,3,4,5]
count = 0;
for v in list:
if v == 5:
print("result matched")
count = count + 1;
break
print("found at",count," the location");
Continue Statement:
Break statement breaks the line whereas the continue statement is used to skip the current block, and return to the “for” or “while” statement.
As for examples:
# Prints out only odd numbers in the range 10
for x in range(10):
if x % 2 == 0:
continue
print(x)
Nested Loop
Like nested if..else statements, we can also use nested Loop in our program.
Example-1 Nested for loop:
# User input for number of rows
r = int(input("Enter the rows:"))
for i in range(0,r+1):
for j in range(i):
print("*",end = '')
print()
Output:
Must Read:
In the above, we have discussed the Python loops, their types, how to use, range() function, also we have a present break and continue statement of python. In the coming one, we will discuss the LIST, Tuple, and dictionary.