Crack Your Dream Job at a Product-Based Company

30 Python Basic Exercises for Beginners with Solutions

Master beginner Python skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in Python, setting a solid foundation for intermediate challenges. Start your journey to Python mastery today!

Learning Objectives:

By the end of these exercises, you will have a strong understanding of Python loops, conditionals, and basic data structures like lists and dictionaries.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Find the sum of two numbers

Required Input:

4, 5

Expected Output:

9

Code In Python

def sum_two_numbers(): a = 4 b = 5 # Your code here return

Output

Click Run Button to view compiled output

2. Determine if a number is even or odd

Required Input:

7

Expected Output:

Odd

Code In Python

def check_even_odd(): n = 7 # Your code here return

Output

Click Run Button to view compiled output

3. Find the largest of three numbers

Required Input:

3, 9, 2

Expected Output:

9

Code In Python

def largest_of_three(): a, b, c = 3, 9, 2 # Your code here return

Output

Click Run Button to view compiled output

4. Reverse a string

Required Input:

hello

Expected Output:

olleh

Code In Python

def reverse_string(): s = "hello" # Your code here return

Output

Click Run Button to view compiled output

5. Check if a string is a palindrome

Required Input:

madam

Expected Output:

True

Code In Python

def is_palindrome(): s = "madam" # Your code here return

Output

Click Run Button to view compiled output

6. Calculate the factorial of a number

Required Input:

5

Expected Output:

120

Code In Python

def factorial(): # Your code here return n = 5 print(factorial(n))

Output

Click Run Button to view compiled output

7. Find the sum of all numbers in a list

Required Input:

[1, 2, 3, 4, 5]

Expected Output:

15

Code In Python

def sum_of_list(): lst = [1, 2, 3, 4, 5] # Your code here return

Output

Click Run Button to view compiled output

8. Count the number of vowels in a string

Required Input:

education

Expected Output:

5

Code In Python

def count_vowels(): s = "education" # Your code here return

Output

Click Run Button to view compiled output

9. Generate the Fibonacci sequence up to n terms

Required Input:

5

Expected Output:

[0, 1, 1, 2, 3]

Code In Python

def fibonacci(): n = 5 # Your code here return

Output

Click Run Button to view compiled output

10. Find the second largest number in a list

Required Input:

[4, 3, 1, 2]

Expected Output:

3

Code In Python

def second_largest(): lst = [4, 3, 1, 2] # Your code here return

Output

Click Run Button to view compiled output

ad vertical

1 of 3