To add numbers in Python, you use the +
operator. This operator works for both integers and floating-point numbers. The print()
function can then display the result.
Python Addition Basics
Here's a breakdown of how to perform addition in Python:
-
The
+
Operator:- Python uses the
+
symbol as the addition operator. - This operator combines two values, whether they are integers (e.g., 1, 2, 100) or floating-point numbers (e.g., 1.5, 3.14, -0.2).
- Python uses the
-
Operands:
- The values you are adding are called operands. They can be:
- Directly written numbers.
- Variables that hold numerical values.
- The values you are adding are called operands. They can be:
-
Printing the Result:
- To see the sum, you typically use the
print()
function. - This displays the outcome on the console.
- To see the sum, you typically use the
Examples of Python Addition
Let's look at some examples to illustrate these concepts.
Integer Addition
#Adding two integer
a = 10
b = 5
sum_result = a + b
print(sum_result) # Output: 15
print(12 + 7) #Output: 19
Float Addition
#Adding two floats
x = 3.14
y = 2.5
float_sum = x + y
print(float_sum) # Output: 5.64
Integer and Float Addition
#Adding an integer and a float
p = 10
q = 3.5
combined_sum = p + q
print(combined_sum) # Output: 13.5
Practical Insights
- Variable Usage: Using variables makes code more readable and reusable.
- Data Types: Python automatically handles the data type of the result (integer or float) based on the operands.
- Order of Operations: Like in mathematics, Python follows the order of operations (PEMDAS/BODMAS).
Operation | Symbol | Example |
---|---|---|
Addition | + | 3 + 5 |
Subtraction | - | 8 - 3 |
Multiplication | * | 4 * 6 |
Division | / | 10 / 2 |
Conclusion
In summary, Python uses the +
operator to add numbers. You can add integers, floating-point numbers, or a combination of both. The print()
function is used to display the results. This simple yet powerful operation is fundamental to mathematical computations in Python.