Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Python Program to Add Two Numbers

1 min read

Problem Definition

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

Solution

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

Program

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

Explanation

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.

Program

num_1 = 2.5
num_2 = 3.5
sum = num_1 + num_2

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

Output

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.

print(2.5+3.5)

Output

6.0


PROGRAMS

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read