Programming paradigms
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(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.
한국어
패러다임이란 무엇인가?
- 프로그래밍 패러다임은 프로그램을 조직화하는 스타일입니다.
- 같은 문제도 다양한 스타일로 해결할 수 있습니다.
- Python은 하나의 프로그램에서 여러 패러다임을 혼합할 수 있게 합니다.
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.
한국어
명령어 기반 및 절차적
- 명령어 기반 코드는 프로그램 상태를 변경하는 단계들의 나열입니다.
- 루프와 할당 등을 통해 무언가를 어떻게 수행할지를 단계별로 설명합니다.
- 절차적 스타일은 이러한 단계들을 재사용 가능한 절차와 함수로 묶습니다.
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.
한국어
객체 지향 프로그래밍
- 面向对象 코드는 데이터와 그에 작용하는 메서드를 객체에 묶습니다.
- 문제를 서로 대화하는 클래스들의 집합으로 모델링합니다.
- 각 클래스가 독립적인 부분이기 때문에 큰 프로그램에 적합합니다.
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.
한국어
선언적
- 선언적 코드는 계산 방법을 아닌 원하는 결과를 명시합니다.
- 언어 또는 라이브러리가 자동으로 단계를 결정합니다.
- 예: 데이터베이스 쿼리(SQL), 또는 Python의 리스트 컴프리헨션.
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.
- The CPU runs it directly, so it is fast but hard to write.
- High-level languages like Python are translated down to it before running.
한국어
저수준
- 저수준 코드는 기계에 가깝습니다: 어셈블리와 머신 코드.
- CPU가 직접 실행하므로 빠르지만 작성하기 어렵습니다.
- 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.
한국어
이제 직접 해보기
- 각 과제는 서로 다른 패러다임으로 작은 문제를 해결합니다.
- Answer 확인 버튼을 눌러 코드를 테스트하세요.
Procedural style. Write sum_list(nums) that uses a loop and an accumulator to add up the list and return the total.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Declarative style. Write evens(nums) that returns a list of the even numbers, using a single list comprehension [... for ... if ...].
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Object-oriented style. Write a class Bag whose __init__(self, items) stores a list, and whose method total(self) returns the sum of those items.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.