In this tutorial, you will learn how to add two integers using the arithmetic operator along with other functions like the input function, format function, float function and print function of the python programming language.
How to Add Two Integers in Python?
Let’s take a look at the first source code , here the values are assigned in the code and the addition operator carries out the function.
RUN CODE SNIPPET# This program adds two numbers
num1 = 24
num2 = 8
# Adding two numbers
sum = num1 + num2
# Displaying the output value
print("The sum of {0} and {1} is {2}".format(num1, num2, sum))
OUTPUT:
The sum of 24 and 8 is 32
- At the start, we are declaring two variables
num1andnum2and assigning the values as24and8respectively. - We add two numbers
num1andnum2using the arithmetic operation+and storing the value in the variablesum. - Now, we display the output value using the
printand display the statement/string"The sum of {0} and {1} is {2}". format(num1, num2, sum). - In the above statement/string, the variables
{0},{1}and{2}will hold the valuesnum1,num2Â andsumwhere theformatfunction helps in variable substitution and data formatting.
Let’s take a look at the second source code , here the values are given as input by the user in the code and the addition operator carries out the function.
RUN CODE SNIPPET# Enter the input numbers from the user
num1 = input('\n Enter First number: ')
num2 = input('\n Enter Second number: ')
# Adding two numbers
sum = float(num1) + float(num2)
# Display the output value
print('\n The sum of {0} and {1} is {2}'.format(num1, num2, sum))
INPUT:
10.2 5.8
OUTPUT:
Enter First number: Enter Second number: The sum of 10.2 and 5.8 is 16.0
- Here we give the user the option to enter the values and the input values are scanned using the
inputfunction and are stored in two variables namelynum1andnum2Â with the statements/stringsEnter First number:andEnter Second number:withÂ\nrespectively. - In the STDIN section of the code editor the input values are entered.
- When adding the two numbers
num1andnum2using the arithmetic operation+, we declare the input values with theÂfloatfunction which converts a number stored in a string or integer into a floating point number, that is a number with a decimal point. We store the value in the variablesum. - As given in the first program, the variables
{0},{1}and{2}will hold the valuesnum1,num2Â andsumwhere theformatfunction helps in variable substitution and data formatting.
NOTE:
- The addition operator (+) is used to add integers in a program and the final value is stored in a variable named as sum.
- The variables used by the format function for substitution is enclosed in curly braces.
- The print statement is followed by a period, to initiate the format function.
- The print statement/string to be displayed in enclosed in double quotes.
- The statement for the input function are enclosed in single quotes and parenthesis.
- The \n in the code indicates a new line or the end of a statement line or a string.