Skip to main content

Python Operators for Beginners: Arithmetic, Logical and More

Python operators: arithmetic, comparison, logical and assignment

Learn what basic Python operators are and how to use them: arithmetic, comparison, logical, and assignment operators, with simple examples.

If variables are nouns, operators are the verbs of code. Without them, you couldn't create a login system, a search engine, or a price filter.

Arithmetic Operators

Arithmetic operators allow you to perform basic mathematical calculations.

OperatorMeaningExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33.3333
%Modulus10 % 31
**Exponentiation10 ** 31000
//Floor division10 // 33

Difference between / and //

It's important to note the difference: / always returns a decimal number (float), while // rounds down to the nearest integer.

Python
print(10 / 3)   # 3.3333333333333335 (float)
print(10 // 3) # 3 (int, discards the decimal)
print(-10 // 3) # -4 (rounds down!)

Assignment Operators

These operators are used to assign a value to a variable. Most combine an arithmetic operation with an assignment in a single step.

Assuming we start with x = 10:

OperatorExampleEquivalent toFinal Result of x
=x = 5x = 55
+=x += 3x = x + 313
-=x -= 2x = x - 28
*=x *= 4x = x * 440
/=x /= 2x = x / 25.0
%=x %= 3x = x % 31
//=x //= 3x = x // 33
**=x **= 2x = x ** 2100
DivZone ad link Logo
AI PlatformTurn Code into Customers.Try it free

Building Blocks: Comparison Operators

Before delving into pure logic, we need to compare data. These operators always return a Boolean value (True or False).

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than10 > 20False
<Less than10 < 20True
>=Greater or equal18 >= 18True
<=Less or equal15 <= 10False

Difference between = and ==

This is one of the most common beginner mistakes:

  • = is the assignment operator: x = 5 (we give x the value 5).
  • == is the comparison operator: x == 5 (we ask if x equals 5).

The 3 Pillars of Logic: and, or, not

This is where Python shines in terms of readability. Other languages use && or ||, but in Python we use English words, which makes the code read like natural language.

The and operator

Returns True only if both conditions are true. It's a strict filter.

Python
age = 25
has_license = True

# To drive, you need both
can_drive = age >= 18 and has_license
print(f"Can drive?: {can_drive}") # True

The or operator

Returns True if at least one of the conditions is true. It's a flexible filter.

Python
is_weekend = True
is_on_vacation = False

# We rest if it's weekend OR if we're on vacation
rest = is_weekend or is_on_vacation
print(f"Rest today?: {rest}") # True

The not operator

Inverts the Boolean value: turns True into False and vice versa.

Python
door_locked = True

if not door_locked:
print("The door is open, you can enter")
else:
print("The door is locked, you need the key")

Truth Table

ABA and BA or Bnot A
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue

Short-Circuit Evaluation

Python is smart and lazy (in a good way). This optimizes performance:

  • In an and: If the first condition is False, Python doesn't look at the second, because it knows the final result will be False anyway.

  • In an or: If the first condition is True, Python ignores the second, because it already knows the result will be True.

Python
# Safety example
divisor = 0
# Without short-circuiting, this would cause an error (division by zero)
# But since divisor != 0 is False, Python doesn't evaluate the second part.
result = divisor != 0 and (10 / divisor > 1)
print(result) # False (no errors)

Operator Precedence

Just like in mathematics (where multiplication comes before addition), logic has a hierarchy. The order of evaluation is:

  1. not (Highest priority)

  2. and

  3. or (Lowest priority)

Pro Tip: Always use parentheses to avoid confusion and make your code readable for others.

Python
# What do you think this returns?
result = True or False and False
# First evaluates: False and False -> False
# Then: True or False -> True
print(result) # True

# Much better with parentheses:
result = (True or False) and False # False

Special Operators: is and in

Python has two additional very powerful operators:

  • in: Checks if an element is within a collection (list, string, etc.).

  • is: Checks if two variables point to the same object in memory (identity).

Python
fruits = ["apple", "banana", "grape"]
print("apple" in fruits) # True
print("pear" in fruits) # False

a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True (same content)
print(a is b) # False (two different lists in memory)

Practical Challenge: "The Club Guardian"

Write a script that decides whether a person can enter a VIP club. The rules are:

  • Must be over 18 years old.

  • Must have a valid ticket OR be on the guest list.

  • Must not be blacklisted (banned).

Test variables:

Python
age = 20
has_ticket = False
is_invited = True
is_banned = False

# Your logic here
# can_enter = ...

Solution:

Python
can_enter = age >= 18 and (has_ticket or is_invited) and not is_banned
print(f"Can enter?: {can_enter}") # True
Resources

Ready to continue? Now that you master operators, learn how to make decisions with conditional structures in Python.

Want to put what you learned to the test? Use DivZone AI to generate and test your own exercises with operators.