Programs

How to Write Python Program to Add Two Numbers? [With Examples]

Introduction

Python has gained a lot of spotlights because of its salient features, easy syntax, good readability, availability of various libraries and APIs.

Now every computer geek wants to learn this trending language, but there may be a question “how to start?”. Well, the answer to that question would be having a look at python documentation and knowing the syntax of python.

After learning all the rules and syntax of python our mind may urge us to implement a trivial or hello world program in python. Now let’s satisfy the thirst of our enthusiastic mind by having a look at a trivial code, which is adding two numbers in python.

Code Walkthrough

Let’s have a look at different possible implementations of this code.

1. Addition of Two Numbers

Let’s demonstrate the code of adding two user-defined numbers.

i = 5
j = 6
sum = i+j
print(“sum of”,i,“and”,j,“is”,sum) #line1
print(“sum of”,i,“and”,j,“is”,i+j) #line2

One of the unique features in python is we don’t need to specify the datatype of the variable and we can directly allocate the variable with a value, and python does the hard work for you.

In the above code variables i and j stores the values which are to be added and the sum variable stores the value of i+j. And then we can print that value. Also here’s a point to be noted, if we don’t want a variable to store the answer we can directly calculate the answer in the print statement itself (as shown in line2).

But what if we want to add numbers that are given as input from the user?

2. Addition of User Input Numbers

This code will be similar to the previous code but the only difference would be assigning the variables with user input. Let’s have a look at the code.

i = input(” enter the first number “)
j = input(” enter the second number “)
sum = i+j
print(“sum of”,i,”and”,j,”is”,sum)
print(“sum of”,i,”and”,j,”is”,i+j)

In the above code, the first line would prompt “enter the first number” in the output screen and the terminal or the console expects an input that will be assigned to the variable i. similarly, the same process will be observed for line 2. Now both the variables i, j are assigned with the user input, and the sum is stored in the sum variable. Feels like a cakewalk right?

3. Addition of Two Float Numbers

Many times we may face a situation to add numbers with decimal precision, lets have a look at that implementation!

i = 1.5223
j = 1.8365
sum = i+j
print(“sum of”,i,”and”,j,”is”,sum)  #line1
print(“sum of”,i,”and”,j,”is”,i+j)  #line2

As already discussed we don’t need to mention the data type of the variable we are going to use. Variables i, j are treated as float values and the final result obtained will have decimal precision.

We can also typecast an integer to a float in python, let’s demonstrate the difference between integer addition and float addition.

i = 3
j = 6
sum = float(i)+float(j) #line1
print(“sum of”,i,”and”,j,”is”,sum) #line2
print(“sum of”,i,”and”,j,”is”,i+j) #line3print(float(i+j)) #line4

In the above snippet, we have two variables i, j which are assigned with two integers. Now if we add these two variables the answer would be of integer type. We can have the answer in float datatype by typecasting the variables into a float (as shown in line 1) or typecasting the answer into the float (as shown in line 4).

The main difference between line 2 and line 3 would be the lack of decimal precision in line 3. The output of line 2 will be 8.0 whereas the output of line 3 will be 8, so if we want to maintain the decimal precision of a variable then we need to use a float datatype.

Also Read: Python Project Ideas & Topics For Beginners

Conclusion 

We have walked through various code snippets where we’ve seen the addition of two user-defined numbers, the addition of user input numbers, the addition of float numbers, observed decimal precision of result with float datatype, and typecasting the numbers in python.

Now that you are aware of how to add two numbers in python, try writing the code on your own and try modifying the code with various data types. And try performing a few other trivial tasks in python and explore the fun in python programming 🙂

I hope you will learn a lot while working on this blog. If you are curious about learning data science to be in the front of fast-paced technological advancements, check out upGrad & IIIT-B’s PG Diploma in Data Science and upskill yourself for the future.

Prepare for a Career of the Future

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN DATA SCIENCE
Learn More

Leave a comment

Your email address will not be published.

Accelerate Your Career with upGrad

Our Popular Data Science Course

×