50+ Python Programming Exercises with Solutions

By Btech Faqa

Published On:

50+ Python coding exercises for beginners with solutions

Join WhatsApp

Join Now

Python is one of the most popular programming languages for beginners, students, developers, and professionals. Its simple syntax makes it easy to learn, but regular practice is important to become confident in programming.

If you are learning Python and looking for Python programming exercises with solutions, this article provides more than 50 beginner-friendly exercises.

These exercises cover important Python concepts such as variables, input and output, operators, conditional statements, loops, numbers, strings, lists, dictionaries, functions, and basic problem-solving.

Try to solve each exercise yourself before checking the solution. This will help you develop programming logic instead of simply memorizing programs.

Why Practice Python Programming Exercises?

Programming becomes easier when you solve problems regularly.

Python exercises can help you:

  • Understand Python syntax
  • Improve programming logic
  • Practice problem-solving
  • Learn loops and conditions
  • Understand strings and lists
  • Work with functions
  • Prepare for practical exams
  • Prepare for coding tests
  • Improve debugging skills
  • Build confidence in programming

Basic Python Programming Exercises

1. Print Hello World

Write a Python program to display Hello World.

Solution

print("Hello World")

Output

Hello World

2. Print Your Name

Write a program to store and print your name.

Solution

name = "Ravi"

print(name)

Output

Ravi

3. Add Two Numbers

Write a program to add two numbers.

Solution

a = 10
b = 20

result = a + b

print("Sum =", result)

Output

Sum = 30

4. Subtract Two Numbers

a = 50
b = 20

result = a - b

print("Difference =", result)

Output

Difference = 30

5. Multiply Two Numbers

a = 10
b = 5

result = a * b

print("Product =", result)

Output

Product = 50

6. Divide Two Numbers

a = 20
b = 5

result = a / b

print("Result =", result)

Output

Result = 4.0

Python Input Exercises

7. Take Name as Input

Write a program to accept the user’s name.

Solution

name = input("Enter your name: ")

print("Hello", name)

Example Output

Enter your name: Ravi
Hello Ravi

8. Add Two User Inputs

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

print("Sum =", a + b)

Example Output

Enter first number: 15
Enter second number: 25
Sum = 40

9. Calculate the Area of a Circle

The formula is:

Area = π × r × r

Solution

import math

r = 5

area = math.pi * r * r

print("Area =", area)

10. Calculate Simple Interest

Formula:

SI = (P × R × T) / 100

Solution

p = 10000
r = 5
t = 2

si = (p * r * t) / 100

print("Simple Interest =", si)

Output

Simple Interest = 1000.0

Python Conditional Programming Exercises

11. Check Even or Odd

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

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

Example Output

Enter a number: 12
Even

12. Check Positive, Negative, or Zero

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

if num > 0:
    print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")

13. Find the Largest of Two Numbers

a = 30
b = 50

if a > b:
    print("Largest =", a)
else:
    print("Largest =", b)

Output

Largest = 50

14. Find the Largest of Three Numbers

a = 20
b = 60
c = 40

if a >= b and a >= c:
    largest = a
elif b >= a and b >= c:
    largest = b
else:
    largest = c

print("Largest =", largest)

Output

Largest = 60

15. Check Voting Eligibility

age = int(input("Enter age: "))

if age >= 18:
    print("Eligible")
else:
    print("Not Eligible")

16. Check Leap Year

year = int(input("Enter year: "))

if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
    print("Leap Year")
else:
    print("Not a Leap Year")

Python Loop Exercises

17. Print Numbers from 1 to 10

for i in range(1, 11):
    print(i)

18. Print Even Numbers

for i in range(1, 21):
    if i % 2 == 0:
        print(i)

Output

2
4
6
8
10
12
14
16
18
20

19. Print Odd Numbers

for i in range(1, 21):
    if i % 2 != 0:
        print(i)

20. Find Sum of Numbers

Find the sum from 1 to 10.

total = 0

for i in range(1, 11):
    total += i

print("Sum =", total)

Output

Sum = 55

21. Print Multiplication Table

num = 5

for i in range(1, 11):
    print(num, "x", i, "=", num * i)

Output

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Python Number Programming Exercises

22. Find Factorial

num = 5
factorial = 1

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

print("Factorial =", factorial)

Output

Factorial = 120

23. Reverse a Number

num = 12345
reverse = 0

while num > 0:
    digit = num % 10
    reverse = reverse * 10 + digit
    num //= 10

print("Reverse =", reverse)

Output

Reverse = 54321

24. Find Sum of Digits

