3 Types of Comprehension in Python

8 62
Avatar for lonestranger
1 year ago

Since day 1, listening in our CS3 subject makes me very sleepy despite our teachers excellent way of teaching. You can easily tell that this field is his specialty from how calm he teaches just like a friend having a simple conversation with you.

I always force myself to listen as the class starts but will always end up sleeping or browsing on Facebook minutes after. The longest I've stayed so far was an hour of listening because I just liked to. Today, I realized that my set up is one of the greatest reasons why I seldomly listen.

I always lie in bed while he's discussing because I don't have a table in my room. I do sit sometimes but will lie down later on. So of course, I could really sleep in that position haha. My sister and I fought a little which made me stay in the living room where there's a long table and chairs. I sat there and positioned my phone, got my pen and yellow paper then encouraged myself to listen as my classmates did too.

We've discussed today the list comprehension, dictionary comprehension, and set comprehension in python programme then compared it using for loop. Comprehension in python is accordingly a short and concise way that enables us to construct new sequences with use of sequences itself which have already been defined.

I wasn't able to understand the list comprehension part because my mother came and ate there while having a random conversation with my father. I easily got distracted by a simple commotion while focusing on something so I was absolutely thankful that they finished so soon.

In the dictionary comprehension, the structure when we define a variable is:

output_dict ={key: value for (key, value) in iterable if (key, value satisfy this condition)}

He demonstrated first the used of for loop. Whenever we try to get to the cube root of numbers, create an empty set first and this is the example given below.

USING FOR LOOP

input_list = [1,2,3,4,5,6,7]

output_dict= {}

Then,

for y in input_list:

output_dict[y] = y**3

print (f"Output dictionary using for loop: {output_dict}")

The programme then will run and give you the outputs 1, 8,27,64,125,216,343. But if you'd only want to get the outputs of odd numbers, insert if y % 2 == 1: where % is called modulo which will check if the number is even while == is an equal sign. We can't use the usual = sign as the programme will run error given that we've already use it. With that, the new formula then will become:

for y in input_list:

if y % 2 == 1:

output_dict[y] = y**3

print (f"Output dictionary using for loop: {output_dict}")

As you can see, we'll have 5 lines in using for loop excluding when it's conditional.

USING DICTIONARY COMPREHENSION

When we use dictionary comprehension, 3 lines will suffice with the use of the formula I've mentioned:

output_dict ={key: value for (key, value) in iterable if (key, value satisfy this condition)}

It will look like this:

input_list = [1,2,3,4,5,6,7]

output_dict= {y:y**3 for y in input_list}

print(f"Output dictionary using dictionary comprehension:{output_dict}")

The output then will be,

Output dictionary using dictionary comprehension: {1, 8,27,64,125,216,343}

Another example looked like this one below which I missed his explanations because I spaced out haha.

country = ["Philippines", "Japan", "France"]

capital_city = ["Manila", "Tokyo", "Paris"]

USING FOR LOOP

When we use for loop, we still have to put an empty set and it would look like this:

output_dict = {}

for (key, value) in zip(country, capital_city)

output_dict[key] = value

print (f"Output Dictionary using for loop: {output_dict}.")

It will give then the capital of the countries given above.

USING DICTIONARY COMPREHENSION

Applying the likely given formula, we have:

country = ["Philippines", "Japan", "France"]

capital_city = ["Manila", "Tokyo", "Paris"]

output_city = {x:y for (x,y) in zip (country, capital _city}

print(f"Output Dictionary using dictionary comprehension: {output_dict}.")

However, if it is conditional like when you only want to get the output of those countries that have 6 or less than characters, add if len (x) <=6: .

In using for loop, add it in between:

for (key, value) in zip(country, capital_city)

if len(x) <=6:

output_dict[key] = value

In using dictionary comprehension, add here:

output_city = {x:y for (x,y) in zip (country, capital _city if len(x) <=6:}

But if it is compound like when you only want to get an output of 5 characters, add len (y)<5 after if len(x) <=6: .

Lastly, is the set comprehension. I'll only be putting the examples.

input_list = [1,2,2,3,3,4,4,4,5,5,6,7,8,8]

USING FOR LOOP

input_list = [1,2,2,3,3,4,4,4,5,5,6,7,8,8]

output_set = set()

for z in input_list:

if z % 2 ==0:

output_set.add(z)

print(f"Output set using for loop: {output_set}")

USING SET COMPREHENSION

input_list = [1,2,2,3,3,4,4,4,5,5,6,7,8,8]

output_set = {z for z in input_list}

print(f"Output set using set comprehension: {output_set}")

But if you choose to print out those even ones, just add if z % 2 ==0 and if you want to square them, add z**2 as well.


These are my notes and I hope our prof. will only give problems like that unlike the previous ones huhu. My apologies also for those who just encountered python today if there are parts that you can't understand. Explaining how did it happen will take more blogs so I've decided to get back from the start and share some of my learnings here as python is a great tool in analyzing data and many more. That's all for today. Just drop your thoughts in the comment section below.

Thanks. Owe you big time!♡

5
$ 3.32
$ 3.25 from @TheRandomRewarder
$ 0.03 from @Ling01
$ 0.02 from @Eunoia
+ 2
Sponsors of lonestranger
empty
empty
empty
Avatar for lonestranger
1 year ago

Comments

Omg sumakit ulo ko.. yung huli kong encounter nyan e sa para pa sa SAP ahahhahahahhaa lusaw but kudos to you kase alam kong mahirap yan

$ 0.02
1 year ago

ano pala yung SAP? nakakasakit talaga ng ulo lalo na kapag may pa quiz tas hindi ako nakinig sa lecture aahahaaja

$ 0.00
1 year ago

Its like data system coding ginagamit sya mostly sa mga companies

$ 0.00
1 year ago

ahh i seee thankss

$ 0.00
1 year ago

Keep up the good learning in coding, sorry I have nothing to say for I am not in that field hehe. Yiee future programmer, dive into Crypto platform soon.

$ 0.02
1 year ago

I understand hehe and thank youu.

$ 0.00
1 year ago

Naol kabalo na sa programming 😭

$ 0.02
1 year ago

nah karon ra gani ta naminaw HAHAHA

$ 0.00
1 year ago