3 Python program to add two numbers


Adding two numbers in Python can be done in various ways, we will be covering 3 ways in which you can write the same program to add two numbers,


  1. Add Two Numbers in Python using two variables.
  2. Add Two Numbers in Python by numbers inputted by user in console.
  3. Add Two Numbers in Pyhton using a function.

Python add two numbers programs
Python add two numbers programs

Program 1: Add Two Numbers using two variables.

# PROGRAM 1:
# Add two numbers using two variables
# number1
# number2
# sumOfNumbers = Holds the sum of number1 and number2

number1 = 20
number2 = 30

sumOfNumbers = number1 + number2

print('Result: {0} + {1} = {2}'.format(number1, number2, sumOfNumbers))

Result: 20 + 30 = 50



Program 2: Add Two Numbers by numbers inputted by user in console

# PROGRAM 2:
# Add two numbers using two numbers inputted by
# user in the console
# number1 = holds the first number
# number2 = holds the second number
# sumOfNumbers = Holds the sum of number1 and number2

print('This Program Adds Two Numbers inputted by the user:')

print('Enter 1st Number:')
number1 = int(input())

print('Enter 2nt Number:')
number2 = int(input())

sumOfNumbers = number1 + number2

print('Sum: {0} + {1} = {2}'.format(number1, number2, sumOfNumbers))

This Program Adds Two Numbers inputted by the user:
Enter 1st Number:
20
Enter 2nt Number:
80
Sum: 20 + 80 = 100

Process finished with exit code 0



Program 3: Add Two Numbers by numbers inputted by user in console

# PROGRAM 3:
# Add two numbers using a function
# number1 = holds the first number
# number2 = holds the second number
# sum_of_numbers = Holds the sum of number1 and number2

def add_two_numbers(n1, n2):
    sum_of_numbers = n1 + n2
    print('Sum: {0} + {1} = {2}'.format(n1, n2, sum_of_numbers))


number1 = 30
number2 = 40
add_two_numbers(number1, number2)

Sum: 30 + 40 = 70

Program 1
Program 1
Program 2
Program 2
Program 3
Program 3


Have Questions? Post them here!





Hello there!,

You are using AdBlocker!

I am an independent developer trying to help fellow developers and students to resolve issues faced on a day-to-day basis, we 'Code to Care' . There is a lot of expenses involved in hosting and managing a website that's the reason we have ads.

A humble request to you to disable adBlocker on Code2care.org and support us pay our bills.

Thanks,
Cod2care - Line's of code for change.

To see contents reload page after unblocking
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities. Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, Tips-n-tricks, How Tos, Blogs, Articles, etc. on various platforms like Windows, Mac, Linux, Mobile platforms, etc. Technologies/Languages like Java, Objective-C, PHP, .Net, Android, SharePoint, jQuery, HTML, CSS, etc.