num = 1234
total = 0

while num > 0:
    total += num % 10
    num //= 10

print("Sum =", total)

Output

Sum = 10

25. Count Digits

num = 12345
count = 0

while num > 0:
    num //= 10
    count += 1

print("Number of digits =", count)

Output

Number of digits = 5

26. Check Prime Number

A prime number has exactly two positive factors: 1 and itself.

num = 7

if num < 2:
    print("Not Prime")
else:
    for i in range(2, num):
        if num % i == 0:
            print("Not Prime")
            break
    else:
        print("Prime")

Output

Prime

27. Print Prime Numbers

Print prime numbers from 1 to 50.

for num in range(2, 51):

    for i in range(2, num):
        if num % i == 0:
            break
    else:
        print(num)

28. Find Factors

num = 12

for i in range(1, num + 1):
    if num % i == 0:
        print(i)

Output

1
2
3
4
6
12

29. Fibonacci Series

n = 10

a = 0
b = 1

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

Output

0 1 1 2 3 5 8 13 21 34

30. Check Palindrome Number

num = 121

if str(num) == str(num)[::-1]:
    print("Palindrome")
else:
    print("Not Palindrome")

Output

Palindrome

31. Check Armstrong Number

For example:

153 = 1³ + 5³ + 3³

Solution

num = 153
temp = num
total = 0

while temp > 0:
    digit = temp % 10
    total += digit ** 3
    temp //= 10

if total == num:
    print("Armstrong Number")
else:
    print("Not Armstrong Number")

Output

Armstrong Number

Python String Exercises

32. Find String Length

text = "Python"

print(len(text))

Output

6

33. Reverse a String

text = "Python"

print(text[::-1])

Output

nohtyP

34. Check String Palindrome

text = "madam"

if text == text[::-1]:
    print("Palindrome")
else:
    print("Not Palindrome")

35. Count Vowels

text = "python programming"

count = 0

for char in text.lower():
    if char in "aeiou":
        count += 1

print("Vowels =", count)

36. Count Consonants

text = "python"

count = 0

for char in text.lower():
    if char.isalpha() and char not in "aeiou":
        count += 1

print("Consonants =", count)

37. Convert String to Uppercase

text = "python"

print(text.upper())

Output

PYTHON

38. Convert String to Lowercase

text = "PYTHON"

print(text.lower())

Output

python

39. Count a Character

text = "banana"

print(text.count("a"))

Output

3

40. Count Words

text = "Python is easy to learn"

words = text.split()

print("Words =", len(words))

Output

Words = 5

Python List Exercises

41. Create a List

numbers = [10, 20, 30, 40, 50]

print(numbers)

42. Find Largest Element

numbers = [10, 50, 20, 80, 30]

print("Largest =", max(numbers))

Output

Largest = 80

43. Find Smallest Element

numbers = [10, 50, 20, 80, 30]

print("Smallest =", min(numbers))

Output

Smallest = 10

44. Find List Sum

numbers = [10, 20, 30, 40]

print("Sum =", sum(numbers))

Output

Sum = 100

45. Find List Average

numbers = [10, 20, 30, 40]

average = sum(numbers) / len(numbers)

print("Average =", average)

Output

Average = 25.0

46. Sort a List

numbers = [5, 2, 8, 1, 3]

numbers.sort()

print(numbers)

Output

[1, 2, 3, 5, 8]

47. Reverse a List

numbers = [1, 2, 3, 4, 5]

numbers.reverse()

print(numbers)

Output

[5, 4, 3, 2, 1]

48. Remove Duplicate Values

numbers = [1, 2, 2, 3, 3, 4]

unique = list(set(numbers))

print(unique)

If preserving the original order is important, use an order-preserving technique instead of directly converting to a set.

Python Dictionary Exercises

49. Create a Dictionary

student = {
    "name": "Ravi",
    "age": 20,
    "course": "B.Tech"
}

print(student)

50. Access Dictionary Values

student = {
    "name": "Ravi",
    "age": 20
}

print(student["name"])
print(student["age"])

Output

Ravi
20

51. Add a Dictionary Item

student = {
    "name": "Ravi"
}

student["course"] = "B.Tech"

print(student)

Python Function Exercises

52. Add Two Numbers Using a Function

def add(a, b):
    return a + b

print(add(10, 20))

Output

30

53. Check Even or Odd Using a Function

def check_number(num):

    if num % 2 == 0:
        return "Even"
    else:
        return "Odd"

print(check_number(10))

Output

Even

54. Factorial Using a Function

def factorial(num):

    result = 1

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

    return result

print(factorial(5))

Output

120

Python Pattern Exercises

55. Print Star Pattern

for i in range(1, 6):
    print("*" * i)

Output

*
**
***
****
*****

56. Print Number Pattern

for i in range(1, 6):

    for j in range(1, i + 1):
        print(j, end=" ")

    print()

Output

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

More Python Programming Exercises to Try

After completing the exercises above, try these problems without looking for a solution.

57. Find the Second Largest Number

Given:

[10, 20, 50, 30, 40]

Expected output:

40

58. Count Even and Odd Numbers

Given:

[1, 2, 3, 4, 5, 6]

Expected result:

Even = 3
Odd = 3

59. Find Duplicate Elements

Given:

[1, 2, 2, 3, 4, 4]

Expected result:

2
4

60. Find Common Elements

Given:

List 1 = [1, 2, 3, 4]
List 2 = [3, 4, 5, 6]

Expected output:

3
4

61. Find Missing Number

Given:

[1, 2, 3, 5]

Expected output:

4

62. Check Anagram Strings

Check whether these two strings are anagrams:

listen
silent

Expected output:

Anagram

63. Find Longest Word

Given:

Python is easy to learn

Find the longest word.

64. Count Character Frequency

Given:

banana

Expected result:

b = 1
a = 3
n = 2

65. Merge Two Lists

Given:

[1, 2, 3]
[4, 5, 6]

Expected output:

[1, 2, 3, 4, 5, 6]

How to Practice Python Programming

Start With Easy Exercises

If you are completely new to Python, start with:

  1. Print statements
  2. Variables
  3. Input
  4. Arithmetic operations
  5. Conditions
  6. Loops

After understanding these topics, move to strings, lists, dictionaries, and functions.

Write Code Yourself

Do not simply copy the programs from tutorials or websites.

First understand the problem and try to write the solution yourself.

Test Different Inputs

For example, if you create an even/odd program, test:

10
7
0
-4
-5

Testing different values helps you identify errors.

Practice Without Looking at Solutions

After studying a program, close the solution and try writing it again.

This helps you remember the programming logic rather than just the code.

Debug Your Programs

Errors are normal when learning programming.

Common Python errors include:

  • SyntaxError
  • NameError
  • TypeError
  • ValueError
  • IndexError
  • ZeroDivisionError

Read the error message carefully and try to understand what caused it.

Python Programming Practice Plan

You can use the following simple practice plan.

Day 1

Practice:

  • Print statements
  • Variables
  • Input
  • Arithmetic operators

Day 2

Practice:

  • if
  • if-else
  • elif
  • Nested conditions

Day 3

Practice:

  • for
  • while
  • break
  • continue

Day 4

Practice:

  • Factorial
  • Prime numbers
  • Fibonacci
  • Palindrome
  • Armstrong number

Day 5

Practice:

  • Strings
  • Slicing
  • String methods
  • Character counting

Day 6

Practice:

  • Lists
  • Sorting
  • Searching
  • Removing duplicates

Day 7

Practice:

  • Dictionaries
  • Functions
  • Patterns
  • Mixed problems

Frequently Asked Questions

What are Python programming exercises?

Python programming exercises are coding problems designed to help learners practice Python syntax, programming concepts, and problem-solving.

Are these exercises suitable for beginners?

Yes. The exercises begin with basic programs and gradually introduce loops, strings, lists, dictionaries, functions, and problem-solving.

How many Python exercises should I solve every day?

Beginners can start with 3–5 exercises per day. As your skills improve, you can increase the number and difficulty of the problems.

Are Python programming exercises useful for B.Tech students?

Yes. They can help B.Tech students prepare for programming labs, practical examinations, assignments, coding tests, and beginner-level technical interviews.

Should I memorize Python programs?

It is better to understand the logic behind a program instead of memorizing the complete code. Understanding the logic makes it easier to solve new problems.

Which Python topics should beginners practice?

Beginners should practice variables, data types, operators, conditions, loops, strings, lists, tuples, dictionaries, functions, and basic problem-solving.

Conclusion

Regular coding practice is one of the best ways to learn Python programming. These 50+ Python programming exercises with solutions cover many of the concepts that beginners need to understand.

Start with simple programs and gradually move toward more challenging exercises. Try writing every program yourself, test it with different inputs, and understand the logic behind the solution.

Whether you are learning Python for college, practical exams, coding practice, or personal projects, solving programming exercises regularly can help you become more confident and improve your problem-solving skills.

🔴Related Post

Leave a Comment