Python Program to Add Two Integers

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

  1. At the start, we are declaring two variables num1 and num2 and assigning the values as 24 and 8 respectively.
  2. We add two numbers num1 and num2 using the arithmetic operation + and storing the value in the variable sum.
  3. Now, we display the output value using the print and display the statement/string "The sum of {0} and {1} is {2}". format(num1, num2, sum).
  4. In the above statement/string, the variables {0} , {1} and {2} will hold the values num1, num2  and sum where the format function 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

  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in two variables namely num1 and num2  with the statements/strings Enter First number: and Enter Second number: with  \n respectively.
  2. In the STDIN section of the code editor the input values are entered.
  3. When adding the  two numbers num1 and num2 using the arithmetic operation + , we declare the input values with the  float function 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 variable sum.
  4. As given in the first program, the variables {0} , {1} and {2} will hold the values num1, num2  and sum where the format function 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.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...