Adding two numbers in Python can be done in various ways, we will be covering 3 ways in which you can write the same program to add two numbers,
- Add Two Numbers in Python using two variables.
- Add Two Numbers in Python by numbers inputted by user in console.
- Add Two Numbers in Pyhton using a function.

Python add two numbers programs
Program 1: Add Two Numbers using two variables.
# PROGRAM 1:
# Add two numbers using two variables
# number1
# number2
# sumOfNumbers = Holds the sum of number1 and number2
number1 = 20
number2 = 30
sumOfNumbers = number1 + number2
print('Result: {0} + {1} = {2}'.format(number1, number2, sumOfNumbers))
Result: 20 + 30 = 50
Program 2: Add Two Numbers by numbers inputted by user in console
# PROGRAM 2:
# Add two numbers using two numbers inputted by
# user in the console
# number1 = holds the first number
# number2 = holds the second number
# sumOfNumbers = Holds the sum of number1 and number2
print('This Program Adds Two Numbers inputted by the user:')
print('Enter 1st Number:')
number1 = int(input())
print('Enter 2nt Number:')
number2 = int(input())
sumOfNumbers = number1 + number2
print('Sum: {0} + {1} = {2}'.format(number1, number2, sumOfNumbers))
This Program Adds Two Numbers inputted by the user:
Enter 1st Number:
20
Enter 2nt Number:
80
Sum: 20 + 80 = 100
Process finished with exit code 0
Program 3: Add Two Numbers by numbers inputted by user in console
# PROGRAM 3:
# Add two numbers using a function
# number1 = holds the first number
# number2 = holds the second number
# sum_of_numbers = Holds the sum of number1 and number2
def add_two_numbers(n1, n2):
sum_of_numbers = n1 + n2
print('Sum: {0} + {1} = {2}'.format(n1, n2, sum_of_numbers))
number1 = 30
number2 = 40
add_two_numbers(number1, number2)
Sum: 30 + 40 = 70

Program 1

Program 2

Program 3
Have Questions? Post them here!
More Posts related to Python,
- Read JSON File in Python Program
- Convert Float to String in Python
- Python - Convert float to String
- Check if String Contains a Substring - Python
- Install and Run Jupyter Notebook on Mac (macOS)
- Sorting an array using Bubble Sort in Python Programming
- Comments in Python Programming
- 3 Python program to add two numbers
- Read a file line by line in Python Program
- How to uninstall pip Python packages
- Change label (text) color in tkinter
- 7 Python Arithmetic Operators with Examples [Tutorial]
- pip get list of all outdated Python packages
- Where does brew install python in macOS
- How to Convert String to DateTime in Python
- Python: Fix command not found pip or pip3 on zsh shell
- How to add borders to tkinter label text
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- TypeError: must be str, not int
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- Check version of pip package installer for Python
- Change the background of Tkinter label or text
- Set width and height for the label in tkinter
- How to write JSON file in Python Program
- Python raise error with message example
More Posts:
- sudo is not recognized as an internal or external command - Windows
- Spotify is down for iOS and Android globally - error no internet connection available, something went wrong - News
- Unable to establish connection to adb : Android Studio Error - Android
- How to Use Command Prompt on a Mac? - MacOS
- Force convert HTML text input to upper case - Html
- Tutorial : Simple Lightweight Pure CSS based Vertical Navigation Menu - CSS
- Hide Navigation Bar from Android Screen Activity - Android
- SQL: Check if table exists - HowTos