Python Program to Add Two Numbers – 2024

Hey guys, in this blog we will see a Python Program to Add Two Numbers in 2 ways.

The first way is where we will be giving the program two constants to add whereas in the second example we will take both the numbers as input from the user.

Example 1: Add Two Numbers

# Python Program to Add Two Numbers

num1 = 3.5
num2 = 4.2

# Adding both numbers
sum = num1 + num2

# Print the results
print(f'The sum of {num1} and {num2} is {sum}')

Output

The sum of 3.5 and 4.2 is 7.7

This program simply reads 2 constants provided by us (3.5 and 4.2), adds them, and simply displays the results using the print() statement.

In the print statement, we have used “f” before our string which says that format the variables (num1, num2, sum) in our string.

Example 2: Add Two Numbers with User Input

# Python Program to Add Two Numbers

num1 = input('Enter the first number -> ')
num2 = input('Enter the second number -> ')

# Adding both numbers
sum = float(num1) + float(num2)

# Print the results
print(f'The sum of {num1} and {num2} is {sum}')

Output

Enter the first number -> 2.3
Enter the second number -> 3.3
The sum of 2.3 and 3.3 is 5.6

This program is also almost the same as the above, except for the point that here we are taking numbers as input from the user. We take input from the users using the input() function in python.

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 520

Subscribe to our Newsletter

Subscribe to our newsletter and receive all latest projects...

Leave a Reply

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