Skip to main content

Operators and Boolean Logic

Making Decisions with Math and Logic

· 3 min read

In the previous unit, we covered variables and data types. Now let's look at what you can actually do with those values. Operators are how Python performs calculations and makes comparisons. They're the verbs of programming.

Operators and Boolean Logic

Arithmetic Operators

Python handles math the way you'd expect. The basic operators work on numbers:

a = 10
b = 3

print(a + b) # 13 (addition)
print(a - b) # 7 (subtraction)
print(a * b) # 30 (multiplication)
print(a / b) # 3.333... (division)
print(a % b) # 1 (modulus - the remainder)
print(a ** b) # 1000 (exponent - 10 to the power of 3)

Division always returns a float, even when the result is a whole number. If you need integer division (no decimal), use //.

print(10 / 2)   # 5.0
print(10 // 3) # 3 (rounds down)

Comparison Operators

Comparison operators compare two values and return a boolean: True or False. These are the foundation of decision-making in code.

x = 10
y = 20

print(x == y) # False (equal to)
print(x != y) # True (not equal to)
print(x > y) # False (greater than)
print(x < y) # True (less than)
print(x >= y) # False (greater than or equal)
print(x <= y) # True (less than or equal)

Notice that equality uses two equals signs (==). A single = is for assignment. This trips people up at first.

Logical Operators

Logical operators combine boolean values. There are three: and, or, and not.

and returns True only if both sides are true.

print(True and True)    # True
print(True and False) # False

or returns True if at least one side is true.

print(True or False)    # True
print(False or False) # False

not flips a boolean to its opposite.

print(not True)   # False
print(not False) # True

You can combine these with comparisons to build more complex conditions.

age = 25
income = 50000

# Both conditions must be true
print(age > 18 and income > 30000) # True

# At least one must be true
print(age < 18 or income > 100000) # False

We'll use these constantly when we get to if statements in the next unit.

Project: Draw Shapes Based on User Input

Let's apply operators in a Turtle project. This program asks for a number and draws different shapes depending on the input.

import turtle

screen = turtle.Screen()
screen.bgcolor("white")

t = turtle.Turtle()

# Get user input and convert to integer
user_input = int(input("Enter a number between 1 and 3: "))

# Use comparison to decide which shape to draw
if user_input == 1:
for _ in range(4):
t.forward(100)
t.right(90)
elif user_input == 2:
for _ in range(3):
t.forward(100)
t.right(120)
elif user_input == 3:
t.circle(50)

turtle.done()

We use int(input(...)) to get a number from the user. Then comparison operators check which value was entered. Enter 1 for a square, 2 for a triangle, 3 for a circle.

What happens if someone enters 4? Nothing draws. In the next unit, we'll learn how to handle unexpected input with if statements and provide feedback to the user.

In the next unit, we'll dig into control flow and learn how if, elif, and else let you direct your program's behavior based on conditions.