make a function in python

To create a function in Python, you can use the def keyword followed by the function name and parentheses () that may include input parameters. The code block within the function should be indented.

Here is an example of a simple function that takes in two variables and returns their sum:

main.py
def add_numbers(num1, num2):
    sum = num1 + num2
    return sum  
68 chars
4 lines

In this example, the def keyword is used to define the add_numbers() function. It accepts two input parameters num1 and num2, calculates their sum, and returns the result.

You can call this function by passing in desired values as input parameters:

main.py
result = add_numbers(2, 3)
print(result) # Output: 5
53 chars
3 lines

In this example, the add_numbers() function is called with values 2 and 3 and the returned result is stored in the result variable. The output of the function is printed to the console, which will display 5.

gistlibby LogSnag