Welcome back, budding Pythonistas! Now that you know how to store information using variables, let's explore how to manipulate that data using Python's arithmetic operators. These are the building blocks for performing calculations, just like you'd use in a math class, but now with the power to automate complex computations.
Python provides a familiar set of operators for performing basic arithmetic. Let's break them down:
- Addition (
+): This operator adds two operands (numbers or variables) together.
num1 = 10
num2 = 5
sum_result = num1 + num2
print(f"The sum is: {sum_result}")- Subtraction (
-): This operator subtracts the second operand from the first.
num1 = 10
num2 = 5
difference_result = num1 - num2
print(f"The difference is: {difference_result}")- Multiplication (
*): This operator multiplies two operands.
num1 = 10
num2 = 5
product_result = num1 * num2
print(f"The product is: {product_result}")- Division (
/): This operator divides the first operand by the second. In Python 3, this always results in a floating-point number (a number with a decimal).
num1 = 10
num2 = 5
division_result = num1 / num2
print(f"The division result is: {division_result}")- Floor Division (
//): This operator performs division but discards any fractional part, returning only the whole number (integer) result. It effectively rounds down to the nearest whole number.