Solutions to the First 10 Python Programming Exercises from ‘Top 30 Python Programming Exercises

Introduction
Welcome to the solutions blog for the “Top 30 Python Programming Exercises for Beginners to Advanced.” In this post, we will review the solutions for the first 10 beginner-level challenges. If you have not already read the original post, you can find it here: Top 30 Python Programming Exercises for Beginners to Advanced. Whether you are learning Python or preparing for interviews, these solutions will help you to enhance your coding logic and problem-solving skills. Let’s get started!
1. Check if a Number is Even or Odd
Problem: Write a Python program that takes a number as input and determines whether it is even or odd.
Solution:
# Function to check if a number is even or odd
def check_even_odd(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
# Input
num = int(input("Enter a number: "))
print(f"The number {num} is {check_even_odd(num)}.")
2.Calculate the Factorial of a Number
Problem: Write a program to calculate the factorial of a given number.m that takes a number as input and determines whether it is even or odd.
Solution:
# Function to calculate factorial
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Input
num = int(input("Enter a number: "))
print(f"The factorial of {num} is {factorial(num)}.")
3.Print the Fibonacci Series
Problem: Write a program to print the Fibonacci series up to n terms.
Solution:
# Function to calculate factorial
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Input
num = int(input("Enter a number: "))
print(f"The factorial of {num} is {factorial(num)}.")
4.Check if a String is a Palindrome
Problem: Check if a given string reads the same forward and backward.
Solution:
# Function to check palindrome
def is_palindrome(string):
return string == string[::-1]
# Input
text = input("Enter a string: ")
if is_palindrome(text):
print(f"'{text}' is a palindrome.")
else:
print(f"'{text}' is not a palindrome.")
5.Find the Largest and Smallest Elements in a List
Problem: Write a program to find the largest and smallest elements in a list
Solution:
# Function to find largest and smallest elements
def find_min_max(lst):
return min(lst), max(lst)
# Input
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
smallest, largest = find_min_max(numbers)
print(f"Smallest: {smallest}, Largest: {largest}")
6. Count Vowels in a String
Problem: Create a program that counts the number of vowels (a, e, i, o, u) in a given string.
Solution:
# Function to count vowels
def count_vowels(string):
vowels = 'aeiouAEIOU'
return sum(1 for char in string if char in vowels)
# Input
text = input("Enter a string: ")
print(f"The number of vowels in the string is {count_vowels(text)}.")
7. Reverse a String Without Slicing
Problem: Reverse a string without using Python’s slicing feature.
Solution:
# Function to reverse a string
def reverse_string(string):
reversed_str = ""
for char in string:
reversed_str = char + reversed_str
return reversed_str
# Input
text = input("Enter a string: ")
print(f"Reversed string: {reverse_string(text)}")
8. Check if a Number is Prime
Problem: Write a program to check if a given number is prime.
Solution:
# Function to check if a number is prime
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# Input
num = int(input("Enter a number: "))
if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
9. Find the Sum of Digits in a Number
Problem: Create a program that calculates the sum of all digits in a number.
Solution:
# Function to find the sum of digits
def sum_of_digits(n):
return sum(int(digit) for digit in str(n))
# Input
num = int(input("Enter a number: "))
print(f"The sum of the digits in {num} is {sum_of_digits(num)}.")
10. Find the Second Largest Number in a List
Problem: Find the second largest number in a list.
Solution:
# Function to find second largest number
def second_largest(lst):
unique_numbers = list(set(lst))
unique_numbers.sort()
return unique_numbers[-2]
# Input
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
print(f"The second largest number is {second_largest(numbers)}.")
Conclusion
Practicing these beginner-level Python exercises with solutions will help you to build a solid foundation in programming. If you want to explore all 30 exercises, check out the original post here: Top 30 Python Programming Exercises for Beginners to Advanced. Keep challenging yourself with new problems, and stay consistent in your learning journey. Stay tuned for the next post featuring solutions for intermediate-level challenges!