DridhOn

Loops Statement in Python

Share on facebook
Share on twitter
Share on pinterest
Share on linkedin
Share on whatsapp

What is Loops?

A loop statement in python allows users to accomplish an individual statement or group of statements multiple times.

In Python we have 2 types of loops:

  • FOR Loop
  • WHILE Loop
for loop in python
FOR Loop

The FOR Loop statement in the Python programming language has the ability to iterate over the elements of any sequence, such as a list or string.

FOR Loop exclusively Executes a series of statements multiple times and truncates the code that manages the loop variable.

If a sequence contains an expression as a list, it is examined first. Then, the first element in the sequence is assigned to repeating the variable input_var. Next, the statements block is executed. Every individual element in the list is allocated to input_var, and the statement(s) block is executed until the entire sequence is exhausted.

Syntax:

for (input var) in (sequence var):

statements(s)

Example:

Input:

l=[1,2,3,5,6,7]
for x in l:
print(x)

Output:

1
2
3
4
5
6
7

More Example:

 

# The list has four elements, indices start at 0 and end at 3

color_list = ["Red", "Blue", "Green", "Black"]
for c in color_list:
print(c)

print("------------------------------------------")
for a in range(4):
print(a)
print("------------------------------------------")
for a in range(2, 7):
print(a)
print("------------------------------------------")
for a in range(2, 19, 5):
print(a)

print("------------------------------------------")

numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declaring the tuple
count_odd = 0
count_even = 0
for x in numbers:
if x % 2 == 0:
print('--------', x)
count_even += 1

else:
count_odd += 1
print('==========', x)
print("Number of even numbers :", count_even)
print("Number of odd numbers :", count_odd)

print("-----------------------------------")
str1 = 'Python Learning'
for rakesh in str1:
print(rakesh)

print("-----------------------------------")

list1 = [1, 2, 3, 4, 5, 6]
for i in list1:
print(i)

print("-----------------------------------")

for i in [1, 2, 3, 4, 5, 6]:
print(i + 1)
if i > 5:
print('Value is greater than 5')
elif i > 6:
print('Value is greater than 6')
else:
print("Don't know the value")
print("Finish")

str1 = 'Python Learning'
for x in str1:
print(x)

print("-----------------------------------")
list1 = [1, 2, 3, 4, 5, 6]
for i in list1:
if i % 2 == 0:
print("Condition is true")
break # pass, continue
print('Finish')

print("-----------------------------------")

for x in range(1,10):
print(x)
print("-----------------------------------")
for x in range(10):
print(x)
print("-----------------------------------")
for x in range(10,50,2):
print(x)
print("-----------------------------------")
for x in range(10,-1,-2):
print(x)

for x in range(1, 10):
if x + 2 == 10:
print('value of x in if block is :: ', x)
break
else:
print('value of x in else block is :: ', x)
# print('do not continue')

print("-----------------------------------")
for x in "Python":
if x == 't':
continue
print(x)
print('end')
print("-----------------------------------")

for x in [10,12,20,25,30,35,40]:
print('value of x is :: ',x)
if x % 2 != 0:
pass
print('this is pass block')
else:
print('value of x in else block is ::',x)

# print("-----------------------------------")
for val in 'python':
if val == 'h':
pass
print('this is pass block')
print('current value of val is :: ', val)
print("bye")
while in python
WHILE Loop

A WHILE Loop in python has repeatedly executed a prey statement until a given condition statement is true.

Syntax:

while expression:

statement(s)

Example:

Input:

c=0
while c<3:
print('kiran')
c+=1

Output:

kiran
kiran
kiran

More Example:

 

a = 0
while a <= 12:
print('old value of a :', a)
a += 1
print('new value of a :', a)
print("------------------------------------------")

while 1 < 12:
print('Hi')

a1 = 0
while a1 < 12:
while a1 < 10:
print(a1)
a1 += 1
else:
print('exiting inner while loop')
a1 += 1
else:
print("Exiting outer While loop and executing else block")
print("------------------------------------------")

c = 0
while c < 10:
c += 1
if c % 2 == 0:
print('even number is ::', c)
elif c % 2 == 1:
print('elif block execution', c)
else:
print('If condition is failed , execution else block', c)
else:
print("No even number available")

print("------------------------------------------")
c = 0
while c < 10:
c += 1
if c % 2 == 0:
print('even number is ::', c)
else:
print("No even number available")

print("------------------------------------------")
x = 0
s = 0
while x < 10:
s = s + x
x = x + 1
else:
print('The sum of first 9 integers : ', s)

print("------------------------------------------")
x = 1
s = 0
while x < 10:
s = s + x
x = x + 1
print('value of x and s are :', x, s)
if x == 5:
break
else:
print('The sum of first 9 integers : ', s)
print('The sum of ', x, ' numbers is :', s)

Assignments for Practice:

#1. Take username and password from the user and validation against your email and password if password on count of 3 attempts
# Its shows account blocked.

#2. Write a program for a school which they have to find out result for students.

#3. Print l=['kiran','swati','bhoomi,'deepi'] show output 3 times separate.

#4. print pyramid *
# * *
# * * * trainagle, Left pyramid , right pyramid

Related Posts

python certification training

Loops Statement in Python

What is Loops? A loop statement in python allows users to accomplish an individual statement or group of statements multiple times. In Python we have 2 types of loops: FOR

Read More »
Scroll to Top