Write a program to add two number in python - Program to add two number in python - Python Programs - Python Programming
Python Program to Add Two Numbers
Here's a Python program to add two numbers:
def add_numbers(a, b):
"""
Returns the sum of two numbers a and b.
"""
return a + b
# example usage
num1 = 5
num2 = 10
result = add_numbers(num1, num2)
print(result) # 15
Here is the practical of the above program in Jupyter Notebook
The add_numbers function takes two numbers as input and returns their sum. The function simply adds the two numbers together using the + operator and returns the result.
In the example usage, we assign the values 5 and 10 to the variables num1 and num2, respectively. We then call the add_numbers function with these variables as arguments, and assign the result to the variable result. Finally, we print the value of the result, which is the sum of num1 and num2, i.e., 15.
Note that you can also add two numbers in Python using the + operator directly, without defining a function:
num1 = 5
num2 = 10
result = num1 + num2
print(result) # 15
This code produces the same output as the previous example, but without defining a separate function.
Comments
Post a Comment