✦ Python Foundations Third lesson

Concept 03 · Operators

Operators —
Make Python Do Something

Operators let Python calculate, compare values, and combine simple logical ideas. Learn how a few symbols can change what a program does.

01 Learn02 Try03 Predict

Operators tell Python what to do with values.

You can use operators to calculate with numbers, compare values, and combine true/false results. Parentheses can make a longer expression easier to read.

x = 10
y = 3

print(x + y)   # 13
print(x - y)   # 7
print(x * y)   # 30
print(x / y)   # 3.333...
print(x // y)  # 3
print(x % y)   # 1

+, -, and * calculate as you expect. / returns a float. // is floor division, and % gives the remainder after division.

10 // 3 # 3

For these positive numbers, floor division gives 3. It is not simply removing decimals in every situation: // follows floor division rules, which matter for negative values too.

↳

Assignment is not comparison: = assigns a value to a variable. == asks whether two values are equal and produces True or False.

Compare values
age = 18

print(age == 18)  # True
print(age != 20)  # True
print(age > 20)   # False
print(age >= 18)  # True
print(age < 21)   # True
print(age <= 18)  # True

Comparison operators such as ==, !=, >, <, >=, and <= always produce True or False.

Combine true/false values
age = 20
has_ticket = True

print(age >= 18 and has_ticket)  # True
print(age < 18 or has_ticket)    # True
print(not has_ticket)            # False

and needs both conditions to be true. or needs at least one. not reverses a true/false value. You will use these ideas in Conditions later.

Python Playground

Loading Python runtime…

Change the values of x and y. Try to predict the results before running the code.

operators.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Think about the remainder left after dividing eight by three.

x = 8
y = 3

print(x % y)
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Ask Sloby about operators

Describe what feels unclear. Sloby can help with calculations, comparisons, and logical expressions.

Hi! I'm Sloby. Let's explore how Python operators work. Ask me about calculations, comparisons, or logical expressions!

What will this program print, and why? Explain each result.

x = 10
y = 3

print(x // y)
print(x % y)
print(x > y and y == 3)
Python AI Tutor · Learn by understanding, not just by running code.