Learning Python can feel overwhelming at first, but you can speed this process up.
How?
By practicing writing code from day one.
We put together a list of Python assignments for beginners exploring basic concepts in Python to help you learn faster.
Each exercise will introduce you to a key concept, provide a step-by-step explanation, and show how it can be useful in real life.
Jump to the one you like the most, or keep reading:
1. Printing and variables
2. User input and simple calculations
3. Basic conditional statements
4. Loops (counting to 10)
5. Checking prime numbers
6. Basic string operations
7. Finding the longest word
8. Reversing a string
9. Palindrome check
10. Finding duplicates in a list
11. Calculating discounts
12. Expense tracker
13. Employee salary calculation
14. Customer order system
15. FizzBuzz
Looking for something slightly more advanced? Check out our list of the best Python projects for beginners.
Let’s start with the absolute basics: how to print text, store values in variables, and display them.
Every Python program needs to store and display data. This is the first step to writing meaningful code.
What is a variable?
A variable is a named storage location for a value. For example:
name = "Alice"
age = 25
“Alice” is stored in name, and 25 is stored in age.
Assignment
Write a program that:
This task is useful in real life when building profile pages (e.g., user registration forms), chatbots (e.g., displaying user details dynamically), and so on.
Solution
name = "Alice"
age = 25
print("Hello, my name is", name, "and I am", age, "years old.")
Now that you can print text and use variables, learn how to let users enter data and perform basic calculations.
Why is this important? Because interactive programs ask users for input rather than using fixed values.
For example, you can apply this skill when building age verification systems (e.g., websites that check if you’re over 18) or interactive registration forms.
Assignment
Write a program that:
Solution
name = input("Enter your name: ")
birth_year = int(input("Enter your birth year: "))
age = 2024 - birth_year # Assuming the current year is 2024
print("Hello", name + "! You are", age, "years old.")
Programs often need to make decisions. Let’s learn how to check if a number is even or odd.
Conditional statements (if, else) let programs make smart decisions instead of running in a fixed way.
This can be applied in all sorts of real-world scenarios.
For example, if you’re building a banking app and need to add a feature that checks if an account number follows an even/odd rule. Or if you create a lottery system that generates even or odd lucky numbers.
Assignment
Write a program that:
Solution
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even")
else:
print("Odd")
Sometimes, you need a program to repeat a task multiple times. Loops allow you to do this efficiently.
Without them, you would have to write the same code multiple times manually.
They also help with various functions, such as keeping score in games or sending automated reminders.
What is a while loop?
A while loop repeats a block of code as long as a condition is true.
Assignment
Write a program that prints numbers from 1 to 10 using a while loop.
Solution
count = 1
while count <= 10:
print(count)
count += 1 # Increase count by 1 each time
Next, let’s apply loops and conditionals together to check if a number is prime.
This skill can help in areas like cybersecurity encryption where prime numbers are used to secure data online through cryptographic algorithms.
Assignment
Write a program that checks if a number is prime.
Solution
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
num = int(input("Enter a number: "))
print(is_prime(num))
Text processing is a fundamental part of programming. Start with an easy exercise: counting vowels in a sentence.
Many real-world applications need to analyze text—for example, word counting in documents or detecting the most common letters in a language.
It can be especially useful for spell checkers, text-to-speech programs that analyze vowels in words, and similar applications.
What is a string?
A string is a sequence of characters, such as:
text = "Hello, World!"
Strings can be looped through, modified, and checked for specific letters.
Assignment
Write a function count_vowels(text) that counts and returns the number of vowels (a, e, i, o, u) in a given sentence.
Solution
def count_vowels(text):
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
return count
sentence = input("Enter a sentence: ")
print("Number of vowels:", count_vowels(sentence))
Now that you can manipulate text, learn how to break down a sentence into words and find the longest one.
How can it be applied to real life?
Search engines and AI chatbots identify important words to give better responses.
For example, features like text summarization find keywords in longer articles and return a balanced result.
Assignment
Write a function longest_word(sentence) that returns the longest word in a given sentence.
Solution
def longest_word(sentence):
words = sentence.split() # Split sentence into words
longest = words[0]
for word in words:
if len(word) > len(longest):
longest = word
return longest
sentence = input("Enter a sentence: ")
print("Longest word:", longest_word(sentence))
Next, combine what you’ve learned so far to manually reverse a string.
Many programs manipulate text data, such as reversing words in usernames or IDs. This technique is also useful for simple text encryption.
Assignment
Write a function reverse_string(text) that:
Solution
def reverse_string(text):
reversed_text = ""
for char in text:
reversed_text = char + reversed_text
return reversed_text
word = input("Enter a word: ")
print("Reversed:", reverse_string(word))
Create an empty string reversed_text to store the reversed word.
Then, loop through each character in the original string:
Once the loop is done, return the reversed string.
You can also check if a word reads the same forward and backward.
Palindromes are used in data validation, security, and DNA sequence analysis. For example, they help with password validation (checking if passwords meet certain criteria) and genetic research (identifying palindromic sequences in DNA).
Assignment
Write a function is_palindrome(word) that checks if a word is a palindrome.
Solution
def is_palindrome(word):
reversed_word = ""
for char in word:
reversed_word = char + reversed_word
return word == reversed_word
word = input("Enter a word: ")
print("Is palindrome:", is_palindrome(word))
Next, practice working with lists and check for repeated elements.
There are many ways to apply this skill: e.g., to detect duplicate orders in ecommerce or add spam filters that check for repeated emails.
Assignment
Write a function find_duplicates(lst) that finds and returns a list of duplicate values.
Solution
def find_duplicates(lst):
seen = set()
duplicates = set()
for num in lst:
if num in seen:
duplicates.add(num)
seen.add(num)
return list(duplicates)
numbers = [1, 2, 3, 2, 4, 5, 6, 5]
print("Duplicates:", find_duplicates(numbers))
From here, you can experiment with more advanced assignments connected to real-world use cases.
For instance, imagine you’re building an online store and need to apply a discount to products dynamically.
Assignment
Write a function apply_discount(price, discount_percent) that:
Solution
def apply_discount(price, discount_percent):
if price < 0 or discount_percent < 0 or discount_percent > 100:
return "Invalid input"
discount_amount = (discount_percent / 100) * price
return round(price - discount_amount, 2)
# Example usage
print(apply_discount(100, 20)) # Output: 80.0
print(apply_discount(50, 10)) # Output: 45.0
print(apply_discount(200, 50)) # Output: 100.0
Managing finances is another essential skill.
This assignment helps you build a simple expense tracker that calculates how much you’ve spent and the average expense per item.
What is a list?
A list stores multiple values in a single variable. Example:
expenses = [20, 15.5, 10, 5]
Lists make it easy to store and analyze multiple numbers.
Assignment
Write a function track_expenses(expenses) that calculates:
Solution
def track_expenses(expenses):
if not expenses:
return "No expenses recorded."
total_spent = sum(expenses)
average_expense = total_spent / len(expenses)
return round(total_spent, 2), round(average_expense, 2)
expenses = [20, 15.5, 10, 5]
print("Total and average expenses:", track_expenses(expenses))
Companies calculate employee salaries based on hours worked and additional bonuses. Write a function to automate this calculation.
Assignment
Write a function calculate_salary(hours_worked, hourly_rate, bonus=0) that calculates an employee’s salary, including optional bonuses.
Solution
def calculate_salary(hours_worked, hourly_rate, bonus=0):
if hours_worked < 0 or hourly_rate < 0 or bonus < 0:
return "Invalid input"
salary = (hours_worked * hourly_rate) + bonus
return round(salary, 2)
print(calculate_salary(40, 15)) # Output: 600.0
print(calculate_salary(40, 15, 100)) # Output: 700.0
Every online store needs a system to process orders. Try building a simple order calculator using dictionaries.
What is a dictionary?
A dictionary stores data in key-value pairs. For example:
menu = {"burger": 5, "fries": 2.5, "soda": 1.5}
Here, “burger” is the key, and 5 is its price.
Assignment
Write a function place_order(order_items, menu_prices) that calculates the total cost of an order.
Solution
def place_order(order_items, menu_prices):
total_cost = 0
for item, quantity in order_items.items():
if item in menu_prices:
total_cost += menu_prices[item] * quantity
else:
return f"Error: {item} is not available on the menu."
return round(total_cost, 2)
menu = {"burger": 5, "fries": 2.5, "soda": 1.5}
order = {"burger": 2, "fries": 1}
print("Total order cost:", place_order(order, menu))
# Output: 12.5
The FizzBuzz challenge is a common coding interview question that checks your problem-solving skills and ability to use Python loops.
Write a function fizz_buzz(n) that prints numbers from 1 to n, but:
Solution
def fizz_buzz(n):
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
fizz_buzz(15)
Looking for more Python exercises with solutions and guidelines? Here are other free resources you can try:
The examples we shared in this guide can help you acquire practical skills. But, the best way to learn Python is by combining theory with practice and getting useful feedback.
If you’re looking for something more structured, Mimo’s Python Career Path is right up your alley. It combines bite-sized lessons with interactive assignments and instant AI feedback.
You’ll move from one topic to another by learning theory and solving challenges within Mimo’s interactive platform.
The platform will help you learn each concept through a mix of text and simple assignments.
As you progress, you’ll be collecting points and building up your streaks.
Soon enough, you’ll start solving more complex problems like:
And more!
These Python exercises guide you from basic concepts like variables and printing to more advanced topics such as functions, loops, and dictionaries.
Each exercise tackles fundamental programming skills while demonstrating their real-world applications.
The key to mastering Python? Consistent practice combined with useful feedback.
If you’re looking to learn Python fast, try Mimo’s Python Career Path, which offers a fun, effective learning experience and a certificate.