Okay, Nattar.
Day 2.
Yesterday, I built a simple Text Analyzer. Today, I decided not to build another big project. Instead, I wanted to sit down and solve 25 small Python problems.
And apparently, today's main character was: Comprehensions. 😅
And then, just when I thought I was done, I got introduced to generators and yield.
So let's see what I actually learned today.
What is a comprehension?
Before today, I had seen comprehensions quite a few times. But seeing them and actually understanding them are two different things.
The simplest way I can explain it to myself is:
A comprehension is a shorter way of creating a collection by looping through something, with an option to apply a condition.
For example, the normal way of creating a list of squares would be,
numbers = [1, 2, 3, 4, 5] squares = [] for num in numbers: squares.append(num * num)
With a list comprehension, I can write,
numbers = [1, 2, 3, 4, 5] squares = [num * num for num in numbers]
Same idea. Much shorter.
At first glance, the comprehension looks a little weird. My brain wants to read it from left to right like normal code, which is probably not the best idea.
So I'm trying to get comfortable reading it as:
"For every number in numbers, give me number squared."
Comprehensions can also include conditions. For example, if I only want even numbers,
numbers = [1, 2, 3, 4, 5, 6] even_numbers = [num for num in numbers if num % 2 == 0]
Again, same logic. Just compressed into one line.
I'm definitely not going to claim that I'm a comprehension expert after one day. But I can finally look at one and understand what it is trying to do.
The main part of today's challenge was solving 25 problems based around comprehensions. I worked through different variations involving:
- Lists
- Numbers
- Strings
- Conditions
- Filtering
- Transforming values
- List comprehensions
- Set comprehensions
- Dictionary comprehensions
The interesting part wasn't really the individual problems. It was seeing the same basic idea appear in different situations.
And then came generators...
I thought the comprehension part was going to be the main learning of today. Then I started working on a generator-based file reader. And this one made something click for me. Especially because I had always thought about reading a file like this:
"Open the file → load everything → work with it."
But generators introduced a different way of thinking.
I wrote a simple function.
def read_file(filename):
with open(filename,"r") as file:
for line in file:
yield line
Then I created the generator.
reader = read_file("sample.txt")
And used it like this.
for line in reader:
print(line)
That's it. But there is actually something interesting happening here.
What is yield doing?
This was the part I had to think about.
When Python reaches:
yield line
It doesn't return all the lines at once. It gives me one line and pauses the function. When I ask for the next value, it continues from where it stopped and gives me the next line.
That makes generators particularly useful when working with large amounts of data. And considering where I'm heading with data and AI...
I have a feeling this concept is going to show up again.
I learnt:
- List comprehensions
- Set comprehensions
- Dictionary comprehensions
- Conditions inside comprehensions
- Transforming data while creating collections
- yield
- Generators
- next()
- Iterating through a generator
- Reading files line by line
- Why generators can be useful for large files
But more importantly, I learned something about how I learn Python. Sometimes I understand something when I see the code.But I really understand it when I can explain why the code behaves that way.
So...
Day 2: done. ✅



Top comments (0)