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,
- How to Install Python Modules in VS Code
- 3 Python program to add two numbers
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: How to get Current Directory
- Python - Convert float to String
- Tkinter - add x and y padding to label text
- Check if String Contains a Substring - Python
- Read JSON File in Python Program
- How to Convert String to DateTime in Python
- Sorting an array using Bubble Sort in Python Programming
- Python Hello World! Program with code example (snippet)
- How to delete a dir or folder using Python code
- Change label (text) color in tkinter
- Set width and height for the label in tkinter
- Check version of pip package installer for Python
- Reading JSON file in Python with Examples
- Indent Python code in Notepad++
- tkinter - Hello World! Program
- How to resolve Failed to create interpreter PyCharm Error
- Base64 Encoding Decoding in Python Programming
- How to pip install Python Modules in VSCode
- How to take user input from the console in a Python program
- Take input argument from command line in Python Programming
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- Calculate discount amount python code
More Posts:
- How to check if a port is in use using terminal [Linux or macOS] - MacOS
- [Solution] com.amazonaws.dynamodb.v20120810 MissingAuthenticationToken Key Id or X.509 certificate - AWS
- Informal written computer correspondence acronyms list and meanings - 2022
- Android [SDK Manager] The system cannot find the path specified - Android-Studio
- Convert JSON String to Java GSON Object Example - Java
- How to rerun last command in Zsh shell - HowTos
- How to check the size of a Docker Container - Docker
- Have you noticed MacOS Ventura About this Mac look like Mac OS X - Mac-OS-X