Loops in Python

While coding we need to use a block of code again and again. Its tedious to write the same code again and again. This will make the program lengthy and difficult to understand. To solve this issue loops are make. Loops are a block of code which can be run multiple times. There are 2 main types of loops. 1. While loop, 2. For loop.

Loop - User Interface & Gesture Icons

Syntax- while condition:

code

Q1 Print numbers from 1 to 100 and 100 to 1.

a=1
while a<100:
    a=a+1
    print(a)
b=100
while b>0:
    b=b-1
    print(b)

Q2:Print the table of a number given by the user.

n=int(input("Enter a number for finding the table:"))
i=1
while i<=10:
    print(n,"*",i,"=",n*i)
    i=i+1

Q:Print the elements of the following list using loop [1,4,9,16,25,36,49,64,81,100].

n=[1,4,9,16,25,36,49,64,81,100]
i=0
while i<len(n):
    print(n[i])
    i=i+1
print(n)

Q: Program to search an element in the list

n=[1,4,9,16,25,36,49,64,81,100]
i=0
while i<len(n):
    print(n[i])
    i=i+1
print(n)
Q: Program to search an element in the list

n=[1,4,9,16,25,36,49,64,81,100]
i=0
s=int(input("Enter the element you want to search in the list:"))

while i<len(n):
    print(n[i])
    i=i+1
    if n[i]==s:
        print("Found :) ")
        break
    else:
        print("No!")
print(n)

Break and Continue Statements Break: Used to terminate the loop when encountered. Continue: Terminates the execution in the current iteration and continues execution of the loop with the next iteration. eg-

n=[1,4,9,16,25,36,49,64,81,100]
i=0
s=int(input("Enter the element you want to search in the list:"))

while i<len(n):
    print(n[i])
    i=i+1
    if n[i]==s:
        print("Found :) ")
        break
    else:
        print("Nah!")
        continue
print(n)

For Loops-These loops are used for sequential traversal. For traversing list ,string, tuples etc. Syntax-

for el in list:
    print(el)

for el in list:
    print(el)
else:
    print("End")

Q. Program to print the following elements. [1,4,9,16,25,36,49,64,81,100]

nums=[1,4,9,16,25,36,49,64,81,100]

for el in nums:
    print(el)

Q.Search for a number x in a tuple.

nums=(1,4,9,16,25,36,49,64,81,100)
x=int(input("Enter a number"))

n=0
for el in nums:
    print(el)
    if(el==x):
        print("Number found at",n)
        n=n+1

Range-This function Range function returns a sequence of numbers,starting from 0 by default, and increments by 1 and stops before a specified number.

syntax: range(start?,stop,step?)

for i in range(5): #eg

print(i)

There are 3 ways to write range in Python: 1.range(10)

2.range(2,10) #starts from 2 and ends at 10

3.range(2,10,2) #starts from 2 with difference of 2.

Q. Print numbers 1 to 100 alternatively.

for i in range(1,101,2):
    print(i)

Q3 Print the multiplication table of a number n.

n=int(input("Enter a number:"))

for i in range(1,11):
    print(n*i)

Pass Statment-Pass is a null statment that does nothing. It is used as a place holder for future code.

for el in range(10):
    pass

Q.WAP to find the sum of first n numbers.(using while)

m=0
n=5
a=0
while m<=n:
    a=m+a
    m+=1

print(a)

Q Write a program to find the factorial for the 1st n natural number

n=5
sum=1
i=1
for i in range(1,n+1):
    sum*=i

print(sum)

Hope you learnt something in this blog. This was just an introduction to the topic. Feel free to explore. Don't forget to like and follow for more coding related blogs.