Lesson 32 of 34

Methods: Object செய்யும் செயல்கள்Methods: What Objects Can Do

10 min 35 XP

class-க்குள் எழுதும் functions-ஐ methods என்பார்கள். முதல் parameter எப்போதும் self. ஒரு method object-ன் தகவல்களைப் படிக்கவும் மாற்றவும் முடியும்.

Functions written inside a class are called methods. Their first parameter is always self. A method can read and change the object's data.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

__str__ method இருந்தால், print(object) அழகான உரையைக் காட்டும்.

With a __str__ method, print(object) shows friendly text.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

Challenge

+35 XP

BankAccount class எழுதுங்கள்: • __init__: balance = 0 உடன் தொடங்கும் • deposit(amount): balance-ல் சேர்க்கும் • withdraw(amount): balance போதுமானால் கழிக்கும், இல்லையென்றால் "Insufficient balance" என print செய்யும் • get_balance(): balance-ஐ return செய்யும்

Write a BankAccount class: • __init__: starts with balance = 0 • deposit(amount): adds to the balance • withdraw(amount): subtracts if there is enough, otherwise prints "Insufficient balance" • get_balance(): returns the balance

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.