Adding Two Numbers in Python: A Step-by-Step Guide

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!

adding two numbers in python

Code for Adding Two Numbers in Python

Here’s the complete Python code for adding two numbers by taking input from user:

Now, let’s understand each part:

  1. # 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.
  2. num1 = int(input("Enter the first number "))
    • This line does a few things:
      • input("Enter the first number "): The input() 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(...): The int() 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 named num1. A variable is like a container that holds a value.
  3. 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.
  4. sum = num1 + num2
    • This line performs the addition:
      • num1 + num2: It takes the values stored in the num1 and num2 variables and adds them together.
      • sum = ...: The result of the addition is then stored in a new variable called sum.
  5. 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.

Putting it All Together

When you run this program, you’ll see these steps happen:

  1. The program will ask you to enter the first number.
  2. You’ll type in a number and press Enter.
  3. The program will then ask you to enter the second number.
  4. You’ll type in another number and press Enter.
  5. The program will add the two numbers together.
  6. 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:

How to Run the Python Addition Code

  1. 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.
  2. Open a Terminal: Open your operating system’s terminal or command prompt.
  3. Navigate to the File: Use the cd command to navigate to the directory where you saved the addition.py file.
  4. 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

Leave a Comment