이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
What is a paradigm?
- A programming paradigm is a style of organising a program.
- The same problem can be solved in different styles.
- Python lets you mix several paradigms in one program.

English
Imperative and procedural
- Imperative code is a list of steps that change the program's state.
- It says how to do something, step by step (loops, assignments).
- Procedural style groups those steps into reusable procedures and functions.
English
def total(nums):
result = 0
for n in nums:
result = result + n
return result
print(total([2, 4, 6]))
English
Object-oriented
- Object-oriented code bundles data and the methods that act on it into objects.
- You model the problem as a set of classes that talk to each other.
- It suits large programs because each class is a self-contained part.
English
class Counter:
def __init__(self):
self.count = 0
def add(self):
self.count = self.count + 1
c = Counter()
c.add()
c.add()
print(c.count)
English
Declarative
- Declarative code says what you want, not how to compute it.
- The language or library works out the steps for you.
- Examples: a database query (SQL), or a Python list comprehension.
English
nums = [1, 2, 3, 4, 5]
print(sum(nums)) # say WHAT: the total
print([n * n for n in nums]) # a list comprehension
English
Low-level
- Low-level code is close to the machine: assembly and machine code.
- A CPU executes machine code directly; assembly language must first be translated into machine code by an assembler.
- High-level code must be translated into machine code. A compiler translates the whole program before it runs (C); an interpreter translates and runs it one statement at a time (Python).
English
Common mistakes
- Procedural is steps; object-oriented is objects with data and methods; declarative describes the result.
- Match the paradigm to the problem.
English
Now you try
- Each task solves a small problem in a different paradigm.
- Press Check answer to test your code.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Procedural style. Write sum_list(nums) that uses a loop and an accumulator to add up the list and 반환 the total.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Declarative style. Write evens(nums) that 환원 a list of the even numbers, using a single list comprehension [... for ... if ...].
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Object-oriented style. Write a class Bag whose __init__(self, items) stores a list, and whose method total(self) 환원 the sum of those items.
출력을 보려면 '실행'을 클릭하세요.