Functions are the building blocks of modularity, allowing us to encapsulate specific tasks or computations into self-contained units of code. By writing functions, you can break down complex problems into smaller, more manageable pieces, enhancing both readability and maintainability.

We'll start with the basics of defining functions, exploring how to create them using the 'def' keyword, pass parameters, and return values. Gradually, we'll delve into more advanced concepts.

(Correction: addition = sum1 + sum2 should be addition = num1 + num2)

Let's look at an example:

def add(a, b):
    """Return the sum of two numbers."""
    return a + b

# Using the function
result = add(5, 3)
print(result)  # Output: 8

In this example:

Functions are a fundamental concept in Python and are essential for writing clean, organized, and reusable code.

Inbuilt functions

Here is a list of some of the in-built functions in Python, for reference:

  1. print() : Displays the given object to the console.

  2. len() : Returns the length (number of items) of an object like a string, list, or dictionary.

  3. type() : Returns the type of an object.

  4. int() : Converts a value to an integer.

  5. float() : Converts a value to a floating-point number.

  6. str() : Converts a value to a string.

  7. tuple() : Converts a value to a tuple.

  8. input() : Reads a line from input, converts it to a string.

  9. range() : Generates a sequence of numbers.

  10. sum() : Sums up the items of an iterable, like a list.

  11. max() : Returns the largest item in an iterable or two or more arguments.

  12. min() : Returns the smallest item in an iterable or two or more arguments.

  13. sorted() : Returns a new sorted list from the elements of any iterable.

  14. open() : Opens a file and returns a corresponding file object.

  15. abs() : Returns the absolute value of a number.

  16. round() : Rounds a number to a specified number of digits.

  17. enumerate() : Adds a counter to an iterable and returns it as an enumerate object.

  18. all() : Returns True if all elements of the iterable are true (or if the iterable is empty).

  19. any() : Returns True if any element of the iterable is true. If the iterable is empty, return False.

We will be gradually covering some of this during the course. You don't need to memorize these, but be aware that they exist, and you can refer to the Python documentation to learn more about them.

URL: Python Built-in Functions Documentation .