# How to do Addition in python

Contents

## 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.

a=5
b=10
print("Total sum of a and b is:",a+b)
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