Adding Two Numbers in Python: A Step-by-Step Guide. Welcome to the world of Python programming! In this tutorial, we’ll create a simple program that adds two numbers together. This is a great starting point for learning the basics of Python, including how to:
- Get input from the user.
- Store values in variables.
- Perform a basic arithmetic operation.
- Display the result.
Let’s dive in!
Table of Contents
Code for Adding Two Numbers in Python
Here’s the complete Python code for adding two numbers by taking input from user:
# Addition of two numbers in Python
num1 = int(input("Enter the first number "))
num2 = int(input("Enter the second number"))
sum = num1+num2
print(f"{num1} + {num2} = {sum}")
Now, let’s understand each part:
# Addition of two numbers in Python
- This line is a comment. In Python, any line starting with
#
is ignored by the interpreter. Comments are used to explain your code to yourself and others. They don’t affect how the program runs.
- This line is a comment. In Python, any line starting with
num1 = int(input("Enter the first number "))
- This line does a few things:
input("Enter the first number ")
: Theinput()
function displays the message “Enter the first number ” to the user and waits for them to type something and press Enter. Whatever they type is received as text (a “string”).int(...)
: Theint()
function converts the string that the user entered into an integer (a whole number). This is necessary because we want to do arithmetic with the numbers.num1 = ...
: The result of the conversion is stored in a variable namednum1
. A variable is like a container that holds a value.
- This line does a few things:
num2 = int(input("Enter the second number"))
- This line does the same as the previous line, but for the second number. It gets input from the user, converts it to an integer, and stores it in a variable called
num2
.
- This line does the same as the previous line, but for the second number. It gets input from the user, converts it to an integer, and stores it in a variable called
sum = num1 + num2
- This line performs the addition:
num1 + num2
: It takes the values stored in thenum1
andnum2
variables and adds them together.sum = ...
: The result of the addition is then stored in a new variable calledsum
.
- This line performs the addition:
print(f"{num1} + {num2} = {sum}")
- This line displays the result to the user using a formatted string (f-string):
f"..."
: This indicates that we’re using an f-string. F-strings allow us to embed variables directly inside a string by putting them in curly braces{}
.{num1}
,{num2}
, and{sum}
: These placeholders will be replaced with the values of those variables.- The entire string (like “5 + 10 = 15”) will be displayed on the console.
- This line displays the result to the user using a formatted string (f-string):
Putting it All Together
When you run this program, you’ll see these steps happen:
- The program will ask you to enter the first number.
- You’ll type in a number and press Enter.
- The program will then ask you to enter the second number.
- You’ll type in another number and press Enter.
- The program will add the two numbers together.
- Finally, it will display the addition equation and the sum in a user-friendly way.
Example Run
Let’s say you input 5
for the first number and 10
for the second number. The program will output:
5 + 10 = 15
How to Run the Python Addition Code
- Save the Code: Copy the code and paste it into a text editor like VS Code, Sublime Text, or Notepad. Save the file with a
.py
extension, for example,addition.py
. - Open a Terminal: Open your operating system’s terminal or command prompt.
- Navigate to the File: Use the
cd
command to navigate to the directory where you saved theaddition.py
file. - Run the Code: Type
python addition.py
and press Enter. The program will then execute and prompt for your input.
Video Guide
Key Concepts Learned
- Comments: Documenting your code using
#
. - Input: Getting data from the user using
input()
. - Data Types: Converting strings to integers using
int()
. - Variables: Storing values in named containers.
- Arithmetic Operations: Adding numbers using
+
. - Output: Displaying results using
print()
. - f-strings: A convenient way to format strings with variables.
What Next?
Congratulations, you’ve written and run your first Python program! Now you can explore more:
- Try different arithmetic operations like subtraction (
-
), multiplication (*
), and division (/
). - Experiment with more complex calculations.
- Learn about other data types like floats (decimal numbers) and strings (text).
Keep practicing, and you’ll become a Python pro in no time!
See Also
- TypeScript vs JavaScript: A Comprehensive Guide with Examples
- Setting Up TypeScript and Running Code
- A Detailed Guide to “Hello, World!” in TypeScript
- Binary Search in C: A Detailed Guide
- Python Interview Questions: 100+ Questions
- Writing Test Cases in Python Using Pytest
- React Components: Understanding Class-Based vs. Functional Approaches