Python Coding Exercises for Beginners
Introduction
Practicing coding exercises is one of the most effective ways for beginners to learn Python. Exercises help you understand fundamental programming concepts, improve problem-solving skills, and gain confidence in writing real code. In this guide, we’ll explore essential Python exercises for beginners, including step-by-step examples and explanations.
1. Hello World Program
Objective: Understand how to display output in Python.
Exercise:
Write a program that prints:
Hello, Python!
Solution:
print("Hello, Python!")
Explanation:
print()outputs text to the console.- Strings must be enclosed in quotes (
""or'').
2. Variables and User Input
Objective: Learn to store data and interact with users.
Exercise:
Ask the user for their name and age, then display a message:
name = input("Enter your name: ")
age = input("Enter your age: ")
print(f"Hello, {name}! You are {age} years old.")
Explanation:
input()collects data from the user.f-stringsallow inserting variables directly into strings.
3. Simple Calculator
Objective: Practice arithmetic operators.
Exercise:
Create a calculator that adds two numbers:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
sum = num1 + num2
print("Sum:", sum)
Explanation:
- Convert input to
floatfor decimal calculations. - Use arithmetic operators like
+,-,*,/.
4. Even or Odd Number Checker
Objective: Learn conditional statements.
Exercise:
Check if a number is even or odd:
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is even.")
else:
print(f"{num} is odd.")
Explanation:
%is the modulus operator, giving the remainder.if-elsestatements allow decision-making.
5. Find the Largest Number
Objective: Practice conditional logic.
Exercise:
Ask the user for three numbers and find the largest:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))if a > b and a > c:
print("Largest number is", a)
elif b > c:
print("Largest number is", b)
else:
print("Largest number is", c)
Explanation:
andallows combining conditions.elifhandles multiple checks efficiently.
6. Loop Exercises
a) Print Numbers 1–10
for i in range(1, 11):
print(i)
b) Sum of Numbers 1–100
sum = 0
for i in range(1, 101):
sum += i
print("Sum:", sum)
Explanation:
forloop iterates over a sequence.- Loops are powerful for repeating tasks efficiently.
7. Lists and Loops
Objective: Learn to work with collections of data.
Exercise:
Store names in a list and print each name:
names = ["Aisha", "Sara", "Ali"]
for name in names:
print("Hello", name)
Explanation:
- Lists can store multiple items.
forloops iterate over each item in the list.
8. Functions in Python
Objective: Understand reusable code blocks.
Exercise:
Create a function to greet users:
def greet(name):
print(f"Hello, {name}!")greet("Aisha")
greet("Ali")
Explanation:
- Functions group code into reusable blocks.
- Parameters allow dynamic input for the function.
9. Simple Guessing Game
Objective: Use loops, conditionals, and random numbers.
Exercise:
import random
number = random.randint(1, 10)
guess = 0while guess != number:
guess = int(input("Guess a number between 1 and 10: "))
if guess < number:
print("Too low!")
elif guess > number:
print("Too high!")
else:
print("Correct! You guessed the number.")
Explanation:
random.randint()generates a random number.whileloop continues until the correct guess.- Conditionals provide feedback to the user.
10. Basic String Manipulation
Objective: Learn string operations.
Exercise:
text = input("Enter a sentence: ")
print("Uppercase:", text.upper())
print("Lowercase:", text.lower())
print("Word count:", len(text.split()))
Explanation:
.upper()and.lower()change case..split()divides the string into words.len()counts items.
Conclusion
Practicing Python exercises regularly is the fastest way to improve your programming skills. These exercises cover basic concepts like variables, loops, conditionals, functions, and lists, helping beginners gain confidence. Start with simple exercises and gradually try more complex projects. Consistent practice will build a strong foundation for advanced Python topics.