For Loop in Python 10 Problems Solved

For Loop in Python 10 Problems Solved

For Loop in Python 10 Problems Solved

For Loop in Python 10 Problems Solved

Just because you understand the concepts in Python programming language doesn’t mean you can do complete Python programming. You need to improve your problem solving skills a lot by doing practical questions. So today I will show you 10 problems of Python programming language and solve those problems. In this blog I will show you 10 problems solved from for loop of Python programming language. This blog is very helpful for those who are beginner programmers who have just started learning Python language. If you find this blog helpful, please leave a comment below, and comment on any topic you want to blog about, you can go to my contact page and contact me by email. So let’s start the blog


1. Question:- Write a program to print numbers from 1 to 5 using a for loop.

Expected Output:-1 2 3 4 5 

				
					# Using a for loop to print numbers from 1 to 5
for i in range(1, 6):
    print(i)

				
			

Output:-

				
					1
2
3
4
5

				
			

 

2. Question:- Write a program to print the squares of numbers from 1 tp 5.

Expected Output:-1 4 9 16 25

				
					# Using a for loop to print the squares of numbers from 1 to 5
for i in range(1, 6):
    print(i ** 2)

				
			

Output:-

				
					1
4
9
16
25

				
			

 

3. Question:- Write a program to print all even numbers from 1 to 10.

Expected Output:- 2 4 6 8 10

				
					# Using a for loop to print even numbers from 1 to 10
for i in range(1, 11):
    if i % 2 == 0:
        print(i)

				
			

Output:-

				
					2
4
6
8
10

				
			

 

4. Question:- Write a program to calculate the sum of numbers from 1 to 10.

Expected Output:- sum = 55

				
					# Initialize a variable to store the sum
total_sum = 0

# Using a for loop to calculate the sum of numbers from 1 to 10
for i in range(1, 11):
    total_sum += i

# Print the total sum
print("sum =", total_sum)

				
			

Output:-

				
					sum = 55
				
			

 

5. Question:- Write a program to print the word “Python” in reverse using a for loop.

Expected Output:- nohtyP

				
					# The word to be reversed
word = "Python"

# Using a for loop to print the word in reverse
for i in range(len(word)-1, -1, -1):
    print(word[i], end="")

				
			

Output:-

				
					nohtyP

				
			

 

6. Question:- Write a program to count the number of vowels in the word “education”.

Expected Output:- 5

				
					# The word to check
word = "education"

# Vowels to check against
vowels = "aeiou"

# Initialize a counter for vowels
vowel_count = 0

# Loop through each character in the word
for char in word:
    if char in vowels:
        vowel_count += 1

# Print the result
print("Number of vowels in 'education' is:", vowel_count)

				
			

Output:-

				
					Number of vowels in 'education' is: 5

				
			

 

7. Question:- Write a program to print the first 10 terms of the Fibonacci sequence.

Expected Output:- 0 1 1 2 3 5 8 13 21 34

				
					# Number of terms to print
n = 10

# Initialize the first two terms of the Fibonacci sequence
a, b = 0, 1

# Print the first 10 terms of the Fibonacci sequence
for _ in range(n):
    print(a, end=" ")
    a, b = b, a + b  # Update a and b to the next terms

				
			

Output:-

				
					0 1 1 2 3 5 8 13 21 34

				
			

 

8. Question:- Write a program to calculate the factorial of a given number, such as 5.

Expected Output:- 120

				
					# Function to calculate the factorial of a number
def factorial(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

# Given number
number = 5

# Calculate and print the factorial
print(f"The factorial of {number} is: {factorial(number)}")

				
			

Output:-

				
					The factorial of 5 is: 120

				
			

 

9. Question:- Write a program to check if a given number, such as 7 is a prime number.

Expected Output:- 7 is a prime number

				
					# Function to check if a number is prime
def is_prime(n):
    # A number less than or equal to 1 is not prime
    if n <= 1:
        return False
    # Check if any number from 2 to the square root of n divides n
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

# Given number
number = 7

# Check if the number is prime
if is_prime(number):
    print(f"{number} is a prime number")
else:
    print(f"{number} is not a prime number")

				
			

Output:-

				
					7 is a prime number

				
			

 

10. Question:- Write a program to count occurrences of each character in the word “programming”.

Expected Output:- p: 1  r: 2  o: 1  g:  2  a: 1  m: 2  i: 1  n: 1  

				
					# The word to analyze
word = "programming"

# Create a dictionary to store character counts
char_count = {}

# Loop through each character in the word
for char in word:
    if char in char_count:
        # If the character is already in the dictionary, increment its count
        char_count[char] += 1
    else:
        # Otherwise, add the character to the dictionary with a count of 1
        char_count[char] = 1

# Print the character counts
for char, count in char_count.items():
    print(f"{char}: {count}")

				
			

Output:-

				
					p: 1
r: 2
o: 1
g: 2
a: 1
m: 2
i: 1
n: 1

				
			

Leave a Comment

Your email address will not be published. Required fields are marked *