-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoops_practice_1.py
More file actions
104 lines (65 loc) · 1.67 KB
/
oops_practice_1.py
File metadata and controls
104 lines (65 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#%%
class Car:
pass
audi = Car()
bmw = Car()
print(type(audi))
print(type(bmw))
#%%
print(bmw)
#%% Instance varibles and methods
class Dog:
audi.windows = 4
print(audi.windows)
#%%
tata = Car()
tata.doors = 4
x = dir(tata)
#%% Constructors
class Dog:
## constructor
def __init__(self, name, age):
self.name = name
self.age = age
## create objects
dog1 = Dog("Buddy",3)
print(dog1)
print(dog1.name)
print(dog1.age)
#%% Instance Methods
# Define a class with instance method
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print(f"{self.name} says woof")
dog1 = Dog("Buddy", 3)
dog1.bark()
dog2 = Dog("Lucy", 5)
dog2.bark()
#%% Modelling a bank account
#define a clas for bank account
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance+= amount
print(f"{amount} is deposited. New balance is {self.balance}")
def get_balance(self):
return self.balance
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient Funds")
else:
self.balance-= amount
print(f"{amount} is withdrawn. New balance is {self.balance}")
#create a bank account
account = BankAccount("Krish",5000)
print(account.balance)
#Call instance methods
account.deposit(100)
account.withdraw(300)
#%%
print(account.get_balance())