Back to Python
2026-02-275 min read

Example 1: Add Two Numbers (Python Programming)

Learn Example 1: Add Two Numbers (Python Programming) step by step with clear examples and exercises.

Title: A full guide to Adding Two Numbers in Python for Beginners

Why This Matters

In this tutorial, we will delve into the fundamental concept of adding two numbers in Python. Mastering this skill is essential as it lays the foundation for more complex arithmetic operations and serves as a stepping stone for your programming journey. Understanding how to add numbers will also prove valuable during job interviews or real-life coding challenges where you may encounter basic mathematical tasks.

Additionally, learning to add numbers in Python provides an opportunity to explore various aspects of the language such as variables, operators, functions, and error handling. This knowledge will empower you to write more sophisticated programs and tackle a wide range of programming problems.

Prerequisites

Before proceeding, ensure that you have a solid understanding of the following Python concepts:

  1. Basic Python syntax and data types (e.g., integers, floats)
  2. Variables and their usage
  3. Operators such as + for addition, -, *, and / for subtraction, multiplication, and division, respectively
  4. Basic control structures like if statements and loops (e.g., for and while)
  5. Functions and their usage
  6. Error handling using try-except blocks
  7. Understanding the difference between assignment (=) and comparison (==) operators
  8. Knowledge of string concatenation and data types in Python

Core Concept

In Python, adding two numbers is simple with the help of the + operator. Here's a basic example:

num1 = 5
num2 = 7
sum = num1 + num2
print('The sum of', num1, 'and', num2, 'is', sum)

In this code snippet, we first declare two variables num1 and num2, assign them the values 5 and 7, respectively. Then, we use the + operator to add these numbers together and store the result in the sum variable. Finally, we display the sum using the print() function.

Variables and Data Types

In Python, variables are used to store data. In this example, we have two integer variables (num1 and num2) and one floating-point variable (sum). It's essential to understand that Python automatically determines the data type of a variable based on the value assigned to it.

Operators

The + operator is used for addition in Python. Other operators include:

  • - for subtraction
  • * for multiplication
  • / for division
  • % for modulus (remainder)
  • ** for exponentiation**

Worked Example

Let's dive into a more practical example where we take user input for the two numbers and calculate their sum:

Take user input for the first number

num1 = float(input('Enter the first number (integer or decimal): '))

Take user input for the second number

num2 = float(input('Enter the second number (integer or decimal): '))

Calculate the sum and display it

sum = num1 + num2

print('The sum of', num1, 'and', num2, 'is', sum)


In this example, we use the `input()` function to take user input for both numbers as either integers or decimals. We convert the input strings to floating-point numbers using the `float()` function and then calculate their sum. The result is displayed using the `print()` function.

Common Mistakes

  1. Not converting input to a number: If you forget to convert the user input to a number, you will encounter an error when trying to perform arithmetic operations. To avoid this mistake, always convert user input to the appropriate data type using functions like int() or float().
  1. Forgotten parentheses: Incorrect use of parentheses can lead to errors in your code. Make sure to use parentheses correctly and consistently.
  1. Not assigning the sum to a variable: If you forget to store the result of an operation in a variable, it will be lost when the program execution moves on to the next line. Always assign the result of operations to variables for further use or display.
  1. Using == instead of =: Be aware that Python uses == for comparison and = for assignment. Using == in place of = will not cause an error but may lead to unexpected results.
  1. Not handling errors: If the user enters non-numeric input, your program will throw an error. To handle such situations, use try-except blocks to catch and manage exceptions gracefully.
  1. String concatenation: Be careful not to accidentally concatenate numbers as strings when using the + operator. This can happen if one of the operands is a string or if you forget to convert user input to a number.

Common Mistakes - Subheadings

  • Not converting user input to a number
  • Incorrect use of parentheses
  • Not assigning the sum to a variable
  • Using == instead of =
  • Not handling errors
  • String concatenation

Practice Questions

  1. Write a Python script that takes user input for three numbers and calculates their average using a function called calculate_average().
  2. Modify the worked example so that it only accepts positive numbers from users, using a function called get_positive_number().
  3. Write a Python function called add_numbers() that adds two numbers and returns the result without using the print() function.
  4. Create a program that calculates the sum of all numbers in a list of integers using a function called sum_list().
  5. Implement an error-handling mechanism in the script for handling invalid user input (e.g., non-numeric values or negative numbers) and displaying helpful messages to guide users towards correct input.

FAQ

  1. Why do I get an error when trying to add strings instead of numbers?
  • You will encounter an error because you cannot perform arithmetic operations on strings. To avoid this, make sure that both operands are numbers before performing the addition.
  1. What happens if I try to add a string and a number in Python?
  • If you attempt to add a string and a number in Python, the interpreter will convert the number to a string first and then concatenate them. For example, '5' + 7 would result in '57'.
  1. Can I use other operators besides + for addition in Python?
  • No, there is no other operator for addition in Python. However, you can use the multiplication operator (*) to repeat a number multiple times, which can sometimes be used as an alternative to repeated addition. For example, 5 * 3 is equivalent to adding 5 + 5 + 5.
  1. What are some common errors I might encounter when trying to add numbers in Python?
  • Common errors include forgetting to convert user input to the appropriate data type, using == instead of =, and not handling exceptions gracefully. To avoid these issues, make sure you have a solid understanding of Python syntax and error handling techniques.
  1. What is string concatenation in Python?
  • String concatenation is the process of combining two or more strings together to create a new string. In Python, this can be achieved using the + operator. For example, 'Hello' + ' World!' would result in 'Hello World!'.
Example 1: Add Two Numbers (Python Programming) | Python | XQA Learn