Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Operators are the verbs of Python—they're how you do things with your data. Every program you write will use operators to perform calculations, make decisions, and control program flow. When you're tested on Python fundamentals, you're being tested on whether you understand not just what operators do, but when to use each type. The difference between = and ==, or knowing when to reach for // instead of /, separates working code from broken code.
Think of operators as falling into distinct categories based on their purpose: performing math, assigning values, making comparisons, combining logic, checking identity, testing membership, and manipulating bits. Don't just memorize the symbols—know what problem each operator solves and what type of value it returns. That conceptual understanding will carry you through debugging, code reading, and writing efficient solutions.
Arithmetic operators handle numerical calculations. These operators work on numeric types and return numeric results—except division, which has some quirks you need to know.
+ operator also joins strings, demonstrating Python's operator overloading/ always returns a float—even $$10 / 2$$ gives you $$5.0$$, not $$5$$// returns the largest integer less than or equal to the result—useful when you need whole numbers only% returns the remainder—essential for checking divisibility, cycling through values, or extracting digits$$2 ** 3$$ returns $$8$$, equivalent to $$9 ** 0.5$$ returns $$3.0$$ (square root)$$2 ** 3 ** 2$$ equals $$512$$, not $$64$$, because it evaluates right-to-leftCompare: / vs. //—both divide numbers, but / always returns a float while // truncates toward negative infinity. If a problem requires counting whole items (like how many full boxes fit), reach for //.
Assignment operators bind values to variable names. The key insight is that compound operators combine calculation and storage into a single, readable step.
x = 5 creates a reference from x to the integer object 5a = b = c = 0 sets all three variables to zerox += 3 is shorthand for x = x + 3//= for floor division and **= for exponentiationCompare: x = x + 1 vs. x += 1—functionally identical for immutable types, but += is more concise and signals intent. For mutable types like lists, += modifies in place while x = x + [item] creates a new object.
Comparison operators evaluate relationships between values and return Boolean results. Every comparison resolves to either True or False, making these essential for conditional logic.
== checks value equality—returns True if both sides have the same value!= checks inequality—returns True if values differ1 == 1.0 is True, but "1" == 1 is False< and > for strict comparisons, <= and >= when equality counts1 < x < 10 works as you'd expect mathematically"apple" < "banana" is True based on character codesCompare: == vs. is—== checks if values are equal, while is checks if two names reference the same object in memory. Use == for value comparison; reserve is for identity checks (especially with None).
Logical operators work with Boolean values to build complex conditions. Understanding short-circuit evaluation here will help you write both correct and efficient code.
and OperatorTrue only if both operands are True—otherwise returns FalseFalse, Python never evaluates the secondor OperatorTrue if at least one operand is True—only False when both are Falsename = user_input or "Guest"not Operatornot True returns False, not False returns Trueif not found: reads more naturally than if found == False:not 0 returns True, not "hello" returns FalseCompare: and vs. or short-circuiting—and stops at the first False (why continue if one condition fails?), while or stops at the first True (why continue if one condition succeeds?). This matters when the second operand has side effects or is expensive to compute.
These operators test relationships between objects and collections. Identity checks memory location; membership checks containment.
is checks object identity—returns True if both variables point to the exact same object in memoryNone—if x is None: is the Pythonic way to check for None valuesin tests for presence in a collection—works with strings, lists, tuples, sets, and dictionaries"name" in my_dict checks if "name" is a key, not a valueCompare: is vs. == with None—always use is None rather than == None. It's faster (identity check vs. value comparison) and avoids edge cases where objects define custom __eq__ methods.
Bitwise operators work directly on the binary representation of integers. These are specialized tools—less common in everyday code but powerful for specific applications like flags, permissions, and low-level optimization.
& (AND) returns 1 only where both bits are 1—useful for masking specific bits| (OR) returns 1 where either bit is 1—useful for combining flags^ (XOR) returns 1 where bits differ—useful for toggling and simple encryption~ inverts all bits—for integer x, returns $$-(x+1)$$ due to two's complement representation<< left shift multiplies by —x << 3 is equivalent to >> right shift divides by —x >> 2 is equivalent to (integer division)Compare: << vs. * for powers of two—x << 3 and x * 8 produce the same result, but bit shifting is closer to hardware operations. In Python, readability usually wins, so use multiplication unless you're doing explicit bit manipulation.
| Concept | Best Examples |
|---|---|
| Basic math operations | +, -, *, ** |
| Division with different return types | / (float), // (integer), % (remainder) |
| Storing and updating values | =, +=, -=, *= |
| Value equality testing | ==, != |
| Ordering comparisons | <, >, <=, >= |
| Combining Boolean conditions | and, or, not |
| Object identity checking | is, is not |
| Collection membership | in, not in |
| Binary manipulation | &, ` |
What's the difference between / and //, and when would you choose floor division over true division?
Which two operators both involve checking equality, but one checks value while the other checks memory location? When should you use each?
Explain short-circuit evaluation: if x = False, what happens when Python evaluates x and some_function()? What about x or some_function()?
Compare and contrast in when used with a list versus a dictionary. What does each check for?
You need to check if a variable contains None. Write the Pythonic way to do this, and explain why == is discouraged for this comparison.