Back to blog
← View series: python tutorials

~/blog

Basic Operators

Apr 1, 20266 min readBy Mohammed Vasim
PythonProgrammingTutorialBeginner

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:

OperatorNameExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 22.5
//Floor Division5 // 22
%Modulus (remainder)5 % 21
**Exponentiation5 ** 225

Basic Math Operations

python
# 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.5

Special Division Operators

python
# 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}") # False

Exponentiation

python
# 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.0

Using Variables with Arithmetic

python
# 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: 1000

Comparison Operators

Comparison operators compare two values and return True or False:

OperatorNameExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than5 > 3True
<Less than5 < 3False
>=Greater than or equal5 >= 5True
<=Less than or equal5 <= 3False

Using Comparisons

python
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: False

Comparing Different Types

python
# 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")    # True

Logical Operators

Logical operators combine boolean values:

OperatorDescriptionExample
andTrue if both are TrueTrue and TrueTrue
orTrue if at least one is TrueTrue or FalseTrue
notInverts the booleannot TrueFalse

Truth Tables

python
# 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)         # True

Practical Examples

python
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}")   # False

Assignment Operators

Assignment operators assign values to variables with optional operations:

OperatorExampleEquivalent To
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
//=x //= 3x = x // 3
%=x %= 3x = x % 3
**=x **= 3x = x ** 3

Using Assignment Operators

python
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.0

Operator Precedence

When you have multiple operators, Python follows a specific order (PEMDAS):

PriorityOperatorDescription
1()Parentheses
2**Exponentiation
3* / // %Multiplication, Division, Floor, Modulus
4+ -Addition, Subtraction
5== != < > <= >=Comparisons
6notLogical NOT
7andLogical AND
8orLogical OR

Examples

python
# 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 = 36

Summary

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:

  1. Asks for two numbers
  2. Performs all arithmetic operations on them
  3. Shows whether the first number is greater than, less than, or equal to the second
Click to see solution
python
# 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.

Control Flow - Conditionals →

Comments (0)

No comments yet. Be the first to comment!

Leave a comment