Python Program to Add Two Numbers – 2022

Machine Learning Projects

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

Check out our other python programming examples

Leave a Comment

Your email address will not be published.