Python Expressions: Intermediate
Dropbox famously reduced its Python codebase from 150,000 lines to 90,000 lines by systematically replacing verbose patterns with cleaner syntax, cutting 40% of their code while making it more readable and faster to maintain. The engineers who led that effort leaned heavily on list comprehensions, conditional expressions, and other expressive Python idioms that let a single line replace five. When a company the size of Dropbox can delete 60,000 lines of code and end up with a better product, it demonstrates that elegant syntax is not just aesthetically pleasing but a genuine engineering advantage. This lesson teaches you the clean syntax patterns that make that kind of transformation possible.
Type Conversions
Switch between types without losing data
Why Convert Between Types?
The Conversion Functions
int() - Convert to Integer
float(): Convert to Decimal
str() - Convert to String
bool() - Convert to Boolean
1 becomes True, 0 becomes False. Empty string "" becomes False, but any non-empty string becomes True.
When Conversions Fail
> This code tries to convert the string "12.5" directly to an integer, but int() cannot parse decimals. Fix the conversion.
ValueError: int() cannot parse "12.5" because it contains a decimal point
The two-step conversion int(float(text)) is a common pattern when data arrives as a decimal string but you need a whole number. float() parses the decimal, then int() truncates it.
> Pick a conversion function to see how Python transforms the string "3.14" into a different type.
text = "3.14" result = (text) print(result, type(result))
Comparison Operators
Test conditions accurately in your code
What is a Comparison?
A comparison is an expression that asks a question about two values. The answer is always a boolean: either True or False. Think of it as asking the computer a yes-or-no question.
The expression age > 18 asks: "Is age greater than 18?" Since age is 25, the answer is True.
Six Comparison Operators
The first returns True, the second False, the third True.
Remember from the beginner lesson: = is assignment (storing a value), while == is comparison (asking if equal). This is one of the most common mistakes beginners make.
> This if-statement has a space in the middle of the equality operator. Remove the extra tile to fix it.
SyntaxError: invalid syntax
The >= and <= operators include the boundary value. This "or equal" distinction matters in real applications like age verification:
Returns True, False, and True. Notice >= is True when age is exactly 18, but > is False. The "or equal" part matters!
Comparing Strings
Storing Comparison Results
Logical Operators
Combine conditions with and, or, and not
The Three Logical Operators
The "and" Operator
The and operator requires BOTH conditions to be True. If either one is False, the whole expression is False. Think of it as a strict rule: everything must pass.
The "or" Operator
The or operator requires AT LEAST ONE condition to be True. It's less strict than "and". Think of it as: if any option works, we're good.
The "not" Operator
The not operator flips a boolean value. True becomes False, and False becomes True. It's like saying "the opposite of..."
Combining Operators
Because and has higher priority, this evaluates as a or (b and c), not (a or b) and c.
A Real-World Example
> Python's and/or operators return actual values, not just True or False. Toggle between them to see which value gets picked.
x = 0 y = 5 result = x y print(result)
Multiple Assignment
Set several variables in one statement
Assigning Multiple Vars
Swapping Values
Unpacking from Collections
- a, b = b, a
- x, y = get_coordinates()
- quotient, rem = divmod(17, 5)
- temp = a; a = b; b = temp
- result = get_coordinates(); x = result[0]; y = result[1]
- result = divmod(17, 5); quotient = result[0]; rem = result[1]
> Python can swap two variables in one line. Compare simultaneous swap to step-by-step assignment to see why order matters.
a = 1 b = 2 print(a, b)
None and Identity
Check for missing values the right way
Sometimes a variable needs to exist but doesn't have a meaningful value yet. Maybe you're waiting for data to arrive, or a search found nothing. Python has a special value for this: None.
What is None?
None is Python's way of representing "no value" or "nothing." It's not the same as zero (0 is a number). It's not the same as an empty string ("" is still a string). None means the complete absence of a value.
You might use None as a placeholder, or a function might return None to indicate "not found."
Functions and None
In Python, if a function doesn't explicitly return a value, it automatically returns None. Consider a function that just prints something:
greet prints "Hello, Maya" but does not return anything, so result becomes None.
Checking for None
Use is to check for None:
Why "is" Instead of "=="?
The == operator checks if two values are equal. The is operator checks if two variables refer to the exact same object in memory.
- result is None
- value is not None
- if user is None: handle()
- result == None
- value != None
- if not user: handle()
a == b is True (same values). a is b is False (different objects). a is c is True (same object).
is when checking for None. Use == when comparing values of regular types like numbers and strings.> None, 0, and "" are all falsy but they are not the same object. Pick an operator to see the difference.
x = None result = x 0 print(result)
None is the correct signal for "no value here." Functions that search for something and fail, or optional parameters that were not provided, return or default to None.
The is None check is a universal Python convention. You will see it in standard library code, open-source projects, and production codebases across every domain.
None explicitly with is None rather than relying on truthiness. A result of 0 or an empty list is falsy but valid, and a truthiness check would incorrectly treat it as "no result".> You are a data analyst at Datadog building a Python script that reads daily pipeline metrics from an API and prints formatted diagnostic summaries to a shared log file. The script must coerce raw string inputs, compare thresholds, combine conditions, and unpack multiple return values in a single pass.
and error rate exceeds the limit.True or Falseand requires both conditions True, or requires at least one Truenot flips True to False and False to TrueNone represents "no value" - check with is None, not == NonePython Expressions: Intermediate
Making decisions with data
- Category
- Python
- Difficulty
- intermediate
- Duration
- 38 minutes
- Challenges
- 0 hands-on challenges
Topics covered: Type Conversions, Comparison Operators, Logical Operators, Multiple Assignment, None and Identity
Lesson Sections
- Type Conversions (concepts: pyTypeConversion)
Remember from the beginner lesson that every value has a type? An integer like 42 is different from a string like "42", even though they look similar. Sometimes you need to convert between these types. Why Convert Between Types? Data doesn't always arrive in the format you need. Here are common situations where type conversion is essential: For example, if a user types "25" into an age field, your program receives the string "25", not the number 25. To check if they're old enough to vote, you ne
- Comparison Operators
Programs need to make decisions. Should this user be granted access? Is the account balance sufficient? Has the deadline passed? These are all yes-or-no questions, and comparison operators let you ask them. What is a Comparison? Six Comparison Operators Python has six comparison operators. Each one asks a different type of question: Checking if two values are exactly the same: Greater-than and less-than operators compare which side is larger or smaller: Comparing Strings You can compare strings
- Logical Operators
Real-world decisions are rarely simple. "Can this user access the file?" might depend on multiple factors: Are they logged in? Do they have permission? Is the file not locked? Logical operators let you combine multiple conditions into one decision. The Three Logical Operators Python has three logical operators. Think of them as ways to connect yes-or-no questions: The "and" Operator Imagine a nightclub that requires guests to be 21+ AND have valid ID. Both conditions must be met: can_enter is Tr
- Multiple Assignment
Python has a convenient feature that lets you assign values to multiple variables in a single line. This makes certain patterns cleaner and more readable. Assigning Multiple Vars The values on the right are matched up with the variables on the left, in order. Swapping Values In many languages, swapping requires a temporary variable. In Python: Python evaluates the right side first (b, a), then assigns to the left side. No temp variable needed. Unpacking from Collections You can "unpack" a tuple
- None and Identity
What is None? Functions and None Checking for None Why "is" Instead of "=="?