Python crash course (if you programmed in something else before) — Part 2

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:

Python
numbers = []
for x in range(10):
  numbers.append(x * x)

You can write:

Python
numbers = [x * x for x in range(10)]

You can also add conditions:

Python
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.

Python
a, b = 10, 20

This also makes variable swapping trivial:

Python
a, b = b, a

In 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:

Python
i = 0

for item in items:  
  print(i, item)<br>    
  i += 1

Use:

Python
for i, item in enumerate(items):    
  print(i, item)

You can even choose the starting index:

Python
for i, item in enumerate(items, start=1):

Zip

Another extremely handy tool is zip, which allows you to iterate over multiple collections simultaneously.

Python
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.

Python
squares = {x: x * x for x in range(5)}

Result:

Python
{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.

Python
a, b, c = [1, 2, 3]

You can also capture remaining elements:

Python
a, *middle, b = [1, 2, 3, 4, 5]

Result:

Python
a = 1
middle = [2, 3, 4]
b = 5

This becomes very useful when dealing with structured data.


Inline conditionals

Python supports ternary expressions.

Instead of:

Python
if x > 10:    
  result = "big"
else:    
  result = "small"

You can write:

Python
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.

Python
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:

Python
nums = [2, 4, 6, 8]
all_even = all(n % 2 == 0 for n in nums)

Check if any element matches a condition:

Python
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.

Python
value = my_dict["key"]

Safer version:

Python
value = my_dict.get("key", "default")

If "key" doesn’t exist, "default" is returned instead.


Lambda functions

Lambdas are small anonymous functions.

Python
square = lambda x: x * x
print(square(5))

They’re most commonly used with sorting:

Python
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.

Python
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.

Leave a Reply

Your email address will not be published. Required fields are marked *