Chapter 4: Making Python Work - Operators
In Chapter 3, you learned how to store information in variables. Now it's time to learn how to do things with that information. Whether you want to perform calculations, compare values, or combine text, you'll need to use operators.
Operators are special symbols in Python that carry out an operation on one or more values (called operands). For example, in the expression 10 + 5, 10 and 5 are the operands, and + is the addition operator.
Let's explore the most common types of operators you'll be using.
Arithmetic Operators
These are the operators you're likely most familiar with from math class. They let you perform mathematical calculations. Let's assume we have two variables for our examples: a = 15 and b = 4.
+
Addition
Adds two operands
a + b
19
-
Subtraction
Subtracts the right operand from the left operand
a - b
11
*
Multiplication
Multiplies two operands
a * b
60
/
Division
Divides the left operand by the right. The result is always a float.
a / b
3.75
%
Modulus
Returns the remainder of the division
a % b
3
**
Exponentiation
Raises the left operand to the power of the right operand
a ** b
50625
//
Floor Division
Divides and returns the whole number result (rounds down).
a // b
3
Here are these operators in action in a Python script:
a = 15
b = 4
# Basic arithmetic
print("a + b =", a + b)
print("a - b =", a - b)
print("a * b =", a * b)
# Division
print("a / b =", a / b) # Classic division (always a float)
print("a // b =", a // b) # Floor division (result is an integer)
print("a % b =", a % b) # Modulus (the remainder)
# Exponentiation (a to the power of b)
print("a ** b =", a ** b)
Order of Operations Python follows the standard mathematical order of operations, often remembered by the acronym PEMDAS:
Parentheses
()Exponents
*Multiplication , Division
/, Floor Division//, Modulus%(from left to right)Addition
+, Subtraction (from left to right)
Assignment Operators
You've already used the most basic assignment operator, =. Assignment operators are used to assign values to variables. There are also "compound" assignment operators that combine an arithmetic operation with an assignment.
Let's start with a variable x = 10.
=
x = 10
x = 10
Assigns the value on the right to the variable on the left.
+=
x += 5
x = x + 5
Adds the right value to the variable and reassigns the result.
-=
x -= 5
x = x - 5
Subtracts the right value and reassigns.
*=
x *= 5
x = x * 5
Multiplies by the right value and reassigns.
/=
x /= 5
x = x / 5
Divides by the right value and reassigns.
%=
x %= 3
x = x % 3
Performs modulus and reassigns the remainder.
//=
x //= 3
x = x // 3
Performs floor division and reassigns.
**=
x **= 2
x = x ** 2
Raises to a power and reassigns.
These are useful shortcuts that make your code cleaner and more concise.
Comparison Operators
Comparison operators are used to compare two values. The result of a comparison is always a Boolean value: True or False. These are essential for making decisions in your code.
Let's use x = 10 and y = 12 for our examples.
==
Equal to
x == y
False
Returns True if the operands are equal.
!=
Not equal to
x != y
True
Returns True if the operands are not equal.
>
Greater than
x > y
False
Returns True if the left operand is greater.
<
Less than
x < y
True
Returns True if the left operand is smaller.
>=
Greater than or equal to
x >= y
False
Returns True if the left is greater or equal.
<=
Less than or equal to
x <= y
True
Returns True if the left is smaller or equal.
Important: Don't confuse the assignment operator (=) with the "equal to" comparison operator (==).
my_variable = 5assigns the value 5 tomy_variable.my_variable == 5checks ifmy_variableis equal to 5 and returnsTrueorFalse.
Logical Operators
Logical operators are used to combine Boolean expressions. They allow you to test multiple conditions at once.
and
Returns True if both statements are true.
(5 > 3) and (2 < 4)
True
or
Returns True if at least one of the statements is true.
(5 > 3) or (2 > 4)
True
not
Reverses the result; returns False if the result is true, and vice-versa.
not(5 > 3)
False
Logical operators are incredibly powerful when you need to make more complex decisions.
Summary and What's Next
You've just learned a crucial part of programming: how to use operators to manipulate and compare data.
Arithmetic Operators (
+, , ,/,%,*,//) for calculations.Assignment Operators (
=,+=,=, etc.) for assigning and updating values.Comparison Operators (
==,!=,>,<, etc.) for comparing values to get aTrueorFalseresult.Logical Operators (
and,or,not) for combiningTrue/Falseconditions.
These tools are the building blocks for creating logic in your programs. Now that you can store data (variables) and compare it (operators), you're ready for the next giant leap: Control Flow. In the next chapter, you'll learn how to make your programs make decisions and run different pieces of code based on the conditions you've learned to create here.
Practice Time!
Predict the output of
10 + 3 * 2 ** 2. Then, type it into a Python shell to check your answer.Create a variable
temp_celsius = 25. Write a formula using an arithmetic operator to convert it to Fahrenheit and print the result. (Formula: F = C * 9/5 + 32).Create two variables,
password = "secret"andusername = "admin". Write a comparison to check if thepasswordvariable is equal to"secret". Print theTrue/Falseresult.Write a logical expression that checks if a student has passed. A student passes if their
gradeis80or higherandthey havecompleted_homework. Create variables forgradeandcompleted_homeworkto test it.
Last updated