• Sheikh Aman

# Addition of two numbers in Python.

Updated: Aug 10, 2020

### Things you will learn now.

• Algorithm for adding two numbers.

• Addition of two numbers in python.

• Addition of two numbers with user input.

• Python program to add two numbers using class.

• Python program to add two numbers using the function.

• Some question for practice

"Addition of two numbers in python" today we will discuss this with a various number of example. To understand this context you should have basic knowledge on these topics:

If you don't know these terms then no need to worry just click on the name and you will get your requirements. So, without wasting any time lets jump to the topic.

### Algorithm for adding two numbers.

• Taking three variables example(num1, num2, sum)

• Assigning numerical values to num1, num2.

• Adding num1 and num2 with arithmetic operators("+").

• Assigning the added value to sum.

• Printing Sum.

### Addition of two numbers.

```number1 = 1.5
number2 = 6.3

# Add two numbers
sum = number1 + number2
# Display the sum
print(sum)```

Run this code to see the output

### Addition of two numbers with the user input.

```# Store input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# Add two numbers
sum = num1 + num2

# Display the sum with statement
print("the sum of "+num1+ " and "+num2+ " is " +sum+"." )
# without statement
print(sum)```

Run this code and see the output.

### Python program to add two numbers using class.

```class Test:

def findsum(self, a, b):
s = a + b
return s

a = int(input("Enter first number:"))
b = int(input("Enter second number:"))

obj = Test()
s = obj.findsum(a, b)

print("Sum is:", s)```

Run this code to see the output

### Python program to add two numbers using function.

```def addition(number1,number2):

return(number1 + number2)

#when i call this function with arguments it will print the addition of the two numbers

print(addition(10,15))```

Run this code to see the output

### Question for practising addition of two numbers.

• Write a program to print the output of 34 and 97.6

• Write a program to find the sum of class 10th and 12th percentage. (using class and function)

Write your code here.

If you love this tutorial then stay tuned for more interesting topics. Here we post topics related to machine learning, python etc.

22 views1 comment

See All