How do you add two numbers in Python?
you can do addition in a different way like directly by adding to two or more numbers. you can also take user input value. if you take user input value then you have to do type conversion because by default it takes a string value. Another way is by using a loop method.
Example:
c = a + b
Add value in a single line
In this, we do not have to do more just use the print function and add the value.
print("Total sum is: ",4+5)
print("Total sum is: ",4.345+5.3422)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Total sum is: 9
Total sum is: 9.6872
Process finished with exit code 0
Program to add two constant number
In this program, we can take two variables and assign a value on them and perform addition.
#Addition of Integer value
a=5
b=10
print("Total sum of a and b is:",a+b)
#Addition of float value
a=5.234
b=10.432
print("Total sum of a and b is:",a+b)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Total sum of a and b is: 15
Total sum of a and b is: 15.666
Process finished with exit code 0
Addition of two user input Number
x=input("enter value of x:")
x=int(x) # Type connversion
y=input("enter value of y:")
y=int(y) # Type connversion
z=x+y # Adding X and Y
print("sum of x and y is:",z)# Result of Sum
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter value of x:4
enter value of y:7
sum of x and y is: 11
Process finished with exit code 0
Addition of Number using while loop
num=int(input("enter number:"))
# Here type conversing and taking input from user
total=0
i=1
while i <= num:
total+=i
i+=1
print("total sum is:",total)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter number:5
total sum is: 15
Process finished with exit code 0
Addition of Number using for loop
n = int(input("Enter number"))
sum = 0
# loop from 1 to n
for num in range(1, n + 1, 1):
sum = sum + num
print("Sum of first ", n, "numbers is: ", sum)
avg = sum / n
print("Average of ", n, "numbers is: ", avg)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Enter number5
Sum of first 5 numbers is: 15
Average of 5 numbers is: 3.0
Process finished with exit code 0
Recommended Post:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers
1 thought on “How to do Addition in python”