How to Add Two Numbers in Python?

07-Oct-2022

.

Admin

How to Add Two Numbers in Python?

Hi Dev,

This article will provide some of the most important example How to Add Two Numbers in Python. let’s discuss about Addition of two numbers in Python. I explained simply about Python Program To Add Two Numbers With Code Examples. I explained simply about How to Print Sum of Two Numbers in Python. Here, Creating a basic example of Sum of Two Numbers - Python Example Program.

Python program to add two numbers; Through this tutorial, you will learn how to add two numbers using + operator in python program.

let's see below simple example with output:

Example 1: Simple Python Program to Add Two Numbers


# This program adds two variable

x = 10

y = 10

# Add two numbers

sum = x + y

# Display the sum

print("sum:", sum)

Output:

sum: 20

Example 2: Python Program to Add Two Numbers with User Input

# Store input numbers

x = int(input("enter first number: "))

y = int(input("enter second number: "))

# Add two numbers

sum = x + y

# Display the sum

print("sum:", sum)

Output:

enter first number:10

enter second number:10

sum: 20

Example 3: Python Program to Add Two Numbers Using Functions

# Program to illustrate

# the use of user-defined functions

def add_numbers(a,b):

sum = a + b

return sum

num1 = 10

num2 = 25

print("The sum is", add_numbers(num1, num2))

Output:

sum: 35

I hope it can help you...

#Python