파이썬S5: OOP(객체지향프로그래밍)

소바·2022년 10월 17일
0

파이썬

목록 보기
5/5

예제: 최대값 찾기


class & classmethod & staticmethod

dir

람다

Looks like it's sorting it for me, but it's sorting it based on this first item.
But I want this second item or what we call the second key to be the sorting factor.

What I'm saying is, hey, I want you to run the sort function.
And the key for the sort function when I'm sorting through everything is I want it to iterate over each item that I'm going to get that is X is going to be this tuple.

And I want you to use the value which we're going to return, which is the second one.

So the key is always going to be by the second item.We're able to sort By the second item.

리스트 컴프리핸션(list comprehension)

더 쉬운 방법은?

I created a list that contains the characters from Hello.

사용법

So when I say param, I mean parameter, or in our case it could be a variable so we can name it whatever we want

It's a way for us to quickly create lists with Python instead of looping and using append or things that we've seen before.

응용

  1. The way we're able to do this is to have an expression here of what we want to do with each item that we're iterating over we loop. this is now an expression of how we want to act upon each character.

  2. Using a for loop through an iterable, we give it a variable which we're going to act upon.

  3. we also have an option to add a conditional and say, hey, only include it into the list based on this condition, based on a true or false.

장점 및 단점

So although this makes code shorter, maybe even cleaner, it does take away a bit of its readability.
So it might be better to just create a descriptive function like generate list of even numbers or something.

딕셔너리 컴프리핸션

The first part is what we want to do with the data.
In our case, I wanted key and value, which I was going to extract here in our for loop.

I could have named this whatever I want though I could have done K and V and you might see this commonly in python code just short term for key and value.
And then I wanted to do the power of two to the value.
So then this is what I wanted to do. This is the action.

This is the for loop that we did. So the for loop was, hey, extract the key and value using the dictionary dot items.
Remember I needed to have two values so I can fill in key and value. And then the value you multiply by two or to the power of two.

profile
소바보이

0개의 댓글