Python Examples

Python Program to Add Two Numbers

The addition of numbers or addition of elements of a list is the most commonly used in any project or a program. It is the most basic operation that is applicable in most of the programs. Big or small. Python addition provides a lot of functions inbuilt or user-defined that allow addition.

There are multiple ways in which we can add elements using python, For example, The addition of natural numbers or two numbers entered by the user, etc.

Addition in python can be done using various methods. Addition of two integer values. Finding the sum of natural numbers. We can Find the sum of even numbers or odd numbers and much more.

Python also provides inbuilt functions that help in addition. If we want to add elements to the list we can use the sum() function. They add the elements to the list. We can pass the variable name to the function, And it will return the sum of all the elements of the list.

python addition

                                  Source: Wikipedia

Source code: Python program to add two numbers

                    

#Add two numbers

num1=7
num2=10
addition=(num1+num2)
print("First number :",num1)
print("Second number :",num2)
print("Addition : ",addition)

Output:

                    

First number: 7
Second number: 10
Addition : 17

Source code: Python program for the addition of two numbers with user input. 

                    

#Addition of two numbers inputted by a user

num1=int(input("Enter first no. :"))
num2=int(input("Enter second no. :"))
addition=(num1+num2)

#displaying the user input values
print("First number is :",num1)
print("Second number is :",num2)

#displaying addition of user input
print("Addition is : ",addition)

Output:

                    

Enter first no. :5
Enter second no. :10
First number is: 5
Second number is: 10
Addition is: 15

Questions

How do you add a sum in python?

The Python sum() function calculates the sum of elements of the iterable. Iterable can be a list, a tuple, or a dictionary. 

sum() function works with both integers and floating-point numbers. It has an optional parameter to add a number to the total that is called as start value.

Syntax:

sum(iterable, start)  

Iterable may be anything like lists, tuples, or dictionaries.

Start is the value which is added to the sum of the elements of the iterable.

For example:

# sum() function in python

#defining a list

The_list = [1,2,3,4,5,1,4,5]

print(“sum of all the elements of the list : “, sum(The_list))

# passing start parameter = 10

print(“addition after start parameter is 10: “, sum(The_list,10))

Output:

The addition of elements of a list is: 25

addition after start parameter is 10: 35

How do you add two numbers in python?

We can add two numbers in python by using the arithmetic operator ‘+’

The operand or the values to be added can be integer values or floating-point values. The ‘+’ operator adds the values. We can use the print statement to return the addition of values to the output window.

For example:

print(5+5)

the above code line returns the value 10 as output

we can also assign values to variables and add two variables using the + operator.

The variable data type should be either be integer or floating-point.

For example:

num1=10

num2=12

print(num1+num2)

The above code returns the value 22.

How do you add things together in python?

Python can be used to add numbers but also for various other things. It can be used to add things like adding items to a list. Appending elements to a tuple or a dictionary.

Python provides an inbuilt append() function to add items or elements to a list.

The append() function in python adds a single item to the existing list. It will not return a new list of Items but will modify the original list by adding the items to the end of the list.

Syntax:

list_name.append(item)

The append() function takes a single item as an input parameter and adds that to the end of the list.

The elements inside a list can be numbers, strings, another list, dictionary.

For example:
                    

#Adding an item to the list

my_list = ['toppr', 'is', 'informative']
my_list.append("the best")
print(my_list)

Output:
                    

['toppr', 'is', 'informative', 'the best']

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.