create a function that adds two numbers in python

To create a function that adds two numbers in Python, you can define a function with two parameters and return the sum of the two parameters:

main.py
def add_numbers(num1, num2):
    return num1 + num2
52 chars
3 lines

You can then call this function and pass in two numbers as arguments:

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

In this example, the add_numbers function takes two parameters num1 and num2, and returns the result of adding them together with the + operator. When the function is called with the arguments 2 and 3, the result 5 is returned and stored in the variable result, which is then printed to the console.

gistlibby LogSnag