Commit 7bd11f9c authored by insun park's avatar insun park
Browse files

feat(courses): 핵심 요약(quick_review) 문서 추가

parent 0620f2cb
# Part 2: Python Core Syntax - Quick Review for Experienced Developers
This document provides a fast-paced review of Python's core syntax for developers already familiar with programming concepts.
---
### 1. Variables and Basic Data Types
- **Dynamic Typing**: No explicit type declaration needed.
```python
my_string = "hello" # str
my_int = 100 # int
my_float = 3.14 # float
my_bool = True # bool
```
- **F-strings (Formatted String Literals)**: Convenient way to embed expressions inside string literals.
```python
name = "World"
print(f"Hello, {name}!")
```
---
### 2. Core Data Structures
A brief overview of Python's built-in data structures.
| Structure | Syntax | Mutable? | Ordered? | Use Case |
| :--- | :--- | :---: | :---: | :--- |
| **List** | `[1, 2, 3]` | Yes | Yes | General-purpose, ordered collection. |
| **Tuple** | `(1, 2, 3)` | No | Yes | Immutable data, function return values, dictionary keys. |
| **Dictionary**| `{'a': 1}` | Yes | Yes (3.7+) | Key-value storage. |
| **Set** | `{1, 2, 3}` | Yes | No | Uniqueness, mathematical set operations. |
---
### 3. "Pythonic" Idioms
- **List Comprehension**: Create lists concisely.
```python
# squares of even numbers from 0 to 9
squares = [x**2 for x in range(10) if x % 2 == 0]
# [0, 4, 16, 36, 64]
```
- **Dictionary/Set Comprehension**:
```python
# Dictionary
# {k: v for k, v in some_iterable}
# Set
# {x for x in another_iterable}
```
- **Unpacking**:
```python
a, b, *remaining = [1, 2, 3, 4, 5]
# a=1, b=2, remaining=[3, 4, 5]
```
---
### 4. Control Flow
- **Indentation**: Python uses indentation (typically 4 spaces) to define code blocks. There are no curly braces `{}`.
- **`if/elif/else`**:
```python
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "F"
```
- **`for` loop**: Iterates over any sequence.
- Use `enumerate()` to get both index and value.
```python
for i, item in enumerate(['a', 'b', 'c']):
print(i, item)
```
- **`while` loop**:
```python
count = 5
while count > 0:
count -= 1
```
---
### 5. Functions
- **Definition**: Use the `def` keyword.
- **Type Hinting** (Optional but recommended):
```python
def greet(name: str) -> str:
return f"Hello, {name}"
```
- **Multiple Return Values**: Returns a tuple.
```python
def get_point():
return 10, 20
x, y = get_point() # x=10, y=20
```
---
This covers the fundamental syntax. For a deeper dive, refer to the main [part_2_python_core_syntax.md](./part_2_python_core_syntax.md) document.
\ No newline at end of file
# Part 3: Python Collections - Quick Review for Experienced Developers
This document provides a quick reference to Python's advanced and specialized collection types from the `collections` module.
---
### 1. Sorting: `sort()` vs `sorted()`
- **`list.sort()`**: In-place sort. Modifies the original list and returns `None`.
```python
my_list = [3, 1, 2]
my_list.sort() # my_list is now [1, 2, 3]
```
- **`sorted()`**: Built-in function. Returns a *new* sorted list, leaving the original unchanged.
```python
original_list = [3, 1, 2]
new_list = sorted(original_list) # new_list is [1, 2, 3], original_list is unchanged
```
---
### 2. Tuple Unpacking
- **Basic Unpacking**:
```python
a, b = (10, 20)
```
- **Extended Unpacking (`*`)**:
```python
first, *middle, last = [1, 2, 3, 4, 5]
# first -> 1
# middle -> [2, 3, 4]
# last -> 5
```
---
### 3. Specialized Collections in `collections`
A brief overview of useful classes in the `collections` module.
| Class | Description | Common Use Case |
| :--- | :--- | :--- |
| `Counter` | A `dict` subclass for counting hashable objects. | Counting item frequencies, finding most common items. |
| `defaultdict`| A `dict` subclass that calls a factory function for missing keys. | Grouping items into lists or other collections without checking for key existence. |
| `deque` | "Double-ended queue". Fast appends and pops from both ends. | Implementing queues, stacks, or keeping a list of "last N" items. |
| `namedtuple` | Factory function for creating tuple subclasses with named fields. | Creating simple, immutable objects with attribute access for better readability. |
---
### 4. Code Examples
- **`Counter`**:
```python
from collections import Counter
counts = Counter(['a', 'b', 'a', 'c', 'a'])
# Counter({'a': 3, 'b': 1, 'c': 1})
print(counts.most_common(1)) # [('a', 3)]
```
- **`defaultdict`**:
```python
from collections import defaultdict
s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4)]
d = defaultdict(list)
for k, v in s:
d[k].append(v)
# defaultdict(<class 'list'>, {'yellow': [1, 3], 'blue': [2, 4]})
```
- **`deque`**:
```python
from collections import deque
last_five = deque(maxlen=5)
for i in range(10):
last_five.append(i)
# deque([5, 6, 7, 8, 9], maxlen=5)
```
- **`namedtuple`**:
```python
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(11, y=22)
print(p.x, p.y) # 11 22
```
---
For more detailed explanations and examples, please see the main [part_3_python_collections.md](./part_3_python_collections.md) document.
\ No newline at end of file
# Part 4: OOP in Python - Quick Review for Experienced Developers
This document provides a concise overview of Object-Oriented Programming (OOP) syntax and concepts in Python for developers familiar with OOP principles.
---
### 1. Classes and Objects
- **Basic Definition**: Use the `class` keyword. The first argument to instance methods is conventionally `self`.
- **`__init__`**: The constructor method.
- **`@dataclass`**: A decorator that auto-generates boilerplate code like `__init__()`, `__repr__()`, `__eq__()`, etc. Highly recommended for data-centric classes.
```python
from dataclasses import dataclass
@dataclass
class Point:
x: float
y: float
def distance_from_origin(self) -> float:
return (self.x**2 + self.y**2) ** 0.5
p = Point(3, 4)
print(p.distance_from_origin()) # Output: 5.0
```
---
### 2. Inheritance
- **Syntax**: `class ChildClass(ParentClass):`
- **`super()`**: Used to call methods from the parent class.
- **Method Overriding**: Simply define a method with the same name in the child class.
```python
@dataclass
class Point3D(Point): # Inherits from Point
z: float
# Overriding the method
def distance_from_origin(self) -> float:
# Calling parent's method using super()
d_2d = super().distance_from_origin()
return (d_2d**2 + self.z**2) ** 0.5
p3d = Point3D(3, 4, 12)
print(p3d.distance_from_origin()) # Output: 13.0
```
---
### 3. Encapsulation
- **Convention, not Enforcement**: Python doesn't have `private` or `protected` keywords like Java/C++.
- **`_` (Single Underscore) Prefix**: A convention indicating an attribute is for internal use (protected).
- **`__` (Double Underscore) Prefix**: "Name mangling". The interpreter changes the name to `_ClassName__attribute`, making it harder to access from outside but not truly private.
```python
class MyClass:
def __init__(self):
self._internal_var = 1 # Convention for internal use
self.__mangled_var = 2 # Name is mangled
def get_mangled(self):
return self.__mangled_var
obj = MyClass()
print(obj._internal_var) # 1 (Accessible)
# print(obj.__mangled_var) # AttributeError
print(obj._MyClass__mangled_var) # 2 (Still accessible if you know the mangled name)
```
---
### 4. Abstraction
- **`abc` module**: Use the `ABC` (Abstract Base Class) and `@abstractmethod` decorator to define abstract classes and methods.
- Subclasses must implement all abstract methods to be instantiated.
```python
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self) -> float:
pass
class Square(Shape):
def __init__(self, side):
self.side = side
def area(self) -> float: # Must implement this method
return self.side ** 2
# s = Shape() # TypeError: Can't instantiate abstract class
sq = Square(5) # OK
```
---
### 5. Magic Methods (Dunder Methods)
Methods with double underscores (`__method__`) that allow you to emulate the behavior of built-in types.
| Method | Description | Called by |
| :--- | :--- | :--- |
| `__init__(self, ...)` | Object initializer | `MyClass(...)` |
| `__str__(self)` | String representation for end-users | `str(obj)`, `print(obj)` |
| `__repr__(self)` | Unambiguous representation for developers | `repr(obj)` |
| `__len__(self)` | Length of the object | `len(obj)` |
| `__eq__(self, other)`| Equality comparison | `obj1 == obj2` |
| `__add__(self, other)`| Addition operator | `obj1 + obj2` |
---
For more detailed explanations, refer to the main [part_4_object_oriented_programming.md](./part_4_object_oriented_programming.md) document.
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment