Interview Questions, Answers and Tutorials

Set of practice exercises to reinforce understanding

Set of practice exercises to reinforce understanding

Hey there, future Python masters! 👋 Are you ready to dive into some exciting Python exercises? Great! In this post, we’re going to explore a set of practice exercises designed to reinforce your understanding of Python basics. Whether you’re just starting out or looking to brush up on your skills, these exercises will help you become a Python pro in no time!

Why Practice Exercises?

Practice makes perfect, right? Well, that’s especially true when it comes to learning Python (or any programming language, for that matter). By getting your hands dirty with code and solving real-world problems, you’ll not only solidify your understanding of Python concepts but also build the confidence to tackle more complex challenges down the road.

Let’s Get Started!

Below are some practice exercises along with Python code examples. Take your time to read through each exercise, try solving it on your own, and then compare your solution with the provided one. Remember, it’s okay if you get stuck! That’s all part of the learning process.

Exercise 1: Even or Odd?

Problem: Write a Python function that takes an integer as input and returns “Even” if the number is even, and “Odd” if the number is odd.

def even_or_odd(number):
    if number % 2 == 0:
        return "Even"
    else:
        return "Odd"

# Test the function
print(even_or_odd(5))  # Output: Odd
print(even_or_odd(10)) # Output: Even

Explanation: This function checks if the given number is divisible by 2. If it is, the number is even; otherwise, it’s odd.

Exercise 2: Sum of Digits

Problem: Write a Python function that takes an integer as input and returns the sum of its digits.

def sum_of_digits(number):
    total = 0
    while number > 0:
        total += number % 10
        number //= 10
    return total

# Test the function
print(sum_of_digits(123))  # Output: 6 (1 + 2 + 3)
print(sum_of_digits(987))  # Output: 24 (9 + 8 + 7)

Explanation: This function iteratively extracts the last digit of the number using the modulo operator % and adds it to the total. Then, it removes the last digit by performing integer division // by 10 until the number becomes 0.

Exercise 3: Palindrome Check

Problem: Write a Python function that takes a string as input and returns True if the string is a palindrome (reads the same forwards and backwards), and False otherwise.

def is_palindrome(string):
    return string == string[::-1]

# Test the function
print(is_palindrome("radar"))    # Output: True
print(is_palindrome("python"))   # Output: False

Explanation: This function compares the original string with its reverse (string[::-1]). If they are equal, the string is a palindrome.

Congratulations on completing these practice exercises! 🎉 We’ve covered some fundamental Python concepts while having fun along the way. Remember, practice is key to mastering any skill, so keep coding and exploring new challenges.

Feel free to modify these exercises or create your own. The more you code, the more confident you’ll become in your Python skills. Happy coding! 😊🐍