Start learning free

LESSON 36 OF 410 · FUNCTIONS I

Exercise: Create Function in Python

Practice Read the full explanation

We've wrritten this code:

print("Hello AlgoCademy!")
print("Coding is amazing!")
print("Functions are so useful!")

print("Hello AlgoCademy!")
print("Coding is amazing!")
print("Functions are so useful!")

print("Hello AlgoCademy!")
print("Coding is amazing!")
print("Functions are so useful!")

Write the same code in a better way by creating and calling a function print_messages()

STEP 1 OF 5
    Ctrl + Enter to check

    PYTHON · FUNCTIONS I

    Exercise Create Function in Python, explained

    Understanding the Problem

    The core challenge here is to avoid repetitive code by using a function. Functions help in making the code more modular, readable, and maintainable. This problem is a basic exercise in understanding how to define and call functions in Python.

    Approach

    To solve this problem, we need to encapsulate the repeated print statements into a single function and then call this function multiple times. This approach reduces redundancy and makes the code cleaner.

    Naive Solution

    The naive solution is the one provided in the problem statement, where the same set of print statements is repeated multiple times. This is not optimal because it leads to code duplication and makes the code harder to maintain.

    Optimized Solution

    The optimized solution involves creating a function print_messages() that contains the print statements. We then call this function multiple times as needed. This approach is better because it adheres to the DRY (Don't Repeat Yourself) principle.

    Algorithm

    Here is a step-by-step breakdown of the optimized solution:

    1. Define a function named print_messages().
    2. Inside the function, include the three print statements.
    3. Call the print_messages() function three times.

    Code Implementation

    def print_messages():
        # Print the messages
        print("Hello AlgoCademy!")
        print("Coding is amazing!")
        print("Functions are so useful!")
    
    # Call the function three times
    print_messages()
    print_messages()
    print_messages()
    

    Complexity Analysis

    The time complexity of this solution is O(1) because the number of operations is constant and does not depend on any input size. The space complexity is also O(1) as we are not using any additional data structures.

    Edge Cases

    There are no significant edge cases for this problem since it involves simple print statements. However, if the function were to take parameters, we would need to consider cases where the parameters might be empty or invalid.

    Testing

    To test this solution, simply run the code and verify that the output matches the expected result:

    Hello AlgoCademy!
    Coding is amazing!
    Functions are so useful!
    Hello AlgoCademy!
    Coding is amazing!
    Functions are so useful!
    Hello AlgoCademy!
    Coding is amazing!
    Functions are so useful!
    

    Thinking and Problem-Solving Tips

    When approaching problems like this, always look for patterns and repetitions in the code. Functions are a powerful tool to encapsulate repeated logic, making your code more modular and easier to maintain. Practice by identifying repetitive code in your projects and refactoring it into functions.

    Conclusion

    In this exercise, we learned how to refactor repetitive code by using functions. This not only makes the code cleaner but also adheres to best practices in programming. Understanding and using functions effectively is a fundamental skill in any programming language.

    Additional Resources

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free