If you haven’t read Part 1, start here
In the first post we covered the absolute basics: variables, lists, loops, range usage and simple functions. The goal there was to get from zero Python syntax to something you can actually run quickly.
This second part focuses on small Python syntax features that are extremely useful in everyday code. If you already programmed in another language, many of these will feel familiar conceptually, but Python often provides shorter or more expressive ways to write them.
These are the kinds of things that make Python code feel Pythonic.
List comprehensions
A list comprehension is a compact way to build lists.
Instead of writing something like:
numbers = []
for x in range(10):
numbers.append(x * x)You can write:
numbers = [x * x for x in range(10)]You can also add conditions:
even = [x for x in range(20) if x % 2 == 0]Conceptually this is just a map + filter, but Python turns it into very readable syntax.
Multiple assignment
Python lets you assign multiple variables in one line.
a, b = 10, 20This also makes variable swapping trivial:
a, b = b, aIn many languages you need a temporary variable. In Python this comes almost for free.
Enumerate
Often you want both the element and its index when looping.
Instead of:
i = 0
for item in items:
print(i, item)<br>
i += 1Use:
for i, item in enumerate(items):
print(i, item)You can even choose the starting index:
for i, item in enumerate(items, start=1):Zip
Another extremely handy tool is zip, which allows you to iterate over multiple collections simultaneously.
names = ["Alice", "Bob", "Charlie"]
scores = [90, 85, 88]
for name, score in zip(names, scores):
print(name, score)This avoids manually indexing both lists.
Dictionary comprehension
Just like list comprehensions, but for dictionaries.
squares = {x: x * x for x in range(5)}Result:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}This is extremely useful when transforming data structures.
Unpacking
Python allows unpacking elements from sequences.
a, b, c = [1, 2, 3]You can also capture remaining elements:
a, *middle, b = [1, 2, 3, 4, 5]Result:
a = 1
middle = [2, 3, 4]
b = 5This becomes very useful when dealing with structured data.
Inline conditionals
Python supports ternary expressions.
Instead of:
if x > 10:
result = "big"
else:
result = "small"You can write:
result = "big" if x > 10 else "small"Short, readable, and very common in Python code.
Context managers (with)
File handling is much safer with with.
with open("file.txt") as f:
content = f.read()When the block ends, the file is automatically closed.
Without with, it’s easy to forget to close files properly.
any() and all()
These two built-in functions are extremely useful.
Check if all elements match a condition:
nums = [2, 4, 6, 8]
all_even = all(n % 2 == 0 for n in nums)Check if any element matches a condition:
any_big = any(n > 10 for n in nums)These often replace entire loops.
Safe dictionary access
Accessing missing keys in dictionaries normally raises an error.
value = my_dict["key"]Safer version:
value = my_dict.get("key", "default")If "key" doesn’t exist, "default" is returned instead.
Lambda functions
Lambdas are small anonymous functions.
square = lambda x: x * x
print(square(5))They’re most commonly used with sorting:
people = [("Alice", 25), ("Bob", 20)]
people.sort(key=lambda x: x[1])Here we sort by age.
f-strings (string formatting)
Modern Python string formatting uses f-strings.
name = "Alice"
age = 25print(f"{name} is {age} years old")This is far cleaner than older formatting methods.
Final thoughts
Once you learn these small syntax features, Python starts feeling much more expressive.
Most day-to-day Python code uses things like:
- list comprehensions
- enumerate
- zip
- dictionary
.get() - f-strings
Combined with the basics from Part 1, you now have a solid foundation to start writing useful Python scripts quickly.