03. Operators
undefined Operators are not exclusive to programming. We use them every day in mathematics and logic. In Python, operators allow us to perform calculations, compare values, assign data to variables, and build logical expressions. In this article, we will focus on the operators that you will use most often throughout this course.
Arithmetic Operators
Arithmetic operators come from mathematics and are used to perform calculations with numeric values.
| Operator | Name | Example |
|---|---|---|
| + | Addition | x + y |
| - | Subtraction | x - y |
| * | Multiplication | x * y |
| / | Division | x / y |
| % | Module | x % y |
| ** | Exponentiation | x ** y |
| // | Floor division | x // y |
undefined Examples
x = 10
y = 3
print(x + y) # 13
print(x - y) # 7
print(x * y) # 30
print(x / y) # 3.3333333333333335
print(x % y) # 1
print(x ** y) # 1000
print(x // y) # 3
Notice that / always return a float number, while // performs a floor division and returns the integer part of the result.
Another interesting thing is that the % operator returns the remainder of a division and is commonly used to discover if a number is odd number or not.
Assignment Operators
Assignment operators are used to assign values to variables. So in the table below, you can see that we can shorten the expression using those operators, and in the last column you can see what each operator means compared to the same expression without the operator.
| Operator | Example | Equal to |
|---|---|---|
| = | x = 5 | x = 5 |
| += | x += 3 | x = x + 3 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 3 | x = x / 3 |
| %= | x %= 3 | x = x % 3 |
| **= | x **= 3 | x = x ** 3 |
| //= | x //= 3 | x = x // 3 |
undefined
score = 10
score += 5
print(score) # 15
score *= 2
print(score) # 30
score -= 10
print(score) # 20
Instead of writing score = score + 5, we can simply score += 5. Compound assignment operators make the code shorter and easier to read.
Comparison Operators
Comparison operators are used to compare two values. Those operators are not only used to compare numeric values. In comparison operators, we always return a boolean. So, if the comparison matches, it returns True; otherwise, False .
| Operator | Name | Example |
|---|---|---|
| == | Equal | x == y |
| != | Not Equal | x != y |
| > | Greater than | x > y |
| < | Less than | x < y |
| >= | Greater than or equal to | x >= y |
| <= | Less than or equal to | x <= y |
undefined
age = 20
print(age >= 18) # True
print(age == 30) # FalseLogical Operators
Logical operators are used to combine conditional statements. They are often used in if statements to combine conditionals.
| Operator | Description | Example |
|---|---|---|
| and | Returns True if both statements are true | x < 5 and x < 10 |
| or | Returns True if one of the statements is true | x < 5 or x < 4 |
| not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
undefined
age = 22
has_license = True
if age >= 18 and has_license:
print("You can drive.")Ternary Operator
Ternary operators are useful for simple conditions. If the logic becomes more complex, prefer using a regular if/else statement to improve readability.
# Traditional way
if score >= 60:
result = "Approved"
else:
result = "Failed"
# Ternary operator
result = "Approved" if score >= 60 else "Failed"Another important concept is understanding how multiple operators work together in the same expression. When an expression contains multiple operators, Python follows a specific order of execution, and it's called operator precedence .
Operator Precedence
| Precedence | Operators |
|---|---|
| Highest | ( ) |
| ** | |
| *, /, //, % | |
| +, - | |
| ==, !=, >, <, >=, <= | |
| not | |
| and | |
| Lowest | or |
undefined Examples
As you can see in the example below, almost the same expression, but we added an operator that makes the sum a priority.
result = 2 + 3 * 4
print(result) # 14
result = (2 + 3) * 4
print(result) # 20In this article, we learned the most common operators used in Python. These operators are enough to build the programs we'll create throughout the next chapters. As you progress, you'll also learn more advanced operators such as identity operators , membership operators , and bitwise operators .
Practice
# Exercise 1
# What is the output?
x = 10
y = 5
print(x + y * 2)# Exercise 2
# Replace the expression using an assignment operator.
score = score + 10# Exercise 3
# What is the result?
age = 20
print(age >= 18 and age < 30)# Exercise 4
# Rewrite this using a ternary operator.
if points >= 100:
level = "Gold"
else:
level = "Silver"