Lớp tùy chỉnh

Nghiên cứu tình huống: Xây dựng phần mềm bằng Python

Mark Pedigo

Principal Data Scientist

Lộ trình

Lộ trình cho thấy phần dự án đã hoàn thành và phần sắp bắt đầu.

Nghiên cứu tình huống: Xây dựng phần mềm bằng Python

Tính khoản thanh toán hàng tháng

Công thức tính khoản thanh toán thế chấp hàng tháng là

$$\text{monthly payment} = P \cdot \frac{r (1 + r)^N}{(1+r)^N - 1},$$

trong đó

  • $P$ là số tiền vay,
  • $r$ là lãi suất hàng tháng, và
  • $N$ là số kỳ thanh toán hàng tháng
1 https://en.wikipedia.org/wiki/Mortgage_calculator
Nghiên cứu tình huống: Xây dựng phần mềm bằng Python

Bổ sung chức năng cho lớp

Phương thức tính khoản thanh toán hàng tháng

def monthly_payment(self):
    # Compute the multiplier
    numerator = self.monthly_interest_rate * \
        (1 + self.monthly_interest_rate) ** self.months
    denominator = (1 + self.monthly_interest_rate) ** self.months - 1
    multiplier = self.divide(numerator, denominator)
    # Calculate the monthly payment
    monthly_payment = self.multiply(self.loan_amount, multiplier)
    return monthly_payment
Nghiên cứu tình huống: Xây dựng phần mềm bằng Python

Tính khoản thanh toán hàng tháng

# Instantiate a mortgage calculator
mortgage_calculator = calculators.MortgageCalculator(300000, 0.065, 30)

# Calculate the monthly payment
print(round(mortgage_calculator.monthly_payment, 2))
1703.37
Nghiên cứu tình huống: Xây dựng phần mềm bằng Python

dir() lần nữa

  • dir(ClassName) - liệt kê các hàm của một lớp
dir(MortgageCalculator)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__',
'__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__',
'calculate_monthly_payment', 'difference', 'monthly_interest',
'monthly_interest_rate', 'monthly_payment', 'months', 'power', 'principal',
'product', 'quotient', 'sum']
Nghiên cứu tình huống: Xây dựng phần mềm bằng Python

Ví dụ f-string

name = "Alice"
age = 17
# Using an f-string to include variables in a sentence
greeting = f"Hello, my name is {name} and I'm {age} years old."
print(greeting)
Hello, my name is Alice and I'm 17 years old.
Nghiên cứu tình huống: Xây dựng phần mềm bằng Python

Ayo berlatih!

Nghiên cứu tình huống: Xây dựng phần mềm bằng Python

Preparing Video For Download...