For Loop in Python
For Loop in Python
Shiksha Online
Shiksha Online
Updated on Dec 29, 2023 16:37 IST
for loop in python is used to iterate over a sequence or an iterable object (such as a
list, tuple, or string). In this article, we will discuss 18 different examples of python for
loop.
For loops in Python is designed to repeatedly execute the code block while iterating
over a sequence or an iterable object such as list, tuple, dictionary, sets. In this
article, we will briefly discuss for loop in Python with different examples.
Content
Example 2: Python program to print all the even numbers within the given range.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Example 3: Python program to calculate the sum of all numbers f rom 1 to a given number.
Example 4: Python program to calculate the sum of all the odd numbers within the given
range.
Example 9: Python program that accepts a word f rom the user and reverses it.
Example 11: Python program to count the number of even and odd numbers f rom a series
of numbers.
Example 12: Python program to display all numbers within a range except the prime
numbers.
Example 13: Python program to get the Fibonacci series between 0 to 50.
Example 15: Python program that accepts a string and calculates the number of digits and
letters.
Example 16: Write a Python program that iterates the integers f rom 1 to 25.
Example 17: Python program to check the validity of password input by users.
Example 18: Python program to convert the month name to a number of days.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code
# between 0 to 10
# there are 11 numbers
# therefore, we set the value
# of n to 11
n = 11
Output
1
2
3
4
5
6
7
8
9
10
Acquire in-depth knowledge of Networking, Hardware & Security. Enroll in our top
programmes and online courses f rom the best colleges in India today to take the next step in
your career!
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Example 2: Pyt hon program t o print all t he even numbers wit hin t he given
range.
Copy code
f or i in range(given_range):
# if number is divisble by 2
# then it's even
if i%2==0:
Output
0
2
4
6
8
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code
Output
55
Example 4: Pyt hon program t o calculat e t he sum of all t he odd numbers wit hin
t he given range.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code
f or i in range(given_range):
# if i is odd, add it
# to the sum variable
if i%2!=0:
sum+=i
Output
25
Example 5: Pyt hon program t o print a mult iplicat ion t able of a given number
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code
f or i in range(11):
print (given_number," x",i," =",5*i)
Output
5x0=0
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Example 6: Pyt hon program t o display numbers f rom a list using a f or loop.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code
Output
1
2
4
6
88
125
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code
f or i in given_number:
count += 1
Output
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Pyt hon assert (): A Beginner Guide t o Underst anding and…
Using Keyword
In this article, we will learn ho w, and when to use the assert() statement in
pytho n with the help o f examples. Later in the article we will also discuss the...re ad m o re
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code
# given string
given_st ring = "madam"
Output
Example 9: Pyt hon program t hat accept s a word f rom t he user and reverses it .
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code
Input
Naukri
Output
irkuaN
Example 10: Pyt hon program t o check if a given number is an Armst rong
number
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Output
Example 11: Pyt hon program t o count t he number of even and odd numbers
f rom a series of numbers.
Copy code
Output
1 is an odd number.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
3 is an odd number.
5 is an odd number.
6 is an even number.
99 is an odd number.
134 is an even number.
55 is an odd number.
Example 12: Pyt hon program t o display all numbers wit hin a range except t he
prime numbers.
Copy code
# flag to track
# if no. is prime or not
# initially assume all numbers are
# non prime
f lag = False
# condition to check if a
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
# condition to check if a
# number is prime or not
if n % i == 0:
f lag = T rue
ret urn f lag
Output
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Also Read: Python Sets Practice Program f or Beginners
Example 13: Pyt hon program t o get t he Fibonacci series bet ween 0 t o 50.
Copy code
Output
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
2
3
5
8
13
21
34
Example 14: Pyt hon program t o f ind t he f act orial of a given number.
Copy code
# given number
given_number= 5
# since 1 is a factor
# of all number
# set the factorial to 1
f act orial = 1
print ("T he f act orial of ", given_number, " is ", f act orial)
Output
Example 15: Pyt hon program t hat accept s a st ring and calculat es t he number
of digit s and let t ers.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code
print (" T he input st ring",user_input , "has", let t ers, "let t ers and", digit s,"digit s.")
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Input
Naukri1234
Output
Example 16: Writ e a Pyt hon program t hat it erat es t he int egers f rom 1 t o 25.
Copy code
# given range
given_range = 25
# if no. is divisible by 4
# print fizz and no by 5
if i % 4 == 0 and i%5!=0:
print ("f izz")
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
# if no. is divisible by 5
# print buzz and not by 4
if i % 5 == 0 and i % 4!= 0:
print ("buzz")
else:
Output
fizzbuzz
1
2
3
fizz
buzz
6
7
fizz
9
buzz
11
fizz
13
14
buzz
fizz
17
18
19
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
fizzbuzz
21
22
23
fizz
buzz
Example 17: Pyt hon program t o check t he validit y of password input by users.
Copy code
has_valid_lengt h = T rue
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
# check if there are lowercase alphabets
if (i.islower()):
has_lower_case = T rue
Input
Naukri12345@
Output
Naukri12345@
Example 18: Pyt hon program t o convert t he mont h name t o a number of days.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code
Output
T op T rending Articles:
Data Analyst Interview Questions | Data Science Interview Questions | Machine Learning
Applications | Big Data vs Machine Learning | Data Scientist vs Data Analyst | How to Become
a Data Analyst | Data Science vs. Big Data vs. Data Analytics | What is Data Science | What is
a Data Scientist | What is Data Analyst
FAQs
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
What is a f or loop in Python?
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.