โ† Back to Hub
Module 3 โ€ข Week 10 โ€ข Math, Comparisons, and Logic Gates

Python Operators

Master Python operators: arithmetic, shorthands, comparisons, and logical gates (and, or, not).

1. Arithmetic Operators

Used to perform mathematical calculations:

  • + (Add), - (Subtract), * (Multiply), / (Divideโ€”always outputs a float!).
  • // (Floor Division): Divides and rounds down to the nearest integer (e.g., 7 // 2 is 3).
  • % (Modulus): Returns the remainder of division (e.g., 7 % 2 is 1).
  • ** (Exponentiation): Calculates powers (e.g., 5 ** 2 is 25).

2. Shorthand Assignment Operators

Shorthand symbols update variable values in place: x += 5 is identical to writing x = x + 5.

3. Comparison Operators

Used to compare values, returning True or False: == (equal), != (not equal), >, <, >=, <=.

4. Logical Operators

Used to combine boolean checks:

  • and: Returns True only if **both** statements are True.
  • or: Returns True if **at least one** statement is True.
  • not: Inverts the boolean result.

๐Ÿ’ป Python Code Examples

Example 1: Exponents and Modulo Operations

print(2 ** 3) # Exponent: 8
print(10 % 3) # Modulo: 1 (remainder)

Description: Uses ** for powers and % to find the remainder of division.

Example 2: Floor vs. Standard Division

print(7 / 2)  # Standard: 3.5
print(7 // 2) # Floor: 3 (rounds down)

Description: Floor division rounds down decimals, returning an integer.

Example 3: Shorthand Incrementing

count = 10
count += 5
print(count) # Output: 15

Description: Uses += to add and reassign a value to a variable in one step.

Example 4: Chained Comparisons

age = 25
print(18 <= age < 30)

Description: Verifies if age is between 18 (inclusive) and 30 in a single comparison chain.

Example 5: Combining logic with 'and' / 'or'

is_sunny = True
has_umbrella = False
print(is_sunny or has_umbrella) # True
print(is_sunny and has_umbrella) # False

Description: Applies logic checks to combine boolean states.

Example 6: Inverting conditions with 'not'

is_weekend = False
print(not is_weekend) # Output: True

Description: Uses 'not' to invert False to True.

๐Ÿง  Active Retrieval Quizzes

Answer the following 10 multiple-choice questions to test your storage strength.

Question 1: What is the floor division result of 9 // 2 in Python?

Explanation: Floor division rounds down the division result to the nearest integer, yielding 4.

Question 2: What is the remainder result of 10 % 3?

Explanation: 10 divided by 3 is 3 remainder 1. Modulo (%) returns the remainder (1).

Question 3: Which operator is used to calculate exponents (powers) in Python?

Explanation: Double asterisks ** are used for exponentiation in Python.

Question 4: What does x += 3 do to the variable x?

Explanation: x += 3 is shorthand for x = x + 3.

Question 5: What does the 'and' operator return if the first statement is True and the second is False?

Explanation: The 'and' operator requires both statements to be True to return True.

Question 6: What does the 'or' operator return if the first statement is True and the second is False?

Explanation: The 'or' operator returns True if at least one statement is True.

Question 7: Which comparison operator checks if two values are NOT equal?

Explanation: != is the inequality comparison operator in Python.

Question 8: What does print(not (5 > 2)) output?

Explanation: 5 > 2 is True, so not (True) inverts the result to False.

Question 9: What is the result of evaluating the division: 8 / 2?

Explanation: Division (/) in Python always returns a float (4.0).

Question 10: What is the result of evaluating: 10 - 3 * 2?

Explanation: Multiplication has higher precedence than subtraction, evaluating 3 * 2 = 6 first, then 10 - 6 = 4.

๐Ÿ–ฅ๏ธ Output Prediction Challenges

Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.

Challenge 1 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print(10 // 3)
Console ready. Type your prediction below...
Challenge 2 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print(10 % 3)
Console ready. Type your prediction below...
Challenge 3 of 5

Predict the output of the code snippet below. Type the exact output in the console.

x = 5
x += 2
print(x)
Console ready. Type your prediction below...
Challenge 4 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print(2 ** 3)
Console ready. Type your prediction below...
Challenge 5 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print(10 > 5 and 5 > 2)
Console ready. Type your prediction below...

๐ŸŽฎ Interactive Code Playground

Type your own Python code below and click "Run Code" to execute it instantly in your browser!

๐Ÿ“ Python Code Editor
Python WebAssembly runtime is unloaded. Click 'Run Code' to load.
๐Ÿ–ฅ๏ธ Output Console
Console ready.
๐Ÿ’ฌ Teacher Consultation

Stuck on any concept, or want to explore an edge case? Don't hesitate to ask follow-up questions to your instructor! Active questioning helps build storage strength.

๐Ÿ“‹ Progress Status

Lesson Complete?

๐Ÿ“Œ Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.