Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconHow to Write Python Program to Add Two Numbers? [With Examples]

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

Last updated:
29th Jan, 2021
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
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.

Top Data Science Skills to Learn to upskill

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?

Explore our Popular Data Science Online Courses

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

Read our popular Data Science Articles

upGrad’s Exclusive Data Science Webinar for you –

How upGrad helps for your Data Science Career?

 

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 🙂

Learn data science courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Profile

Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.

Frequently Asked Questions (FAQs)

1What are the pros and cons of programming in Python?

Python is an amazing language that has deep roots in development and is now gaining support in the programming domain. But along with its pros, there are some cons as well that must be considered before opting it out. The pros and cons are as follows:
Pros
1. The Python syntax is extremely user-friendly and easy to write. It is nearly equivalent to writing some sentences in the English language with indentation.
2. Problems that are solved in too many lines in other languages can be solved in less than half-line in Python with the help of various inbuilt functions.
3. The code is much cleaner and shorter as the programmer does not have to write much and most of the work is done by the compiler itself.
Cons
1. One of the biggest drawbacks of Python is its speed. It is very slow, slower than C++ and even Java.
2. Many programmers recommend avoiding Python because it spoils the logic building as most of the work is done by the inbuilt Python functions.

2What do you understand about the numeric data type in Python?

In Python, the numerical data or values are generally represented by the numeric data types. There can be three types of numeric values in Python which are as follows:
1. Integer
The integers like 3, 0, -100, -2, and 25 are represented by the int class. Unlike other programming languages like C++, there is no limit to how long an integer value can be. You can enter a value equivalent to the long long int of C++ in the int class in Python.
2. Float
The fractional or decimal numerical values are known as float and are represented by the float class. The scientific notation of a float value can be determined by appending “e” or “E” followed by the “+” or “-”.
3. Complex
The values that are in the form of “a+ib” where “a” is the real part and “b” is the imaginary part are considered as complex numbers and are represented by the complex class of Python.

3Name some best Python IDEs?

There are a lot of Python IDEs out there that are pretty decent. Some of these are PyCharm by JetBrains, Visual Studio Code by Microsoft, Spyder, Eclipse, Atom, PyDev, and Wing IDE.

Explore Free Courses

Suggested Blogs

Top 13 Highest Paying Data Science Jobs in India [A Complete Report]
905264
In this article, you will learn about Top 13 Highest Paying Data Science Jobs in India. Take a glimpse below. Data Analyst Data Scientist Machine
Read More

by Rohit Sharma

12 Apr 2024

Most Common PySpark Interview Questions & Answers [For Freshers & Experienced]
20925
Attending a PySpark interview and wondering what are all the questions and discussions you will go through? Before attending a PySpark interview, it’s
Read More

by Rohit Sharma

05 Mar 2024

Data Science for Beginners: A Comprehensive Guide
5068
Data science is an important part of many industries today. Having worked as a data scientist for several years, I have witnessed the massive amounts
Read More

by Harish K

28 Feb 2024

6 Best Data Science Institutes in 2024 (Detailed Guide)
5179
Data science training is one of the most hyped skills in today’s world. Based on my experience as a data scientist, it’s evident that we are in
Read More

by Harish K

28 Feb 2024

Data Science Course Fees: The Roadmap to Your Analytics Career
5075
A data science course syllabus covers several basic and advanced concepts of statistics, data analytics, machine learning, and programming languages.
Read More

by Harish K

28 Feb 2024

Inheritance in Python | Python Inheritance [With Example]
17647
Python is one of the most popular programming languages. Despite a transition full of ups and downs from the Python 2 version to Python 3, the Object-
Read More

by Rohan Vats

27 Feb 2024

Data Mining Architecture: Components, Types & Techniques
10803
Introduction Data mining is the process in which information that was previously unknown, which could be potentially very useful, is extracted from a
Read More

by Rohit Sharma

27 Feb 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
80775
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes
Read More

by Rohit Sharma

19 Feb 2024

Sorting in Data Structure: Categories & Types [With Examples]
139137
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier to search through it quickly and e
Read More

by Rohit Sharma

19 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon