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 // 2is3).%(Modulus): Returns the remainder of division (e.g.,7 % 2is1).**(Exponentiation): Calculates powers (e.g.,5 ** 2is25).
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?
Question 2: What is the remainder result of 10 % 3?
Question 3: Which operator is used to calculate exponents (powers) in Python?
Question 4: What does x += 3 do to the variable x?
Question 5: What does the 'and' operator return if the first statement is True and the second is False?
Question 6: What does the 'or' operator return if the first statement is True and the second is False?
Question 7: Which comparison operator checks if two values are NOT equal?
Question 8: What does print(not (5 > 2)) output?
Question 9: What is the result of evaluating the division: 8 / 2?
Question 10: What is the result of evaluating: 10 - 3 * 2?
๐ฅ๏ธ Output Prediction Challenges
Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.
Predict the output of the code snippet below. Type the exact output in the console.
print(10 // 3)
Predict the output of the code snippet below. Type the exact output in the console.
print(10 % 3)
Predict the output of the code snippet below. Type the exact output in the console.
x = 5
x += 2
print(x)
Predict the output of the code snippet below. Type the exact output in the console.
print(2 ** 3)
Predict the output of the code snippet below. Type the exact output in the console.
print(10 > 5 and 5 > 2)
๐ฎ Interactive Code Playground
Type your own Python code below and click "Run Code" to execute it instantly in your browser!
Console ready.
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
๐ Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.