Classes Create a BankAccount Class

Learning objective: By the end of this lesson, students will be able to create a class and instantiate objects from it.

🎓 You Do

Let’s get some practice building an object hierarchy in Python by creating a BankAccount class!

Create a BankAccount class with the following members:

Create two instances, make both deposits and withdrawals, and print the attributes to test them out.

Hint

Here’s how to generate a random integer for the in Python:

import random

# Use this inside of BankAccount's __init__ to generate
# a random account number from 111111111 to 999999999
self.account_no = random.randint(111111111, 999999999)

Bonus

Override the __str__ method to return the following formatted string:

Account <account_no> - Balance: xxxxx.xx

Replacing <account_no> with the actual account number and xxxxx.xx with the actual balance.