Python Addition Two Number Example Tutorial

Hi Guys,
In this quick example, let's explore a beginner-friendly Python addition two number example tutorial. This guide will help you learn how to add two variables in Python using simple code.
We’ll walk you through two examples: one with hardcoded values and another using user input. This is an essential topic for Python beginners and a great way to get started with basic arithmetic operations in Python.
Let’s take a look at both examples below:
Example : 1 main.py# This program adds two numbers number1 = 4.5 number2 = 5.4 # Add two numbers total = number1 + number2 # Display the sum print('The sum of {0} and {1} is {2}'.format(number1, number2, total))Output :
The sum of 4.5 and 5.4 is 9.9Example : 2
In this second example, we’ll ask the user to input two numbers. These will be added together and the result printed using formatted string output.
main.py# Store input numbers number1 = input('Enter first number: ') number2 = input('Enter second number: ') # Add two numbers total = float(number1) + float(number2) # Display the sum print('The sum of {0} and {1} is {2}'.format(number1, number2, total))Output :
Enter first number: 25 Enter second number: 50 The sum of 25 and 50 is 75.0Run python file:
python3 main.pyOutput

I Hope It will help you....
Frequently Asked Questions (FAQs)
Q1: How to add two numbers in Python?
You can use the +
operator to add two numbers, like: total = a + b
.
Q2: How to take user input in Python?
Use input()
to take input from the user. For numerical input, convert it using float()
or int()
.
Q3: Can I add string inputs in Python?
If strings represent numbers, you can convert them using float()
or int()
. Otherwise, +
will concatenate them.
Q4: What is the output of 4.5 + 5.4 in Python?
The result is 9.9.
Q5: Will this code work in Python 2?
Yes, with slight changes in the print
syntax (use parentheses).