30 Python Basic Exercises for Advanced with Solutions

Master advanced 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 professional-level 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. Find all Armstrong numbers between two given numbers

Required Input:

100, 500

Expected Output:

[153, 370, 371, 407]

Code In Python

def find_armstrong_numbers(): start, end = 100, 500 # Your code here return

Output

Click Run Button to view compiled output

2. Find all prime numbers in a range using the Sieve of Eratosthenes

Required Input:

50

Expected Output:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

Code In Python

def sieve_of_eratosthenes(): n = 50 # Your code here return

Output

Click Run Button to view compiled output

3. Implement a function to check if a string is a valid palindrome, ignoring spaces, punctuation, and case

Required Input:

A man, a plan, a canal: Panama

Expected Output:

True

Code In Python

def is_valid_palindrome(): s = "A man, a plan, a canal: Panama" # Your code here return

Output

Click Run Button to view compiled output

4. Write a function that returns the nth Fibonacci number using memoization

Required Input:

7

Expected Output:

13

Code In Python

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

Output

Click Run Button to view compiled output

5. Write a function to flatten a deeply nested list (with arbitrary levels of nesting)

Required Input:

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

Expected Output:

[1, 2, 3, 4, 5]

Code In Python

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

Output

Click Run Button to view compiled output

6. Write a function to rotate a matrix 90 degrees counterclockwise

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_counterclockwise(): matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Your code here return

Output

Click Run Button to view compiled output

7. Write a function to compute the power of a number using recursion (x^y)

Required Input:

2, 3

Expected Output:

8

Code In Python

def power_recursive(): x, y = 2, 3 # Your code here return

Output

Click Run Button to view compiled output

8. Write a function to find the greatest common divisor (GCD) of two numbers using recursion

Required Input:

48, 18

Expected Output:

6

Code In Python

def gcd_recursive(): a, b = 48, 18 # Your code here return

Output

Click Run Button to view compiled output

9. Write a function to compute the factorial of a number using tail recursion

Required Input:

5

Expected Output:

120

Code In Python

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

Output

Click Run Button to view compiled output

10. Write a function to compute the sum of all even Fibonacci numbers below a given number

Required Input:

100

Expected Output:

44

Code In Python

def sum_of_even_fibonacci(): n = 100 # Your code here return

Output

Click Run Button to view compiled output

ad vertical

1 of 3