0% found this document useful (0 votes)
3 views11 pages

For Loop in Python

The document provides multiple examples of using for loops in Python to perform various tasks such as printing natural numbers, calculating sums, checking for palindromes, and generating Fibonacci series. Each example includes code snippets and explanations of the logic behind the implementation. The document serves as a practical guide for understanding and applying for loops in different programming scenarios.

Uploaded by

Sourav Chhetry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

For Loop in Python

The document provides multiple examples of using for loops in Python to perform various tasks such as printing natural numbers, calculating sums, checking for palindromes, and generating Fibonacci series. Each example includes code snippets and explanations of the logic behind the implementation. The document serves as a practical guide for understanding and applying for loops in different programming scenarios.

Uploaded by

Sourav Chhetry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

For Loop in Python (Practice Problem)

Example 1: Print the first 10 natural numbers using for


loop

# between 0 to 10
# there are 11 numbers
# therefore, we set the value
# of n to 11
n = 11

# since for loop starts with


# the zero indexes we need to skip it and
# start the loop from the first index
for i in range(1,n):
print(i)

Example 2: Python program to print all the even numbers


within the given range.

# if the given range is 10


given_range = 10

for i in range(given_range):

# if number is divisble by 2
# then it's even
if i%2==0:

# if above condition is true


# print the number
print(i)
Example 3: Python program to calculate the sum of all
numbers from 1 to a given number.

# if the given number is 10


given_number = 10

# set up a variable to store the sum


# with initial value of 0
sum = 0

# since we want to include the number 10 in the sum


# increment given number by 1 in the for loop
for i in range(1,given_number+1):
sum+=i

# print the total sum at the end


print(sum)

Example 4: Python program to calculate the sum of all the


odd numbers within the given range.

# if the given range is 10


given_range = 10

# set up a variable to store the sum


# with initial value of 0
sum = 0

for i in range(given_range):

# if i is odd, add it
# to the sum variable
if i%2!=0:
sum+=i

# print the total sum at the end


print(sum)

Example 5: Python program to print a multiplication table


of a given number

# if the given range is 10


given_number = 5

for i in range(11):
print (given_number," x",i," =",5*i)

Example 6: Python program to display numbers from a list


using a for loop.

# if the below list is given


list = [1,2,4,6,88,125]
for i in list:
print(i)

Example 7: Python program to count the total number of


digits in a number.

# if the given number is 129475


given_number = 129475

# since we cannot iterate over an integer


# in python, we need to convert the
# integer into string first using the
# str() function
given_number = str(given_number)

# declare a variable to store


# the count of digits in the
# given number with value 0
count=0

for i in given_number:
count += 1

# print the total count at the end


print(count)

Example 8: Python program to check if the given string is


a palindrome.

# given string
given_string = "madam"

# an empty string variable to store


# the given string in reverse
reverse_string = ""

# iterate through the given string


# and append each element of the given string
# to the reverse_string variable
for i in given_string:
reverse_string = i + reverse_string

# if given_string matches the reverse_srting exactly


# the given string is a palindrome
if(given_string == reverse_string):
print("The string", given_string,"is a Palindrome.")

# else the given string is not a palindrome


else:
print("The string",given_string,"is NOT a Palindrome.")

Example 9: Python program that accepts a word from the


user and reverses it.

# input string from user


given_string = input()

# an empty string variable to store


# the given string in reverse
reverse_string = ""

# iterate through the given string


# and append each element of the given string
# to the reverse_string variable
for i in given_string:
reverse_string = i + reverse_string

# print the reverse_string variable


print(reverse_string)
Example 10: Python program to check if a given number is
an Armstrong number

# the given number


given_number = 153

# convert given number to string


# so that we can iterate through it
given_number = str(given_number)

# store the lenght of the string for future use


string_length = len(given_number)

# initialize a sum variable with


# 0 value to store the sum of the product of
# each digit
sum = 0

# iterate through the given string


for i in given_number:
sum += int(i)**string_length

# if the sum matches the given string


# its an amstrong number
if sum == int(given_number):
print("The given number",given_number,"is an Amstrong
number.")
# if the sum do not match with the given string
# its an amstrong number
else:
print("The given number",given_number,"is Not an Amstrong
number.")

Example 11: Python program to count the number of even


and odd numbers from a series of numbers.

# given list of numbers


num_list = [1,3,5,6,99,134,55]

# iterate through the list elemets


# using for loop
for i in num_list:

# if divided by 2, all even


# number leave a remainder of 0
if i%2==0:
print(i,"is an even number.")

# if remainder is not zero


# then it's an odd number
else:
print(i,"is an odd number.")
Example 12: Python program to display all numbers within
a range except the prime numbers.
# import the math library
import math

# function to print all


# non-primes in a range
def is_not_prime(n):

# flag to track
# if no. is prime or not
# initially assume all numbers are
# non prime
flag = False

# iterate in the given range


# using for loop starting from 2
# as 0 & 1 are neither prime
# nor composite
for i in range(2, int(math.sqrt(n)) + 1):

# condition to check if a
# number is prime or not
if n % i == 0:
flag = True
return flag
# lower bound of the range

# upper bound of the range


range_ends = 30
print("Non-prime numbers between",range_starts,"and",
range_ends,"are:")

for number in filter(is_not_prime, range(range_starts,


range_ends)):
print(number)

Example 13: Python program to get the Fibonacci series


between 0 to 50.

# given upper bound


num = 50

# initial values in the series


first_value,second_value = 0, 1

# iterate in the given range


# of numbers
for n in range(0, num):

# if no. is less than 1


# move to next number
if(n <= 1):
next = n

# if number is within range


# execute the below code block
if nextnum:
break
# print each element that
# satisfies all the above conditions
print(next)
Example 14: Python program to find the factorial of a
given number

# given number
given_number= 5

# since 1 is a factor
# of all number
# set the factorial to 1
factorial = 1

# iterate till the given number


for i in range(1, given_number + 1):
factorial = factorial * i

print("The factorial of ", given_number, " is ", factorial)

Example 15: Python program that accepts a string and


calculates the number of digits and letters.

# take string input from user


user_input = input()

# declare 2 variable to store


# letters and digits
digits = 0
letters = 0

# iterate through the input string


for i in user_input:

# check if the character


# is a digit using
# the isdigit() method
if i.isdigit():

# if true, increment the value


# of digits variable by 1
digits=digits+1

# check if the character


# is an alphabet using
# the isalpha() method
elif i.isalpha():
# if true, increment the value
# of letters variable by 1
letters=letters+1

print(" The input string",user_input, "has", letters, "letters and",


digits,"digits.")

You might also like