Python Program to Add Two Numbers

Here you will get the simplest way to add two numbers in python by using the + operator. The program will first ask the user to enter two numbers, calculate their sum, and finally print it.

input() is an inbuilt function that is used to take input from the user.

Example 1: Without Using Function

In this example, we do not define any function. We are directly adding the numbers and then printing the result.

a = int(input("enter first number: "))
b = int(input("enter second number: "))

sum = a + b

print("sum:", sum)

Output

enter first number:5
enter second number:7
sum: 12

Example 2: Using Function

In this example, we will create a user-defined function sum(). It will take two numbers as arguments and return their sum.

def sum(n1,n2):
  return n1+n2

a = int(input("enter first number: "))
b = int(input("enter second number: "))

print("sum:", sum(a, b))

Output:

enter first number: -4
enter second number: 5
sum: 1

Video Tutorial

Here is the video that will help you learn the concept easily.

Need Python Programming Help? Visit AssignmentXp.com, they have expert python programmers available 24*7.

Python programming is an essential skill for many computer science fields.

However, it can be challenging to learn and complete programming assignments on your own. That’s why Python assignment help is available to make programming easier and less stressful.

Comment down below in case you are getting any problems with the sum of two numbers in python. You can also share your feedback or any queries.

15 thoughts on “Python Program to Add Two Numbers”

  1. Hiii am Arun student of Computer science means programmer plz help me am now biginner programmer but I take intereste in Programming world .But I don’t have any Senior and Coach .
    Can you help me

    1. Sure if you are going with programming you need to start from somewhere and i believe since you are cse students so you must be knowing oop so better is to start with java core and after that you will get an idea how and where to go. Perhaps with python ruby big data java script or anything you like

  2. Thanks so much, Neeraj! I really appreciate you helping out us fellow programmers with our coding, even though you don’t have to! πŸ™‚

  3. We need
    Nested For Loops
    Nested While Loop
    Function (1)
    Nested if
    If else if
    Class

    Just an examples with minimun 15 lines of codes

  4. Thank you developer of this site. This helped me a lot. Now I don’t have any problem in programming anymore. πŸ‘πŸ»πŸ‘πŸ»πŸ‘πŸ»

  5. Good day, I am learning Python programming, I have been asked to”
    Write a Python function Sum() to add two numbers
    For example:
    Test Result
    print(Sum(1,1)) 2
    print(Sum(1,2)) 3
    print(Sum(1,3)) 4

    This is the code which I wrote:
    num1=1
    num2=1
    result=num1+num2
    print(result)

    However, when I try to submit my results, I get the following answer:
    ***Run error***
    Traceback (most recent call last):
    File “__tester__.python3”, line 13, in
    print(Sum(1,1))
    NameError: name ‘Sum’ is not defined

    Please assist on how I can fix this error.

  6. @Hilda coombe
    you have not defined Sum anywhere. for ur given question u can try the following ways. (I am a beginner too)

    a=4
    b=5
    def Sum(a,b):
    print(a+b)
    Sum(a,b)

    #OR Other Way

    a=int(input(“please enter 1st value :”))
    b=int(input(“please enter 12nd value :”))
    def Sum(a,b):
    print(a+b)
    Sum(a,b)

Leave a Comment

Your email address will not be published. Required fields are marked *