Python program to add two numbers; Through this tutorial, i am going to show you how to add two numbers in python.
In this tutorial, i will use arithmetic addition operator + to add two numbers in python . The addition operator accepts two operands and returns the result of addition.
Python Program to Add Two Numbers
- 1: Simple Python Program to Add Two Numbers
- 2: Python Program to Add Two Numbers with User Input
- 3: Python Program to Add Two Numbers Using Functions
1: Simple Python Program to Add Two Numbers
a = 10 b = 20 sum = a + b print("sum:", sum)
Output
sum: 30
2: Python Program to Add Two Numbers with User Input
See the following python program to add two numbers from user input; as shown below:
a = int(input("enter first number: ")) b = int(input("enter second number: ")) sum = a + b print("sum:", sum)
Output
enter first number:10
enter second number:20
sum: 30
3: Python Program to Add Two Numbers Using Functions
See the python program to add two numbers using functions; as shown below:
# Program to illustrate # the use of user-defined functions def add_numbers(x,y): sum = x + y return sum num1 = 15 num2 = 20 print("The sum is", add_numbers(num1, num2))
Output
sum: 35
Recommended Python Tutorials
Recommended:-Python String Methods/Functions
Recommended:-List methods in Python
Recommended:-Python Remove First & Last Character from String
Recommended:-Python Convert String Lowercase to Uppercase
Recommended:-Python Program to Convert Uppercase to Lowercase
Recommended:-Python Program Find Length of String
Recommended:-Python Concatenate String and Variable (int float)
Recommended:-Python Trim String – rstrip(), lstrip(), strip()
Recommended:-Python Dictionary Methods with Example
Recommended:-Python Reverse String
Recommended:-Python String find() Method
Recommended:-Python Replace Character in String
Recommended:-Python Convert String to Float
Recommended:-Python String translate() Method Example
Recommended:-Python Convert String to List
Recommended:-Python filter() Method
Recommended:-Python Map() Method