50+ Python Coding Exercises for Beginners with Solutions

By Btech Faqa

Published On:

50+ Python coding exercises for beginners with solutions

Join WhatsApp

Join Now

Learning Python is easier when you practice coding regularly. Reading concepts and watching tutorials can help you understand the basics, but Python coding exercises help you develop real programming skills.

If you are a beginner, B.Tech student, college student, or someone starting Python programming, this collection of 50+ Python coding exercises with solutions is a useful place to practice.

The exercises in this article start with simple programs and gradually introduce conditions, loops, numbers, strings, lists, dictionaries, functions, patterns, searching, and sorting.

Try to solve each exercise yourself before looking at the solution. This will help you improve your logical thinking and coding confidence.

Why Practice Python Coding Exercises?

Regular coding practice helps you understand how programming works.

By solving Python exercises, you can improve your ability to:

  • Write Python programs
  • Understand programming logic
  • Work with variables
  • Use operators
  • Apply conditions
  • Use loops
  • Work with strings
  • Manipulate lists
  • Use dictionaries
  • Create functions
  • Solve mathematical problems
  • Debug programs
  • Prepare for coding tests
  • Prepare for practical examinations

Python Coding Exercises for Beginners

1. Print Hello World

Exercise

Write a Python program to print Hello World.

Solution

print("Hello World")

Output

Hello World

2. Print Your Name

Exercise

Write a program to store your name in a variable and print it.

Solution

name = "Ravi"

print(name)

Output

Ravi

3. Add Two Numbers

Exercise

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

print("Difference =", a - b)

Output

Difference = 30

5. Multiply Two Numbers

a = 10
b = 5

print("Product =", a * b)

Output

Product = 50

6. Divide Two Numbers

a = 20
b = 5

print("Division =", a / b)

Output

Division = 4.0

Input and Basic Python Exercises

7. Take User Input

Write a program to accept a name from the user.

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)

9. Find the Square of a Number

num = 5

print("Square =", num ** 2)

Output

Square = 25

10. Find the Cube of a Number

num = 4

print("Cube =", num ** 3)

Output

Cube = 64

Conditional Statement Exercises

11. Check Even or Odd

Write a program to check whether a number is 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 or Negative

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 = 25
b = 40

if a > b:
    print(a)
else:
    print(b)

Output

40

14. Find the Largest of Three Numbers

a = 10
b = 50
c = 30

if a >= b and a >= c:
    print(a)
elif b >= a and b >= c:
    print(b)
else:
    print(c)

Output

50

15. Check Voting Eligibility

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

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

Loop-Based Python Coding Exercises

16. Print Numbers from 1 to 10

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

17. Print Even Numbers from 1 to 20

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

18. Print Odd Numbers from 1 to 20

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

19. Find the Sum of Numbers from 1 to 10

total = 0

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

print("Sum =", total)

Output

Sum = 55

20. Print a 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 10 = 50

Number-Based Coding Exercises

21. Find Factorial of a Number

num = 5
factorial = 1

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

print("Factorial =", factorial)

Output

Factorial = 120

22. 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

23. Find Sum of Digits

num = 1234
total = 0

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

print("Sum =", total)

Output

Sum = 10

24. Count Digits in a Number

num = 12345
count = 0

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

print("Digits =", count)

Output

Digits = 5

25. 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

26. 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)

27. Find Factors of a Number

num = 12

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

Output

1
2
3
4
6
12

28. Generate 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

29. Check Palindrome Number

num = 121

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

Output

Palindrome

30. Check Armstrong Number

num = 153

total = 0
temp = num

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 Coding Exercises

31. Find String Length

text = "Python"

print(len(text))

Output

6

32. Reverse a String

text = "Python"

print(text[::-1])

Output

nohtyP

33. Check String Palindrome

text = "madam"

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

34. Count Vowels

text = "python programming"

count = 0

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

print("Vowels =", count)

35. Count Consonants

text = "python"

count = 0

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

print("Consonants =", count)

36. Convert String to Uppercase

text = "python"

print(text.upper())

Output

PYTHON

37. Convert String to Lowercase

text = "PYTHON"

print(text.lower())

Output

python

38. Count Character Occurrences

text = "banana"

print(text.count("a"))

Output

3

39. Count Words in a Sentence

text = "Python is easy to learn"

words = text.split()

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

Output

Words = 5

Python List Coding Exercises

40. Create and Print a List

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

print(numbers)

41. Find Largest List Element

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

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

Output

Largest = 80

42. Find Smallest List Element

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

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

43. Find Sum of List Elements

numbers = [10, 20, 30, 40]

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

Output

Sum = 100

44. Find Average of List Elements

numbers = [10, 20, 30, 40]

average = sum(numbers) / len(numbers)

print("Average =", average)

Output

Average = 25.0

45. Sort a List

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

numbers.sort()

print(numbers)

Output

[1, 2, 3, 5, 8]

46. Reverse a List

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

numbers.reverse()

print(numbers)

Output

[5, 4, 3, 2, 1]

47. Remove Duplicates from a List

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

unique = list(set(numbers))

print(unique)

For cases where the original order matters, use an order-preserving method instead of converting directly to a set.

Python Dictionary Exercises

48. Create a Dictionary

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

print(student)

49. Access Dictionary Values

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

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

Output

