Q:

Python program to find addition of two numbers (4 different ways)

belongs to collection: Python basic programs

0

Python program to find addition of two numbers (4 different ways)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Example:

    Input:
    A = 2, B = 3

    Output:
    Sum = 5

Here, we are implementing the program to find the addition of two numbers using 4 different ways.

1) Simply take input from the user and typecast to an integer at the same time after that performing addition operation on the both number.

if __name__ == "__main__" :

    # take input from user
    a = int(input())
    b = int(input())

    # addition operation perform
    sum_num = a + b
    
    print("sum of two number is: ",sum_num)

Output

10
20
sum of two number is:  30

2) Using a user-defined function for doing the sum of two numbers.

# define a function for performing 
# addition of number
def sum_num(a,b) :
    
    return a + b

# Main code
if __name__ == "__main__" :

    a = int(input())
    b = int(input())

    print("sum of two number:",sum_num(a,b))

Output

10
20
sum of two number: 30

3) We are taking the input from user in one line after that typecast into an integer and stored them in the list then use sum() inbuilt function which returns the sum of elements of the list.

if __name__ == "__main__" :    
	# take input from the user in list
	a = list(map(int,input().split()))

	# sum function return sum of elements 
	# present in the list
	print("sum of two number is:",sum(a))

Output

10 20
sum of two number is: 30

4) We are taking the input from user in one line and store them in two different variables then typecast both into an integer at the time of addition operation.

if __name__ == "__main__" :
    
    # take input from the user in a and b variables
    a,b = input().split()

    # perform addition operation
    rslt = int(a) + int(b)
    
    print("sum of two number is:",rslt)

Output

10 20
sum of two number is: 30

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python Arithmetic Operators Example... >>
<< Python program to find sum of two numbers...