Home » Python » Python programs

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

**Adding two numbers in Python**: Here, we are going to learn **various ways to find the addition of two numbers in python**.

Submitted by Ankit Rai, on July 28, 2019

Take input of two numbers from the user and print their sum.

**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

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions