Python Program to Add Two Numbers

2 min read

Problem Definition

Create a Python program to add two numbers and print the result, taking runtime input from the user.


  1. Take input from the user save them in variables
  2. Add the numbers and print the result
  3. End


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2

print("The Sum is", sum)

Runtime Test Cases

Enter first number: 2
Enter second number: 4
The Sum is 6


The input() function takes the input from the user however we need to convert the input to an integer using int() method to perform mathematical operation i.e. addition(+).

This program simply takes two arbitrary inputs from the keyboard store them in two different variables later these numbers are added and stored in another variable sum which at the end gets printed out.

Problem Definition

Create a Python program to add two numbers.


num_1 = 2.5
num_2 = 3.5
sum = num_1 + num_2
print("Sum of {} and {} is {}".format(num_1, num_2,sum))


Sum of 2.5 and 3.5 is 6.0

First, the two numbers are stored in the variables num_1 and num_2, respectively. Addition in Python is done by ( + ) operator, Then the result is saved in the sum variable and printed out using string formatting.

To learn more about string formatting in Python read - How To Use String Formatting In Python

However, the more memory-efficient way to perform an addition in Python is by not using any variables at all in just one line, but it can make the code hard to read.





Latest Articles

Latest from djangocentral

Django 4.1 adds async-compatible interface to QuerySet

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - The Django core team has been progressively adding async suppor…
Read more →

3 min read

Making Django Admin Jazzy With django-jazzmin

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…
Read more →

4 min read