Learn how to add two number in python language in details:
In this article, you’ll learn how to add two number in python language without input from keyboard or input from the keyboard in details with explanation and example:
If you learn the earlier language like C, C++ then you easily observe that programming in python language is easier than another language that’s why many software developers choose python language to establish software. To learn more
Explanation:
- First, create two variables to store two number and initialize the value like num1=45 and num2=32 then
- Create the third variable to store the sum of two number
- int is a predefined data type, it means the given value is an integer and we also use float data types for real number.
- The print is a built-in function to display the result on the screen.
Example1: Write a program in python to add two number
Solution:
#python program to add two number
num1 = 45 num2 = 32 sum = int(num1) + int(num2) print(‘The sum of {0} and {1} is {2}’.format(num1,num2,sum)) |
The out of this program:
The sum of 45 and 32 is 77 |
Note: # is used as a comment which is ignored by the interpreted.
Example2: Write a program in python to add two number
Solution:
#python program to add two number
num1 = 55.23 num2 = 54.67 sum = float(num1) + float(num2) print(‘The sum of {0} and {1} is {2}’.format(num1,num2,sum)) |
The out of this program:
The sum of 55.23 and 54.67 is 109.9 |
Example3: Write a program in python to add two number, input is given by user from the keyboard
Solution:
#python program to add two number
num1 = input(‘Enter first number: ‘) num2 = input(‘Enter second number: ‘) sum = int(num1) + int(num2) print(‘The sum of {0} and {1} is {2}’.format(num1,num2,sum)) |
The output of this program:
Enter first number: 78 Enter second number: 54 The sum of 78 and 54 is 132 |
Note: input() is a build-in function, it use to take input from the user.
Example4: Write a program in python to add two number, input is given by user from the keyboard
Solution:
#python program to add two number
num1 = input(‘Enter first number: ‘) num2 = input(‘Enter second number: ‘) sum = float(num1) + float(num2) print(‘The sum of {0} and {1} is {2}’.format(num1,num2,sum)) |
The output of this program:
Enter first number: 78 Enter second number: 54 The sum of 58.32 and 45.65 is 103.97 |