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.
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
Have Questions? Post them here!
More Posts related to Python,
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
More Posts:
- Validate email address in Python using regular expression (regex) - Python
- How to Setup AWS Credentials using Visual Studio Code - AWS
- Which Python version is bundled with macOS Sonoma 14 by default - MacOS
- Nano Show Line Numbers - Linux
- How to Know the Line Number on Windows Notepad App - Windows-11
- [Fix] Springboot: Web application could not be started as there was no ServletWebServerFactory bean defined in the context. - Java
- macOS 13 Ventura - The New About this Mac Window - MacOS
- Java 8: Find the Max value in a List - Java