How To Add Numbers In Python?

Python is a powerful programming language that you can use to perform various tasks. In this guide, we will show you how to add numbers in Python.

Checkout this video:

Introduction

Python is a versatile language that you can use on the backend, frontend, or full stack of a web application. In this guide, you will learn how to add two numbers in Python.

Basic Syntax

Python is a widely used high-level interpreted language. Guido van Rossum developed Python in the late 1980s. It took 18 months to write the first version of Python. Python is named after the Monty Python’s Flying Circus comedy group. The first public release was in 1991.

Python is easy to learn for beginners and has many modules and libraries that allow for robust programming. Python is used in web development, scientific computing, artificial intelligence, deep learning, and data science.

In order to add two numbers in Python, we use the addition operator (+). This is the basic syntax:

num1 = 5
num2 = 10
result = num1 + num2

The print() Function

In Python, the print() function is used to print data to the standard output device (screen).

To print a string in Python 3, just write:
“`python
print(“this is a string”)
“`

If you want to print a string in Python 2, you have to write:
“`python
print “this is a string”
“`

In order to print numbers or other data types in Python 3, you have to use the placeholders %s for strings, %d for integers and %f for floating point numbers. You can also use %x for hexadecimal numbers. For example:
“`python
print(“this is an integer: %d” % 42) # prints “this is an integer 42”
print(“this is a floating point number: %f” % 42.0) # prints “this is a floating point number 42.0”
print(“this is a hexadecimal number: %x” % 0xff) # prints “thisis a hexadecimal number ff”

The input() Function

One of the most powerful functions in Python is the input() function. This function can be used to take input from the user and store it in any variable. All you need to do is specify the name of the variable in which you want to store the user input.

For example, let’s take a look at the following code:

num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))

# Adding two numbers
sum = num1 + num2

# Displaying the sum
print(“The sum of {0} and {1} is {2}” .format(num1, num2, sum))

In this code, we are taking two numbers as input from the user and storing them in variables ‘num1’ and ‘num2’. We are then adding these two numbers and storing the result in a variable ‘sum’. Finally, we are printing out the value stored in ‘sum’.

Variables

In Python 3, there are various ways to type integers. Decimal integers, such as 17 and 42, have type int, while floating-point numbers, like 3.14159 and 2.71828, have type float.

We can store integers in variables:
“`1742“` is an integer but it doesn’t have a type yet because it’s not stored in a variable. If we want to give this number a type, we can store it in a variable:
Type “`int_num = 1742“`, then hit Enter.

Strings

Strings are immutable. This means that once defined, they cannot be changed. If you need to change a string, you can create a new one from the existing string.

In Python, there are two number data types: integers and floating-point numbers or floats.Integers are whole numbers, negatives, and decimals.Floats are real numbers and can also be negative or positive decimals.

Lists

Lists are one of the four most popular data structures in Python. They are used to store an ordered collection of items, which can be of any type. You can create a list by placing items inside square brackets [ ] and separating them with commas. For example:

[1, 2, 3, 4]
[‘a’, ‘b’, ‘c’, ‘d’]
[True, False, True]
You can access individual items in a list by using their index. Indexes start at 0, so the first item in a list has an index of 0, the second item has an index of 1, and so on. For example:

my_list = [1, 2, 3]
my_list[0] # This will return 1 (the first item in the list)
my_list[1] # This will return 2 (the second item in the list)
my_list[2] # This will return 3 (the third item in the list)
You can also use negative indexes to access items from the end of the list. For example:

my_list = [1, 2, 3]
my_list[-1] # This will return 3 (the last item in the list)
my_list[-2] # This will return 2 (the second to last item in the list) my_list[-3] # This will return 1 (the third to last item in the list)

Tuples

Tuples are sequence types, just like lists. The difference between the two is that tuples are immutable; that is, you cannot change the values of a tuple once it has been created. You can, however, change the values of a list after it has been created.

To create a tuple, you use parentheses instead of square brackets. For example:
t1 = (1, 2, 3)
This creates a tuple with three values: 1, 2 and 3. You can access the values in a tuple using indexing, just like you would with a list. For example:
print(t1[0])
This would print out the value 1.

Dictionaries

A dictionary is a mutable data type in python. It is mainly used to store key-value pairs. A dictionary can store any kind of data such as integers, strings, float, etc. We can also store a list or another dictionary inside a dictionary.

Conclusion

In this tutorial, we learned how to add two numbers in Python using the addition (+) operator. We also looked at the built-in function sum(), which can be used to find the sum of a list of numbers. Finally, we saw how to use the += operator to add numbers to variables.

Scroll to Top