Ravi
20

50. Add an Item to a Dictionary

student = {
    "name": "Ravi"
}

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

print(student)

Python Function Exercises

51. Create a Function to Add Two Numbers

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

print(add(10, 20))

Output

30

52. Function to Check Even or Odd

def check_number(num):

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

print(check_number(10))

Output

Even

53. Function to Find Factorial

def factorial(num):

    result = 1

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

    return result

print(factorial(5))

Output

120

Python Pattern Exercises

54. Print Star Pattern

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

Output

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

55. 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

Additional Python Coding Challenges

After completing the 55 exercises above, try these challenges without looking at a solution.

Challenge 1: Find the Second Largest Number

Given:

[10, 20, 50, 30, 40]

Expected result:

40

Challenge 2: Count Even and Odd Numbers

Given:

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

Expected result:

Even = 3
Odd = 3

Challenge 3: Find Duplicate Elements

Given:

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

Expected result:

2
4

Challenge 4: Find Common Elements

Given:

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

Expected result:

3
4

Challenge 5: Find Missing Number

Given:

[1, 2, 3, 5]

Expected result:

4

Challenge 6: Check Two Strings for Anagrams

Example:

listen
silent

Expected result:

Anagram

Challenge 7: Find the Longest Word

Given:

Python is easy to learn

Find the longest word.

Challenge 8: Count Character Frequency

Given:

banana

Expected result:

b = 1
a = 3
n = 2

Challenge 9: Merge Two Lists

Given:

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

Expected result:

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

Challenge 10: Find Common Words

Given two sentences, find the words that occur in both sentences.

How to Practice Python Coding Exercises

1. Try the Problem First

Before looking at the solution, spend a few minutes thinking about the problem.

Ask yourself:

  • What is the input?
  • What should the output be?
  • Which Python concept can solve it?
  • Do I need a loop?
  • Do I need a condition?
  • Do I need a list or dictionary?

2. Write the Logic

For example, for an even or odd problem:

  1. Take a number.
  2. Divide it by 2.
  3. Check the remainder.
  4. Remainder 0 means even.
  5. Otherwise, odd.

Then convert this logic into Python code.

3. Test Different Inputs

Do not test your program with only one value.

For example:

10
7
0
-4
-5

Testing different values helps you find errors.

4. Try Without Looking at the Solution

Once you understand an example, close the page and write the program yourself.

This is one of the best ways to improve your coding skills.

5. Modify the Programs

Try changing the existing programs.

For example, change:

range(1, 11)

to:

range(1, 101)

Then see how the output changes.

Common Python Topics to Practice

Beginners should practice these important Python topics:

Python Basics

  • Variables
  • Data types
  • Input and output
  • Type conversion
  • Operators

Conditional Statements

  • if
  • if-else
  • elif
  • Nested if

Loops

  • for
  • while
  • break
  • continue
  • pass

Strings

  • Slicing
  • Searching
  • Replacing
  • Counting
  • Reversing
  • String methods

Collections

  • Lists
  • Tuples
  • Sets
  • Dictionaries

Functions

  • Parameters
  • Arguments
  • Return values
  • Default arguments
  • Lambda functions

Problem Solving

  • Prime numbers
  • Factorial
  • Fibonacci
  • Palindrome
  • Armstrong numbers
  • Searching
  • Sorting
  • Pattern programs

Tips for Python Beginners

Practice Every Day

Try to solve at least 3–5 coding exercises every day.

Regular practice is more useful than trying to learn hundreds of programs in one day.

Understand the Logic

Do not memorize complete programs. Understand why each line is used.

Start Simple

Begin with basic problems such as:

  • Add two numbers
  • Even or odd
  • Largest number
  • Factorial
  • Reverse a string

Then move to more difficult problems.

Practice Debugging

When your code does not work, read the error message carefully.

Try to identify whether the problem is:

  • Syntax
  • Logic
  • Variable
  • Data type
  • Runtime

Practice Without Notes

Before an exam or coding test, try writing common programs without looking at your notes.

Frequently Asked Questions

What are Python coding exercises?

Python coding exercises are programming problems that help learners practice Python syntax, programming logic, and problem-solving.

Are these Python exercises suitable for beginners?

Yes. The exercises start with basic Python concepts and gradually introduce loops, strings, lists, dictionaries, functions, and problem-solving.

How many Python exercises should I practice every day?

Beginners can start with 3–5 exercises per day and gradually increase the difficulty.

Are Python coding exercises useful for B.Tech students?

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

Should I memorize Python programs?

No. Understanding the logic is more important than memorizing complete programs. Once you understand the logic, you can create similar programs yourself.

Which Python exercises should beginners start with?

Start with input/output, arithmetic operations, conditions, loops, even and odd numbers, factorial, Fibonacci, palindrome, strings, lists, and functions.

Conclusion

Regular practice is one of the best ways to learn Python programming. These 50+ Python coding exercises provide a simple starting point for beginners who want to improve their programming skills.

The exercises cover important topics such as variables, operators, conditional statements, loops, numbers, strings, lists, dictionaries, functions, and patterns.

Do not just copy the solutions. Try each exercise yourself first, test your code with different inputs, and understand why the solution works.

With consistent practice, you can improve your Python coding logic and become more confident in solving programming problems.

🔴Related Post

Leave a Comment