diff --git a/chapter1/comprehension.py b/chapter1/comprehension.py new file mode 100644 index 0000000..d10734e --- /dev/null +++ b/chapter1/comprehension.py @@ -0,0 +1,51 @@ +################################################## +# 리스트 +# 기본 +# numbers = [] +# for i in range(5): +# numbers.append(i) +# 리스트 컴프리헨션 사용 +numbers = [i for i in range(5)] +print('numbers :',numbers) + +# 고급(중첩 루프) +# pairs = [] +# for x in range(3): +# for y in range(3): +# pairs.append((x,y)) +pairs = [(x, y) for x in range(3) for y in range(3)] +print('pairs :',pairs) + +# 고급(조건 및 중첩 루프) +# not_same_pairs = [] +# for x in range(3): +# for y in range(3): +# if x != y: +# not_same_pairs.append((x, y)) +not_same_pairs = [(x, y) for x in range(3) for y in range(3) if x != y] +print('not_same_pairs :', not_same_pairs) + +# 문자열 +# words = ['java', 'python', 'dart'] +# upper_words = [] +# for word in words: +# upper_words.append(word.upper()) +words = ['java', 'python', 'dart'] +upper_words = [word.upper() for word in words] +print('upper_words :',upper_words) + +################################################## +# 딕셔너리 +dic_numbers = {i : i for i in range(5)} +print(dic_numbers) +################################################## +################################################## +# 집합 +data = [1, 2, 2, 3, 4, 4] +set_numbers = {i for i in data} +print(set_numbers) +################################################## +# 제너레이터 +generator_numbers = {i for i in data} +print(list(generator_numbers)) +################################################## \ No newline at end of file diff --git a/chapter1/decorator.py b/chapter1/decorator.py new file mode 100644 index 0000000..4d44622 --- /dev/null +++ b/chapter1/decorator.py @@ -0,0 +1,17 @@ +def hello(): + print("hello") +def deco(fn): + def deco_hello(): + print("*" * 20) + fn() + print("*" * 20) + return deco_hello +@deco +def hello2(): + print("hello 2") +hello() +deco_hello = deco(hello) +deco_hello() +hello = deco(hello) +hello() +hello2() \ No newline at end of file diff --git a/chapter1/generator.py b/chapter1/generator.py new file mode 100644 index 0000000..c7c2920 --- /dev/null +++ b/chapter1/generator.py @@ -0,0 +1,18 @@ +def gen_range(start, stop): + while start < stop: + yield start + start += 1 +res = [i for i in gen_range(0, 10)] +print(res) + +def generator_send(): + main_routine_value = 0 + while True: + main_routine_value = yield + yield main_routine_value * 2 +gen = generator_send() +print(gen) +next(gen) +print(gen.send(100)) # 200 +next(gen) +print(gen.send(300)) # 600 \ No newline at end of file diff --git a/chapter1/magic_method.py b/chapter1/magic_method.py new file mode 100644 index 0000000..e49c521 --- /dev/null +++ b/chapter1/magic_method.py @@ -0,0 +1,28 @@ +class Fruit(object): + def __init__(self, name, price): + self._name = name + self._price = price + + def __add__(self, target): + return self._price + target._price + + def __sub__(self, target): + return self._price - target._price + + def __mul__(self, target): + return self._price * target._price + + def __truediv__(self, target): + return self._price / target._price + + def __str__(self): + return self._name + +apple = Fruit("사과", 100000) +durian = Fruit("두리안", 50000) + +print(apple + durian) +print(apple - durian) +print(apple * durian) +print(apple / durian) +print(f"{apple}와 {durian}") \ No newline at end of file diff --git a/chapter1/pythonic_code.md b/chapter1/pythonic_code.md new file mode 100644 index 0000000..5c3abc7 --- /dev/null +++ b/chapter1/pythonic_code.md @@ -0,0 +1,67 @@ +## 학습 내용 요약 + + + +- 제너레이터는 지연 평가 방식이 가능하기 때문에 객체에 엄청나게 많은 데이터를 넣어 생성해야 할 때에는 컴프리헨션보다 효율성 측면에서 매우 뛰어나다. +- 파이썬에서 존재하는 타입들은 모두 클래스이기 때문에 매직 메소드를 이용하여 연산이 가능하다. + +## 핵심 개념 + + + +#### 딕셔너리로 제너레이터 컴프리헨션 불가능하다. + +- 딕셔너리는 key value로 구성되어야 하기 때문 + +#### 언패킹은 함수에서 사용할때 주의해야함. + +- 인자를 해체하는 개념이기 때문에 해체된 인자의 개수가 함수의 인자 개수와 다르다 에러가 발생함 + +#### 제너레이터는 컴프리헨션보다 메모리 효울성이 좋다. + +- 제너레이터는 지연 평가 방식이 가능하기 때문에 메모리르 더 효율적으로 사용할 수 있다. + +#### 함수에 데코레이터 편하게 적용하려면 @를 사용하면된다. + +- @데코레이터함수 + +## 실습 예제 + + + +- 컴프리헨션 : comprehension.py +- 언패킹 : unpacking.py +- 제너레이터 : generator.py +- 매직메소드 : magic_method.py +- 데코레이터 : decorator.py + +## 참고 자료 + + + +#### 컴프리헨션 + +- https://incurio.tistory.com/entry/Python-리스트-컴프리헨션List-Comprehension-완벽-가이드?utm_source=chatgpt.com#google_vignette + +#### 언패킹 + +- https://ground90.tistory.com/131 + +#### 제너레이터 + +- https://velog.io/@jewon119/TIL30.-Python-제너레이터Generator-개념-정리 + +#### 매직메소드 + +- https://tibetsandfox.tistory.com/42 + +#### 데코레이터 + +- https://wikidocs.net/160127 + +## 체크리스트 + +- [x] 주제에 대한 핵심 내용을 다루고 있다 +- [x] 실습 가능한 코드 예제가 포함되어 있다 +- [x] 마크다운 문법이 올바르게 적용되었다 +- [x] 참고 자료 출처가 명시되어 있다 diff --git a/chapter1/unpacking.py b/chapter1/unpacking.py new file mode 100644 index 0000000..101aac4 --- /dev/null +++ b/chapter1/unpacking.py @@ -0,0 +1,24 @@ +# 기본 언패킹 +a, b, c = (10, 20, 30) +print(a, b, c) # 10 20 30 + +# 가변인자 언패킹 +x, *y, z = [1, 2, 3, 4, 5] +print(x, y, z) # 1 [2, 3, 4] 5 + +# 함수 호출 시 언패킹 +def f(p, q, r): + return p + q + r + +nums = (4, 5, 6) +print(f(*nums)) + +# 키워드 인자 언패킹 +d = {'p': 1, 'q': 2, 'r': 3} +print(f(**d)) # 6 + +# 딕셔너리 병합 언패킹 +d1 = {'a': 1, 'b': 2} +d2 = {'c': 3} +d_merged = {**d1, **d2} +print(d_merged) \ No newline at end of file