In this program, you will learn to add two numbers and display it using print() function.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python Input, Output and Import
- Python Data Types
- Python Operators
In the program below, we've used the + operator to add two numbers.
Example 1: Add Two Numbers
# This program adds two numbers num1 = 1.5 num2 = 6.3 # Add two numbers sum = num1 + num2 # Display the sum print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))Output
The sum of 1.5 and 6.3 is 7.8The program below calculates the sum of two numbers entered by the user..
Example 2: Add Two Numbers With User Input
# Store input numbers num1 = input('Enter first number: ') num2 = input('Enter second number: ') # Add two numbers sum = float(num1) + float(num2) # Display the sum print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))Output
Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.8In this program, we asked the user to enter two numbers and this program displays the sum of two numbers entered by user.
We use the built-in function input() to take the input. Since, input() returns a string, we convert the string into number using the float() function. Then, the numbers are added.
Alternative to this, we can perform this addition in a single statement without using any variables as follows.
print('The sum is %.1f' %(float(input('Enter first number: ')) + float(input('Enter second number: '))))Output
Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.8Although this program uses no variable (memory efficient), it is harder to read.
The sum() function adds the items of an iterable and returns the sum.
Example
marks = [65, 71, 68, 74, 61]# find sum of all marks total_marks = sum(marks)
print(total_marks) # Output: 339sum() Syntax
The syntax of the sum() function is:
sum(iterable, start)The sum() function adds start and items of the given iterable from left to right.
sum() Parameters
- iterable - iterable (list, tuple, dict, etc). The items of the iterable should be numbers.
- start (optional) - this value is added to the sum of items of the iterable. The default value of start is 0 (if omitted)
sum() Return Value
sum() returns the sum of start and items of the given iterable.
Example: Working of Python sum()
numbers = [2.5, 3, 4, -5] # start parameter is not providednumbers_sum = sum(numbers)
print(numbers_sum) # start = 10numbers_sum = sum(numbers, 10)
print(numbers_sum)Output
4.5 14.5If you need to add floating-point numbers with exact precision, then you should use math.fsum(iterable) instead.
If you need to concatenate items of the given iterable (items must be strings), then you can use the join() method.
'string'.join(sequence)Visit this page to learn about, Python join() Method
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list.
Syntax:
sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable. If start is not given in the syntax , it is assumed to be 0.Possible two syntaxes:
sum(a) a is the list , it adds up all the numbers in the list a and takes start to be 0, so returning only the sum of the numbers in the list. sum(a, start) this returns the sum of the list + startBelow is the Python implementation of the sum()
Python3
numbers = [1,2,3,4,5,1,4,5]
Sum = sum(numbers)
print(Sum)
Sum = sum(numbers, 10)
print(Sum)
Output:
25 35Error and Exceptions
TypeError : This error is raised in the case when there is anything other than numbers in the list.
Python3
arr = ["a"]
Sum = sum(arr)
print(Sum)
Sum = sum(arr, 10)
print(Sum)
Runtime Error :
Traceback (most recent call last): File "/home/23f0f6c9e022aa96d6c560a7eb4cf387.py", line 6, in Sum = sum(arr) TypeError: unsupported operand type(s) for +: 'int' and 'str'Python3
numbers = [1,2,3,4,5,1,4,5]
Sum = sum(numbers)
average= Sum/len(numbers)
print (average)
Output:
3