Here’s a collection of coding problems suited for Python beginners, each with an example and explanation.
-
Sum of Digits
Problem:
Write a program that calculates the sum of the digits of a given number.
Example:
# Take input from the user
number = input("Enter a number: ")
# Calculate the sum of digits
digit_sum = sum(int(digit) for digit in number)
# Print the result
print(f"The sum of the digits is: {digit_sum}")
The program uses a generator expression inside the `sum()` function to iterate over each digit in the input string, convert it to an integer, and sum them up.
-
Factorial Calculation
Problem:
Write a program to calculate the factorial of a given number.
Example:
# Take input from the user
n = int(input("Enter a number: "))
# Initialize factorial
factorial = 1
# Calculate factorial
for i in range(1, n + 1):
factorial *= i
# Print the result
print(f"The factorial of {n} is {factorial}")
This program uses a loop to multiply numbers from 1 to `n` to compute the factorial.
-
Count Vowels in a String
Problem:
Write a program that counts the number of vowels in a given string.
Example:
# Take input from the user
text = input("Enter a string: ")
# Define vowels
vowels = "aeiou"
# Count vowels
vowel_count = sum(1 for char in text.lower() if char in vowels)
# Print the result
print(f"Number of vowels in the string: {vowel_count}")
The program converts the string to lowercase and uses a generator expression to count characters that are vowels.
-
Reverse a String
Problem:
Write a program to reverse a given string.
Example:
# Take input from the user
text = input("Enter a string: ")
# Reverse the string
reversed_text = text[::-1]
# Print the result
print(f"Reversed string: {reversed_text}")
String slicing `[::-1]` is used to reverse the string by stepping backwards through the entire string.
-
Find the Largest Number in a List
Problem:
Write a program that finds the largest number in a list of integers.
Example:
# Define a list of integers
numbers = [int(x) for x in input("Enter numbers separated by spaces: ").split()]
# Find the largest number
largest_number = max(numbers)
# Print the result
print(f"The largest number is {largest_number}")
The program uses the `max()` function to find the largest number in the list created from user input.
-
Generate Multiplication Table
Problem:
Write a program to generate a multiplication table for a given number.
Example:
# Take input from the user
number = int(input("Enter a number: "))
# Generate and print the multiplication table
print(f"Multiplication table for {number}:")
for i in range(1, 11):
print(f"{number} x {i} = {number * i}")
The program uses a loop to generate and print the multiplication table for the given number.
-
Remove Duplicates from a List
Problem:
Write a program to remove duplicate values from a list.
Example:
# Define a list with duplicate values
original_list = [int(x) for x in input("Enter numbers separated by spaces: ").split()]
# Remove duplicates by converting to a set
unique_list = list(set(original_list))
# Print the result
print(f"List without duplicates: {unique_list}")
Converting the list to a `set` removes duplicates, and then converting it back to a `list` provides the unique values.
-
Check for Prime Number
Problem:
Write a program that checks if a given number is a prime number.
Example:
# Take input from the user
number = int(input("Enter a number: "))
# Check if the number is prime
if number > 1:
for i in range(2, int(number0.5) + 1):
if number % i == 0:
print(f"{number} is not a prime number.")
break
else:
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")
The program checks for factors up to the square root of the number to determine if it is prime. If any factors are found, the number is not prime.
-
Sum of Even Numbers in a List
Problem:
Write a program to find the sum of all even numbers in a list.
Example:
# Define a list of integers
numbers = [int(x) for x in input("Enter numbers separated by spaces: ").split()]
# Calculate the sum of even numbers
even_sum = sum(num for num in numbers if num % 2 == 0)
# Print the result
print(f"Sum of even numbers: {even_sum}")
The program uses a generator expression inside the `sum()` function to filter and sum only the even numbers.
-
Convert Temperature
Problem:
Write a program to convert a temperature from Celsius to Fahrenheit and vice versa.
Example:
# Take input from the user
temp = float(input("Enter temperature: "))
unit = input("Enter 'C' for Celsius to Fahrenheit or 'F' for Fahrenheit to Celsius: ").upper()
# Perform the conversion
if unit == 'C':
converted_temp = (temp * 9/5) + 32
print(f"{temp} Celsius is {converted_temp} Fahrenheit")
elif unit == 'F':
converted_temp = (temp - 32) * 5/9
print(f"{temp} Fahrenheit is {converted_temp} Celsius")
else:
print("Invalid input")
The program uses conditional statements to determine the conversion type based on user input and applies the respective formula.
These problems cover a range of fundamental Python concepts including loops, conditionals, list operations, and user input. Working through them can help build a solid foundation in programming with Python.
Could not find the solution ?