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,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- apt-get list --installed packages in Ubuntu Linux - Ubuntu
- How to get unique values from a list in Python - Python
- SQL: Check if table exists - HowTos
- What is $$ in Bash Shell Script- Special Variable - Bash
- Open New tab using keyboard shortcut in Mac Terminal - Mac-OS-X
- W3 HTML validator warning Unable to Determine Parse Mode - Html
- Show Hidden Files in Mac Terminal - MacOS
- Disable Fading Edges Scroll Effect Android Views - Android