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,
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
More Posts:
- How to open new Terminal using Bash Command - Bash
- Bash Command to Find String in a File - Bash
- Horizontally Center Align tag in HTML using CSS - CSS
- Find and Replace Multiple different words with the same word in a text file - NotepadPlusPlus
- How to Manage Profile Picture on Microsoft Teams - Teams
- Using Java 8 Month Enum with Examples - Java
- PowerShell: How to Write Output to a File - Powershell
- How to Find the Location of Notepad on Windows 11 - Windows