← View series: python tutorials
~/blog
Basic Operators
Introduction
In this tutorial, you'll learn about operators in Python - symbols that perform operations on values. Operators are the building blocks of any program as they allow you to perform calculations, make comparisons, and combine conditions.
What You'll Learn
- Arithmetic operators (math operations)
- Comparison operators (comparing values)
- Logical operators (combining conditions)
- Assignment operators (shorthand for operations)
- Operator precedence (which operations run first)
Arithmetic Operators
Arithmetic operators perform mathematical calculations:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 5 / 2 | 2.5 |
// | Floor Division | 5 // 2 | 2 |
% | Modulus (remainder) | 5 % 2 | 1 |
** | Exponentiation | 5 ** 2 | 25 |
Basic Math Operations
# Addition
print(10 + 5) # Output: 15
# Subtraction
print(10 - 5) # Output: 5
# Multiplication
print(10 * 5) # Output: 50
# Division (always returns float)
print(10 / 5) # Output: 2.0
print(10 / 4) # Output: 2.5Special Division Operators
# Floor Division (rounds down)
print(10 // 3) # Output: 3
print(10 // 4) # Output: 2
print(-10 // 3) # Output: -4 (rounds toward negative infinity)
# Modulus (remainder after division)
print(10 % 3) # Output: 1
print(10 % 4) # Output: 2
print(15 % 5) # Output: 0
# Practical use of modulus
number = 17
print(f"{number} is odd: {number % 2 != 0}") # True
print(f"{number} is even: {number % 2 == 0}") # FalseExponentiation
# Raising to a power
print(2 ** 3) # Output: 8 (2 cubed)
print(5 ** 2) # Output: 25 (5 squared)
print(10 ** 6) # Output: 1000000
# Square root using ** 0.5
print(16 ** 0.5) # Output: 4.0Using Variables with Arithmetic
# Store values in variables first
a = 10
b = 3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.333...
print(a // b) # Output: 3
print(a % b) # Output: 1
print(a ** b) # Output: 1000Comparison Operators
Comparison operators compare two values and return True or False:
| Operator | Name | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 5 > 3 | True |
< | Less than | 5 < 3 | False |
>= | Greater than or equal | 5 >= 5 | True |
<= | Less than or equal | 5 <= 3 | False |
Using Comparisons
x = 10
y = 5
# Equal to
print(x == y) # Output: False
# Not equal to
print(x != y) # Output: True
# Greater than
print(x > y) # Output: True
# Less than
print(x < y) # Output: False
# Greater than or equal
print(x >= y) # Output: True
# Less than or equal
print(x <= y) # Output: FalseComparing Different Types
# Numbers
print(10 == 10) # True
print(10 == 10.0) # True (int equals float)
# Strings (case-sensitive)
print("hello" == "hello") # True
print("hello" == "Hello") # False
print("hello" != "world") # TrueLogical Operators
Logical operators combine boolean values:
| Operator | Description | Example |
|---|---|---|
and | True if both are True | True and True → True |
or | True if at least one is True | True or False → True |
not | Inverts the boolean | not True → False |
Truth Tables
# AND - both must be True
print(True and True) # True
print(True and False) # False
print(False and True) # False
print(False and False) # False
# OR - at least one must be True
print(True or True) # True
print(True or False) # True
print(False or True) # True
print(False or False) # False
# NOT - inverts the value
print(not True) # False
print(not False) # TruePractical Examples
age = 25
has_license = True
# Can they drive?
can_drive = age >= 18 and has_license
print(f"Can drive: {can_drive}") # True
# Is the person a teenager?
is_teenager = age >= 13 and age <= 19
print(f"Is teenager: {is_teenager}") # False
# Using NOT
is_not_adult = not (age >= 18)
print(f"Not adult: {is_not_adult}") # FalseAssignment Operators
Assignment operators assign values to variables with optional operations:
| Operator | Example | Equivalent 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 |
Using Assignment Operators
x = 10
print(x) # Output: 10
x += 5 # Same as: x = x + 5
print(x) # Output: 15
x -= 3 # Same as: x = x - 3
print(x) # Output: 12
x *= 2 # Same as: x = x * 2
print(x) # Output: 24
x /= 4 # Same as: x = x / 4
print(x) # Output: 6.0Operator Precedence
When you have multiple operators, Python follows a specific order (PEMDAS):
| Priority | Operator | Description |
|---|---|---|
| 1 | () | Parentheses |
| 2 | ** | Exponentiation |
| 3 | * / // % | Multiplication, Division, Floor, Modulus |
| 4 | + - | Addition, Subtraction |
| 5 | == != < > <= >= | Comparisons |
| 6 | not | Logical NOT |
| 7 | and | Logical AND |
| 8 | or | Logical OR |
Examples
# Without parentheses (follows precedence)
result = 2 + 3 * 4
print(result) # Output: 14 (not 20!)
# With parentheses (explicit order)
result = (2 + 3) * 4
print(result) # Output: 20
# Complex expression
result = (10 - 2) * 3 ** 2 / 2
print(result) # Output: 108.0
# Step by step: 3**2=9, (10-2)=8, 8*9=72, 72/2=36... wait let me recalculate
# 3**2 = 9
# (10 - 2) = 8
# 8 * 9 = 72
# 72 / 2 = 36Summary
In this tutorial, you learned:
- ✅ Arithmetic operators:
+,-,*,/,//,%,** - ✅ Comparison operators:
==,!=,>,<,>=,<= - ✅ Logical operators:
and,or,not - ✅ Assignment operators:
+=,-=,*=,/=, etc. - ✅ Operator precedence (PEMDAS)
🧑💻 Practice Exercise
Create a calculator program that:
- Asks for two numbers
- Performs all arithmetic operations on them
- Shows whether the first number is greater than, less than, or equal to the second
Click to see solution
# Simple calculator
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print("\n--- Results ---")
print(f"Addition: {num1} + {num2} = {num1 + num2}")
print(f"Subtraction: {num1} - {num2} = {num1 - num2}")
print(f"Multiplication: {num1} * {num2} = {num1 * num2}")
print(f"Division: {num1} / {num2} = {num1 / num2}")
print(f"Floor Division: {num1} // {num2} = {num1 // num2}")
print(f"Modulus: {num1} % {num2} = {num1 % num2}")
print(f"Exponent: {num1} ** {num2} = {num1 ** num2}")
# Comparison
if num1 > num2:
print(f"\n{num1} is greater than {num2}")
elif num1 < num2:
print(f"\n{num1} is less than {num2}")
else:
print(f"\n{num1} is equal to {num2}")What's Next
In the next tutorial, we'll learn about Control Flow - Conditionals - how to make decisions in your code.