Programming paradigms · Paradigma pemrograman
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.
Apa itu paradigma?
- Paradigma pemrograman adalah gaya dalam mengorganisir program.
- Masalah yang sama dapat diselesaikan dengan gaya yang berbeda.
- Python memungkinkan Anda mencampur beberapa paradigma dalam satu program.
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.
Imperatif dan Prosedural
- Kode imperatif adalah daftar langkah yang mengubah status program.
- Kode ini menyatakan bagaimana melakukan sesuatu, langkah demi langkah (perulangan, penugasan).
- Gaya prosedural mengelompokkan langkah-langkah tersebut ke dalam prosedur dan fungsi yang dapat digunakan kembali.
def total(nums):
result = 0
for n in nums:
result = result + n
return result
print(total([2, 4, 6]))
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.
Berorientasi Objek
- Kode berorientasi objek menggabungkan data dan metode yang bekerja padanya menjadi objek.
- Anda memodelkan masalah sebagai sekumpulan kelas yang saling berkomunikasi.
- Cocok untuk program besar karena setiap kelas merupakan bagian yang mandiri.
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)
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.
Deklaratif
- Kode deklaratif menyatakan apa yang Anda inginkan, bukan bagaimana menghitungnya.
- Bahasa atau pustaka akan menentukan langkah-langkahnya untuk Anda.
- Contoh: kueri basis data (SQL), atau komprehensi list Python.
nums = [1, 2, 3, 4, 5]
print(sum(nums)) # say WHAT: the total
print([n * n for n in nums]) # a list comprehension
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.
Level Rendah
- Kode level rendah berada dekat dengan mesin: assembly dan kode mesin.
- CPU menjalankannya secara langsung, sehingga cepat tetapi sulit ditulis.
- Bahasa level tinggi seperti Python diterjemahkan ke dalamnya sebelum dijalankan.
Common mistakes
- Procedural is steps; object-oriented is objects with data and methods; declarative describes the result.
- Match the paradigm to the problem.
Kesalahan umum
- Prosedural berupa langkah; berorientasi objek adalah objek dengan data dan metode; deklaratif menggambarkan hasil.
- Sesuaikan paradigma dengan masalahnya.
Now you try
- Each task solves a small problem in a different paradigm.
- Press Check answer to test your code.
Sekarang Anda coba
- Setiap tugas menyelesaikan masalah kecil dengan paradigma yang berbeda.
- Tekan Periksa jawaban untuk menguji kode Anda.
Procedural style. Write sum_list(nums) that uses a loop and an accumulator to add up the list and return the total. · Gaya prosedural. Tulis sum_list(nums) yang menggunakan perulangan dan akumulator untuk menjumlahkan daftar dan mengembalikan totalnya.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Declarative style. Write evens(nums) that returns a list of the even numbers, using a single list comprehension [... for ... if ...]. · Gaya deklaratif. Tulis evens(nums) yang mengembalikan daftar bilangan genap, menggunakan satu pembuat list (list comprehension) [... for ... if ...].
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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. · Gaya pemrograman berorientasi objek. Tulis kelas Bag yang __init__(self, items)-nya menyimpan sebuah daftar, dan metode total(self)-nya mengembalikan jumlah item-item tersebut.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.