Appendix B: Python Quick Reference
Panduan Cepat Python untuk Machine Learning
Appendix B: Python Quick Reference
Catatan
Appendix ini adalah referensi cepat untuk konsep Python essensial yang digunakan dalam kursus Machine Learning. Untuk penjelasan mendalam, kunjungi dokumentasi resmi Python.
B.1 Tipe Data Fundamental
Tipe Data Dasar
| Tipe | Deskripsi | Contoh |
|---|---|---|
int |
Bilangan bulat | x = 42 |
float |
Bilangan desimal | y = 3.14 |
str |
Teks | name = "Python" |
bool |
Nilai logika | flag = True |
None |
Nilai kosong | value = None |
Tipe Data Koleksi
# List (terurut, dapat diubah)
fruits = ['apple', 'banana', 'cherry']
fruits[0] # 'apple'
fruits.append('orange')
# Tuple (terurut, tidak dapat diubah)
coordinates = (10, 20)
x, y = coordinates # unpacking
# Dictionary (key-value pairs)
person = {'name': 'Alice', 'age': 30, 'city': 'Jakarta'}
person['name'] # 'Alice'
person['age'] = 31 # update value
# Set (unik, tidak terurut)
unique_numbers = {1, 2, 3, 3, 2} # {1, 2, 3}
unique_numbers.add(4)B.2 Variabel dan Operator
Operator Aritmetika
a = 10
b = 3
print(a + b) # 13 (penjumlahan)
print(a - b) # 7 (pengurangan)
print(a * b) # 30 (perkalian)
print(a / b) # 3.333... (pembagian)
print(a // b) # 3 (pembagian integer)
print(a % b) # 1 (modulo/sisa)
print(a ** b) # 1000 (pangkat)Operator Perbandingan & Logika
x = 5
print(x == 5) # True
print(x != 5) # False
print(x > 3 and x < 10) # True
print(x < 0 or x > 0) # True
print(not x == 10) # TrueB.3 Control Flow
If-Elif-Else
score = 85
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
else:
grade = 'C'Loop (For & While)
# For loop
for i in range(5):
print(i) # 0, 1, 2, 3, 4
for fruit in ['apple', 'banana', 'cherry']:
print(fruit)
# While loop
count = 0
while count < 3:
print(count)
count += 1
# List comprehension (lebih Pythonic)
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]B.4 Functions dan Lambda
Fungsi Biasa
def calculate_mean(values):
"""Menghitung rata-rata dari list nilai."""
return sum(values) / len(values)
mean = calculate_mean([10, 20, 30]) # 20
# Fungsi dengan default arguments
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
greet("Alice") # "Hello, Alice!"
greet("Bob", "Hi") # "Hi, Bob!"Lambda (Anonymous Functions)
# Fungsi lambda untuk operasi sederhana
square = lambda x: x ** 2
print(square(5)) # 25
# Berguna dengan map, filter, sorted
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers)) # [1, 4, 9, 16, 25]
evens = list(filter(lambda x: x % 2 == 0, numbers)) # [2, 4]B.5 List Comprehensions
List comprehensions adalah cara ringkas membuat lists:
# Syntax: [expression for item in iterable if condition]
# Contoh sederhana
squares = [x**2 for x in range(10)]
# Dengan kondisi
even_squares = [x**2 for x in range(10) if x % 2 == 0]
# Nested comprehension
matrix = [[j for j in range(3)] for i in range(3)]
# [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
# Dict comprehension
word_lengths = {word: len(word) for word in ['apple', 'banana', 'cherry']}
# {'apple': 5, 'banana': 6, 'cherry': 6}B.6 NumPy Essentials
NumPy adalah library fundamental untuk komputasi numerik:
import numpy as np
# Membuat arrays
arr = np.array([1, 2, 3, 4, 5])
matrix = np.array([[1, 2, 3], [4, 5, 6]])
zeros = np.zeros((3, 3))
ones = np.ones((2, 4))
range_arr = np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
# Shape dan indexing
print(matrix.shape) # (2, 3)
print(matrix[0, 1]) # 2 (baris 0, kolom 1)
# Slicing
print(arr[1:4]) # [2 3 4]
print(matrix[:, 1]) # [2 5] (semua baris, kolom 1)
# Broadcasting (operasi otomatis pada dimensi berbeda)
a = np.array([1, 2, 3])
b = np.array([[1], [2], [3]])
result = a + b # shape akan otomatis menyesuaikan
# Operasi matematika
print(np.sum(arr)) # 15
print(np.mean(arr)) # 3.0
print(np.std(arr)) # deviasi standar
print(np.max(arr)) # 5
print(arr * 2) # [2 4 6 8 10]B.7 Pandas Essentials
Pandas untuk manipulasi dan analisis data tabelar:
import pandas as pd
# Membuat DataFrame
data = {
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 28],
'score': [85, 92, 78]
}
df = pd.DataFrame(data)
# Membaca/menyimpan data
df = pd.read_csv('data.csv')
df.to_csv('output.csv', index=False)
df.to_excel('output.xlsx')
# Inspeksi data
print(df.head()) # 5 baris pertama
print(df.info()) # tipe dan non-null count
print(df.describe()) # statistik deskriptif
# Seleksi kolom
print(df['name']) # Series
print(df[['name', 'age']]) # DataFrame
# Filtering
adults = df[df['age'] > 25]
high_scorers = df[df['score'] >= 85]
# Groupby operations
by_age = df.groupby('age')['score'].mean()
group_stats = df.groupby('age').agg({'score': ['mean', 'max'], 'age': 'count'})
# Menangani missing values
df.fillna(0) # ganti NaN dengan 0
df.dropna() # hapus baris dengan NaNB.8 File I/O
Bekerja dengan JSON
import json
# Membaca JSON
with open('data.json', 'r') as f:
data = json.load(f)
# Menulis JSON
data = {'name': 'Alice', 'age': 30}
with open('output.json', 'w') as f:
json.dump(data, f, indent=2)B.9 Exception Handling
Menangani error dengan graceful:
try:
x = int("abc") # ValueError
except ValueError:
print("Invalid input!")
except Exception as e:
print(f"Error: {e}")
finally:
print("Cleanup code here")
# Raise exception
if x < 0:
raise ValueError("x harus positif!")B.10 String Operations
text = "Hello, World!"
# Basic operations
print(len(text)) # panjang string
print(text.upper()) # HELLO, WORLD!
print(text.lower()) # hello, world!
print(text.replace("World", "Python")) # Hello, Python!
# String slicing
print(text[0:5]) # "Hello"
print(text[-6:]) # "World!"
# F-strings (Python 3.6+) - REKOMENDASI
name = "Alice"
age = 30
message = f"Nama: {name}, Umur: {age}"
# String methods
words = text.split(", ") # ["Hello", "World!"]
joined = "-".join(words) # "Hello-World!"
# Checking
print("Hello" in text) # True
print(text.startswith("Hello")) # TrueB.11 Date & Time Operations
from datetime import datetime, timedelta
# Current date/time
now = datetime.now()
print(now) # 2024-12-07 10:30:45.123456
# Create specific date
date = datetime(2024, 12, 7)
# Date arithmetic
tomorrow = now + timedelta(days=1)
next_week = now + timedelta(weeks=1)
# String formatting
print(now.strftime("%Y-%m-%d")) # 2024-12-07
print(now.strftime("%d/%m/%Y %H:%M")) # 07/12/2024 10:30
# Parsing string to datetime
date_str = "2024-12-07"
date = datetime.strptime(date_str, "%Y-%m-%d")B.12 Built-in Functions (Berguna untuk ML)
| Fungsi | Deskripsi | Contoh |
|---|---|---|
len() |
Panjang objek | len([1,2,3]) → 3 |
max()/min() |
Nilai terbesar/terkecil | max([1,5,3]) → 5 |
sum() |
Jumlah elemen | sum([1,2,3]) → 6 |
abs() |
Nilai absolut | abs(-5) → 5 |
round() |
Pembulatan | round(3.14159, 2) → 3.14 |
enumerate() |
Index + value | for i, v in enumerate(['a','b']) |
zip() |
Kombinasi iterables | zip([1,2], ['a','b']) → [(1,‘a’), (2,‘b’)] |
sorted() |
Sorting | sorted([3,1,2]) → [1,2,3] |
reversed() |
Reverse | list(reversed([1,2,3])) → [3,2,1] |
isinstance() |
Type checking | isinstance(5, int) → True |
B.13 Common Pitfalls & Best Practices
Pitfall 1: Mutable Default Arguments
# SALAH
def add_to_list(item, my_list=[]):
my_list.append(item)
return my_list
# BENAR
def add_to_list(item, my_list=None):
if my_list is None:
my_list = []
my_list.append(item)
return my_listPitfall 2: Off-by-One Errors
# List index dimulai dari 0
arr = [1, 2, 3, 4, 5]
print(arr[0]) # 1 (elemen pertama)
print(arr[-1]) # 5 (elemen terakhir)
print(arr[:3]) # [1, 2, 3] (tidak termasuk index 3)Pitfall 3: Shallow Copy vs Deep Copy
import copy
original = [[1, 2], [3, 4]]
shallow = original.copy() # Hanya copy level pertama
deep = copy.deepcopy(original) # Copy semua level
shallow[0][0] = 999
print(original) # [[999, 2], [3, 4]] - BERUBAH!
print(deep) # [[1, 2], [3, 4]] - tidak berubahB.14 ML-Relevant Python Patterns
Iterating dengan enumerate
# Useful untuk training loops
for epoch in enumerate(range(num_epochs)):
loss = train_step()
print(f"Epoch {epoch}: Loss = {loss}")Unpacking Values
# Useful untuk preprocessing data
X, y = features, labels
train_X, test_X = split_data(X)Using with Statement
# Automatic resource management
with open('model.pkl', 'rb') as f:
model = pickle.load(f)
# File otomatis tertutup setelah blokDictionary Get dengan Default
# Useful untuk hyperparameter handling
config = {'lr': 0.001}
batch_size = config.get('batch_size', 32) # default 32 jika tidak ada