Python Operators

Let’s look at operators

  1. Notice

    Operators combine values. / performs true division; // performs floor division, including for negative numbers.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 7
print(number / 2)
print(number // 2)
print(number % 2)
Live state

Top-level scope

number→Object 1
Object 1 · int7
This step

Operators combine values. / performs true division; // performs floor division, including for negative numbers.

Picture operators

Operators combine values. / performs true division; // performs floor division, including for negative numbers.

Image brief

Alt text
A diagram for Python Operators. Instructions: number = 7 print(number / 2) print(number // 2) print(number % 2). Displayed result: 3.5 3 1. The final example bindings are number --> object 1 (int). object 1: 7
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Operators". Teaching focus: Operators combine values. / performs true division; // performs floor division, including for negative numbers. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = 7 print(number / 2) print(number // 2) print(number % 2) | v DISPLAYED RESULT 3.5 3 1 FINAL BINDINGS number --> object 1 (int) object 1: 7 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Operators. Instructions: number = 7 print(number / 2) print(number // 2) print(number % 2). Displayed result: 3.5 3 1. The final example bindings are number --> object 1 (int). object 1: 7

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 7
print(number / 2)
print(number // 2)
print(number % 2)
Live state

Top-level scope

number→Object 1
Object 1 · int7
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
3.5
3
1

What do you expect this program to print?

What does this example display?

number = 7
print(number / 2)
print(number // 2)
print(number % 2)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

3.5 3 1

Follow the printed values in order. The output is: 3.5 3 1

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

Operators combine values. / performs true division; // performs floor division, including for negative numbers.

Operators combine values. / performs true division; // performs floor division, including for negative numbers.

Operators

Operators in Python are symbols or keywords that help you perform different kinds of actions, like calculations, comparisons, or checking conditions. Let’s explore each type of operator with easy-to-understand examples.

1. Arithmetic Operators

These are used to do basic math like addition, subtraction, multiplication, etc.

OperatorNameExampleResult
+Addition3 + 25
-Subtraction5 - 23
*Multiplication4 * 28
/Division6 / 23.0
%Modulus (Remainder)5 % 21
//Floor Division5 // 22
**Exponent (Power)2 ** 38
a = 10
b = 3

print("Addition:", a + b)        # 13
print("Subtraction:", a - b)     # 7
print("Multiplication:", a * b)  # 30
print("Division:", a / b)        # 3.333...
print("Modulus:", a % b)         # 1
print("Floor Division:", a // b) # 3
print("Exponent:", a ** b)       # 1000 (10³)
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Modulus: 1
Floor Division: 3
Exponent: 1000

2. Comparison Operators

These are used to compare two values. The result is always either True or False.

OperatorMeaningExampleResult
==Equal to3 == 3True
!=Not equal to5 != 2True
>Greater than5 > 3True
<Less than2 < 4True
>=Greater or equal4 >= 4True
<=Less or equal3 <= 5True
x = 7
y = 10

print(x == y)    # False, because 7 is not equal to 10
print(x != y)    # True, because 7 is not equal to 10
print(x > y)     # False, because 7 is not greater than 10
print(x < y)     # True, because 7 is less than 10
print(x >= y)    # False, because 7 is not greater than or equal to 10
print(x <= y)    # True, because 7 is less than or equal to 10
False
True
False
True
False
True

3. Assignment Operators

These are used to assign or update values of variables.

OperatorMeaningExample
=Assignx = 5
+=Add and assignx += 3 (same as x = x + 3)
-=Subtract and assignx -= 2
*=Multiply and assignx *= 4
/=Divide and assignx /= 2
%=Modulus and assignx %= 3
//=Floor divide and assignx //= 2
**=Power and assignx **= 2

Example

The following example applies each of the assignment operators one after the other, showing how the value of x changes step by step:

x = 5         # Start with assignment
x += 3        # x = x + 3 → x becomes 8
x -= 2        # x = x - 2 → x becomes 6
x *= 4        # x = x * 4 → x becomes 24
x /= 2        # x = x / 2 → x becomes 12.0
x %= 5        # x = x % 5 → x becomes 2.0
x //= 2       # x = x // 2 → x becomes 1.0 (floor division)
x **= 3       # x = x ** 3 → x becomes 1.0 (1 raised to power 3)
print(x)
1.0

4. Logical Operators

Used to combine multiple conditions.

OperatorMeaningExampleResult
andTrue if both are True5 > 3 and 4 > 2True
orTrue if at least one is True5 > 3 or 2 > 4True
notReverse the resultnot(5 == 5)False

Example

This example shows how logical operators work in different scenarios:

a = 7
b = 3

# AND: both conditions must be True
print(a > 5 and b < 5)

# OR: at least one condition must be True
print(a < 5 or b < 5)

# NOT: reverses the result
print(not a == 7)

# Combined logic
print(not (a < 10 and b > 5))
True
True
False
True

5. Identity Operators

Used to check if two variables point to the same object.

OperatorMeaningExampleResult
isTrue if both are same objecta is bTrue or False
is notTrue if not same objecta is not bTrue or False

Example

a = [1, 2]
b = a
print(a is b)       # True
print(a is not b)   # False
True
False
True

6. Membership Operators

Used to check if a value is inside a group (like a list or string).

OperatorMeaningExampleResult
inTrue if value is present"a" in "apple"True
not inTrue if value not present"x" not in "apple"True

Example

colors = ["red", "blue"]
print("red" in colors)      # True
print("green" not in colors) # True
True
True

7. Bitwise Operators

These work on binary (0s and 1s). You won’t need them yet, but here’s what they do:

OperatorName
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left Shift
>>Right Shift

Code Example

a = 5      # 0101 in binary
b = 3      # 0011 in binary

print(a & b)   # 1
print(a | b)   # 7
print(a ^ b)   # 6
print(~a)      # -6
print(a << 1)  # 10
print(a >> 1)  # 2
1
7
6
-6
10
2

Conclusion

Operators are the tools that help you build logic in your Python programs. Now that you’ve learned the basics, try using them in your own mini programs!

What to remember

Connect what you noticed in the example with the idea below.

Remember

Operators combine values. / performs true division; // performs floor division, including for negative numbers.

Watch out for this idea

// always truncates toward zero.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Operator Precedence →