Python Exercises

Python is one of the most beginner-friendly programming languages, but the best way to truly master it is through consistent practice. At The Coding College, we understand the importance of hands-on learning, which is why we’ve curated this guide to Python exercises for all skill levels—from beginners to advanced programmers.

Why Practice Python with Exercises?

Practical exercises are the cornerstone of coding mastery. Here’s how they help:

  • Reinforce Learning: Solidify theoretical knowledge by applying it to real problems.
  • Build Problem-Solving Skills: Improve your ability to analyze and break down problems into logical steps.
  • Prepare for Real-World Applications: Practice concepts commonly used in professional Python projects.

Whether you’re learning Python basics or preparing for coding interviews, these exercises will accelerate your growth.

Beginner-Level Python Exercises

1. Hello World

Write a Python program to print “Hello, World!”

print("Hello, World!")  

2. Sum of Two Numbers

Write a program to calculate the sum of two numbers entered by the user.

# Input two numbers  
a = int(input("Enter first number: "))  
b = int(input("Enter second number: "))  

# Calculate sum  
print("Sum:", a + b)  

3. Check Even or Odd

Determine if a number is even or odd.

num = int(input("Enter a number: "))  

if num % 2 == 0:  
    print("Even")  
else:  
    print("Odd")  

Intermediate-Level Python Exercises

1. Factorial of a Number

Calculate the factorial of a number using a for loop.

num = int(input("Enter a number: "))  
factorial = 1  

for i in range(1, num + 1):  
    factorial *= i  

print("Factorial:", factorial)  

2. Fibonacci Sequence

Generate the first n numbers of the Fibonacci sequence.

n = int(input("Enter the number of terms: "))  
a, b = 0, 1  

for _ in range(n):  
    print(a, end=" ")  
    a, b = b, a + b  

3. Prime Number Check

Check if a given number is a prime number.

num = int(input("Enter a number: "))  

if num > 1:  
    for i in range(2, int(num**0.5) + 1):  
        if num % i == 0:  
            print("Not a prime number")  
            break  
    else:  
        print("Prime number")  
else:  
    print("Not a prime number")  

Advanced-Level Python Exercises

1. Palindrome Check

Check if a string is a palindrome (reads the same forward and backward).

string = input("Enter a string: ")  

if string == string[::-1]:  
    print("Palindrome")  
else:  
    print("Not a palindrome")  

2. Merge and Sort Two Lists

Combine two lists and sort the resulting list.

list1 = [5, 3, 8]  
list2 = [4, 7, 1]  

merged_list = sorted(list1 + list2)  
print("Merged and sorted list:", merged_list)  

3. Dictionary Frequency Count

Count the frequency of each word in a string using a dictionary.

text = input("Enter a sentence: ")  
words = text.split()  

frequency = {}  
for word in words:  
    frequency[word] = frequency.get(word, 0) + 1  

print("Word frequency:", frequency)  

Python Practice Resources at The Coding College

Looking for more exercises to test your skills? Explore these sections on our website:

  • Python Basics: A step-by-step guide for beginners.
  • Advanced Python Projects: Real-world challenges to build your portfolio.
  • Interview Prep Questions: Exercises tailored for coding interviews.

How to Get the Most Out of Python Exercises

  1. Understand the Problem
    Read the exercise description carefully before coding.
  2. Code Without Copying
    Try to solve the exercise on your own before looking at the solution.
  3. Iterate and Optimize
    Once your code works, think about ways to make it more efficient or readable.
  4. Practice Regularly
    Consistency is key to mastering Python.

Start Your Python Journey Today!

At The Coding College, we’re committed to helping you succeed in your programming journey. Dive into our Python tutorials, challenges, and projects to level up your skills.

Leave a Comment