Scrollable Nav Bar

Python Operators

Operators are the “action words” of Python. If variables and values are the data you store, operators are the instructions that tell Python what to do with that data—add it, compare it, check membership, and more.

This chapter will walk you through the most commonly used operators in Python, with small examples you can try immediately.

[Screenshot: Jupyter/IDE showing a few operator examples running]


1. Arithmetic Operators

Arithmetic operators perform math on numbers. You’ve already seen + for addition; Python also provides subtraction, multiplication, division, and a few operators that are especially useful in programming.

Addition and subtraction (+, -)

Addition and subtraction behave as you’d expect:

print(10 + 5)   # 15
print(10 - 5)   # 5
Code language: PHP (php)

Multiplication (*)

The multiplication operator is an asterisk *.

print(6 * 7)    # 42
Code language: PHP (php)

Exponentiation (**)

Exponentiation raises a number to a power using **.

print(5 ** 2)   # 25
print(2 ** 5)   # 32
Code language: PHP (php)

Division (/) returns a float

Division uses a forward slash /. In Python, regular division returns a floating-point number (a decimal), even when the result looks like a whole number.

print(20 / 5)   # 4.0
print(20 / 6)   # 3.3333333333333335
Code language: PHP (php)

That .0 is expected—Python chooses a float because division doesn’t always produce a whole number.

Floor division (//) for “whole number” division

Sometimes you want the quotient without the decimal part. That’s what floor division // does.

print(20 // 6)  # 3
print(21 // 6)  # 3
Code language: PHP (php)

Floor division is especially handy when you’re working with indexes, pages, grouping, or breaking values into buckets.

Modulus (%) for the remainder

The modulus operator % gives the remainder after division.

print(20 % 6)   # 2
Code language: PHP (php)

Here’s the idea: 20 / 6 gives 3 with a remainder of 2, so 20 % 6 is 2.

This comes up a lot in real programming. For example, checking whether a number is even:

number = 14
print(number % 2 == 0)  # True
Code language: PHP (php)

Or doing something every Nth time:

for i in range(1, 11):
    if i % 3 == 0:
        print(i, "is divisible by 3")
Code language: PHP (php)

[Screenshot: Output showing modulus-based checks]


2. Operators with Strings

Operators aren’t limited to numbers. Python also supports a couple of useful operations on strings.

String concatenation with +

Using + with strings joins them together.

first = "Hello"
second = "World"
print(first + " " + second)  # Hello World
Code language: PHP (php)

This works only when both sides are strings. If you try to add a string and a number directly, Python will raise an error:

# print("Age: " + 25)  # TypeError
Code language: PHP (php)

When you need to combine text with numbers, convert the number to a string:

print("Age: " + str(25))  # Age: 25
Code language: PHP (php)

String repetition with *

Multiplying a string by a number repeats it.

print("Hi! " * 4)  # Hi! Hi! Hi! Hi!
Code language: PHP (php)

This is a small trick, but it’s surprisingly useful for quick formatting, separators, or creating simple patterns.


3) Comparison Operators (They Produce True or False)

Comparison operators evaluate two values and return a Boolean: True or False.

Equality and inequality (==, !=)

== checks whether two values are equal.

print(True == True)     # True
print(10 == 10)         # True
print(10 == 11)         # False
Code language: PHP (php)

!= checks whether two values are not equal.

print(10 != 11)         # True
Code language: PHP (php)

Less-than and greater-than (<, <=, >, >=)

These operators compare numeric values (and also work with strings in alphabetic order, though you’ll use them most often with numbers).

print(4 < 5)            # True
print(5 <= 5)           # True
print(5 > 2)            # True
print(5 >= 2)           # True
Code language: PHP (php)

A common beginner mistake is confusing assignment (=) with comparison (==). Use = to store a value in a variable, and == to compare values.

[ Example showing = vs == in code]


4. Logical Operators (and, or, not)

Logical operators combine or flip Boolean values. They read like plain English, which makes them easier to remember.

and

With and, both sides must be True for the whole expression to be True.

print(True and True)    # True
print(True and False)   # False
Code language: PHP (php)

or

With or, at least one side must be True.

print(True or False)    # True
print(False or False)   # False
Code language: PHP (php)

not

not flips a Boolean value.

print(not True)         # False
print(not False)        # True
Code language: PHP (php)

Logical operators are most useful in conditions, like if statements:

age = 20
has_id = True

if age >= 18 and has_id:
    print("Allowed")
else:
    print("Not allowed")
Code language: PHP (php)

5. Membership Operators (in, not in)

Membership operators check whether a value exists inside a sequence (like a list, tuple, set, or string). They also return True or False.

Checking membership in lists

numbers = [1, 2, 3, 4, 5]
print(1 in numbers)         # True
print(10 in numbers)        # False
print(10 not in numbers)    # True
Code language: PHP (php)

Checking membership in strings

You can also search inside strings.

text = "my pet cat"
print("cat" in text)       # True
print("dog" in text)       # False
Code language: PHP (php)

Be careful with substring checks. For example, "cat" in "catatonic" is also True because the letters c-a-t appear in that word.

[ Membership checks with list and string]


6. Identity Operators (is, is not)

Identity operators check whether two variables refer to the same object in memory, not whether they merely have the same value.

This is different from ==.

a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)   # True  (values are the same)
print(a is b)   # False (different objects)
Code language: PHP (php)

Identity checks are most commonly used with None:

value = None

if value is None:
    print("No value yet")
Code language: PHP (php)

7. Operator Precedence (Why Parentheses Help)

When you combine operators, Python follows a precedence order (similar to math). Multiplication happens before addition, and parentheses can force an order.

print(2 + 3 * 4)     # 14
print((2 + 3) * 4)   # 20
Code language: PHP (php)

When an expression gets even slightly complex, parentheses make your intent clearer and reduce mistakes.

[ Same expression with and without parentheses]


8) Quick Practice (Try These in Your Notebook)

Type these into your Python environment and predict the result before running them:

print(17 % 5)
print(9 // 2)
print("na" * 4 + " Batman!")
print(10 / 2)
print(5 <= 5)
print(True and not False)
print("py" in "python")

x = None
print(x is None)
Code language: PHP (php)

Summary

Operators are how you tell Python to work with your data. Arithmetic operators handle math (including helpful tools like % for remainders), strings can be joined and repeated using + and *, and comparison/logical/membership operators help you make decisions by producing True or False. Identity operators, especially is None, are a clean way to check for “no value.”

In the next chapters, you’ll use these operators constantly inside conditions, loops, and real programs—so it’s worth practicing until the results feel natural.