30 Python Basic Exercises for Intermediate with Solutions

Master intermediate 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 advanced 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, string manipulations, and data structures like lists, sets, tuples, 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. Check if all elements in a list are unique

Required Input:

[1, 2, 3, 4]

Expected Output:

True

Code In Python

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

Output

Click Run Button to view compiled output

2. Find the product of all elements in a list

Required Input:

[1, 2, 3, 4]

Expected Output:

24

Code In Python

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

Output

Click Run Button to view compiled output

3. Count the occurrences of each element in a list

Required Input:

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

Expected Output:

{1: 1, 2: 2, 3: 3}

Code In Python

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

Output

Click Run Button to view compiled output

4. Find the sum of the digits of a number until the result has only one digit

Required Input:

1234

Expected Output:

1

Code In Python

def sum_of_digits_until_one(): n = 1234 # Your code here return

Output

Click Run Button to view compiled output

5. Find the number of words in a sentence

Required Input:

Python programming is fun

Expected Output:

4

Code In Python

def count_words(): sentence = "Python programming is fun" # Your code here return

Output

Click Run Button to view compiled output

6. Find the second most frequent character in a string

Required Input:

success

Expected Output:

c

Code In Python

def second_most_frequent(): s = "success" # Your code here return

Output

Click Run Button to view compiled output

7. Check if two lists are identical

Required Input:

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

Expected Output:

True

Code In Python

def check_identical_lists(): lst1 = [1, 2, 3] lst2 = [1, 2, 3] # Your code here return

Output

Click Run Button to view compiled output

8. Rotate a matrix 90 degrees clockwise

Required Input:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Expected Output:

[[7, 4, 1], [8, 5, 2], [9, 6, 3]]

Code In Python

def rotate_matrix(): matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Your code here return

Output

Click Run Button to view compiled output

9. Remove all spaces from a string

Required Input:

hello world

Expected Output:

helloworld

Code In Python

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

Output

Click Run Button to view compiled output

10. Capitalize the first letter of each word in a string

Required Input:

python is fun

Expected Output:

Python Is Fun

Code In Python

def capitalize_words(): s = "python is fun" # Your code here return

Output

Click Run Button to view compiled output

ad vertical

1 of 3