Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fc6ffa5

Browse files
Object oriented Python Exercise
1 parent a1b511f commit fc6ffa5

File tree

22 files changed

+860
-0
lines changed

22 files changed

+860
-0
lines changed

‎exercise-code/E1-1.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class BankAccount:
5+
def set_details(self, name, balance=0):
6+
self.name = name
7+
self.balance = balance
8+
9+
def display(self):
10+
print(self.name, self.balance)
11+
12+
def withdraw(self, amount):
13+
self.balance -= amount
14+
15+
def deposit(self, amount):
16+
self.balance += amount
17+
18+
a1 = BankAccount()
19+
a1.set_details('Mike', 200)
20+
21+
a2 = BankAccount()
22+
a2.set_details('Tom')
23+
24+
a1.display()
25+
a2.display()
26+
27+
a1.withdraw(100)
28+
a2.deposit(500)
29+
30+
a1.display()
31+
a2.display()
32+
33+

‎exercise-code/E2-1.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class BankAccount:
5+
def __init__(self, name, balance=0):
6+
self.name = name
7+
self.balance = balance
8+
9+
def display(self):
10+
print(self.name, self.balance)
11+
12+
def withdraw(self, amount):
13+
self.balance -= amount
14+
15+
def deposit(self, amount):
16+
self.balance += amount
17+
18+
a1 = BankAccount('Mike', 200)
19+
a2 = BankAccount('Tom')
20+
21+
a1.display()
22+
a2.display()
23+
24+
a1.withdraw(100)
25+
a2.deposit(500)
26+
27+
a1.display()
28+
a2.display()

‎exercise-code/E2-10.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Circle:
5+
def __init__(self, radius):
6+
self.radius = radius
7+
8+
@property
9+
def radius(self):
10+
return self._radius
11+
12+
@radius.setter
13+
def radius(self, new_radius):
14+
if new_radius > 0:
15+
self._radius = new_radius
16+
else:
17+
raise ValueError('Radius should be positive')
18+
19+
@property
20+
def diameter(self):
21+
return self._radius * 2
22+
23+
@property
24+
def circumference(self):
25+
return 2 * 3.14 * self._radius
26+
27+
def area(self):
28+
return 3.14 * self._radius * self._radius
29+
30+
c1 = Circle(7)
31+
print( c1.radius, c1.diameter, c1.circumference, c1.area() )
32+

‎exercise-code/E2-2.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Book:
5+
def __init__(self,isbn, title,author,publisher,pages,price,copies):
6+
self.isbn = isbn
7+
self.title = title
8+
self.author = author
9+
self.publisher = publisher
10+
self.pages = pages
11+
self.price = price
12+
self.copies = copies
13+
14+
def display(self):
15+
print(self.title)
16+
print(f'ISBN : {self.isbn}')
17+
print(f'Price : {self.price}')
18+
print(f'Number of copies : {self.copies}')
19+
print('.' * 50)
20+
21+
22+
book1 = Book('957-4-36-547417-1', 'Learn Physics','Stephen', 'CBC', 350, 200,10)
23+
book2 = Book('652-6-86-748413-3', 'Learn Chemistry','Jack', 'CBC', 400, 220,20)
24+
book3 = Book('957-7-39-347216-2', 'Learn Maths','John', 'XYZ', 500, 300,5)
25+
book4 = Book('957-7-39-347216-2', 'Learn Biology','Jack', 'XYZ', 400, 200,6)
26+
27+
book1.display()
28+
book2.display()
29+
book3.display()
30+
book4.display()
31+

‎exercise-code/E2-3.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Book:
5+
6+
def __init__(self,isbn, title,author,publisher,pages,price,copies):
7+
self.isbn = isbn
8+
self.title = title
9+
self.author = author
10+
self.publisher = publisher
11+
self.pages = pages
12+
self.price = price
13+
self.copies = copies
14+
15+
def display(self):
16+
print(self.title)
17+
print(f'ISBN : {self.isbn}')
18+
print(f'Price : {self.price}')
19+
print(f'Number of copies : {self.copies}')
20+
print('.' * 50)
21+
22+
def in_stock(self):
23+
return True if self.copies>0 else False
24+
25+
def sell(self):
26+
if self.in_stock():
27+
self.copies -= 1
28+
else:
29+
print('The book is out of stock')
30+
31+
32+
book1 = Book('957-4-36-547417-1', 'Learn Physics','Stephen', 'CBC', 350, 200,10)
33+
book2 = Book('652-6-86-748413-3', 'Learn Chemistry','Jack', 'CBC', 400, 220,20)
34+
book3 = Book('957-7-39-347216-2', 'Learn Maths','John', 'XYZ', 500, 300,5)
35+
book4 = Book('957-7-39-347216-2', 'Learn Biology','Jack', 'XYZ', 400, 200,6)
36+
37+
book1.display()
38+
book2.display()
39+
book3.display()
40+
book4.display()
41+
42+
43+
book3.sell()
44+
book3.sell()
45+
book3.sell()
46+
book3.sell()
47+
book3.sell()
48+
book3.sell()

‎exercise-code/E2-4.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Book:
5+
6+
def __init__(self,isbn, title,author,publisher,pages,price,copies):
7+
self.isbn = isbn
8+
self.title = title
9+
self.author = author
10+
self.publisher = publisher
11+
self.pages = pages
12+
self.price = price
13+
self.copies = copies
14+
15+
def display(self):
16+
print(self.title)
17+
print(f'ISBN : {self.isbn}')
18+
print(f'Price : {self.price}')
19+
print(f'Number of copies : {self.copies}')
20+
print('.' * 50)
21+
22+
23+
book1 = Book('957-4-36-547417-1', 'Learn Physics','Stephen', 'CBC', 350, 200,10)
24+
book2 = Book('652-6-86-748413-3', 'Learn Chemistry','Jack', 'CBC', 400, 220,20)
25+
book3 = Book('957-7-39-347216-2', 'Learn Maths','John', 'XYZ', 500, 300,5)
26+
book4 = Book('957-7-39-347216-2', 'Learn Biology','Jack', 'XYZ', 400, 200,6)
27+
28+
book1.display()
29+
book2.display()
30+
book3.display()
31+
book4.display()
32+
33+
34+
books = [book1, book2, book3, book4]
35+
36+
for book in books:
37+
book.display()
38+
39+
jack_books = [book.title for book in books if book.author == 'Jack']
40+
41+
print(jack_books)
42+

‎exercise-code/E2-5.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Book:
5+
def __init__(self,isbn, title,author,publisher,pages,price,copies):
6+
self.isbn = isbn
7+
self.title = title
8+
self.author = author
9+
self.publisher = publisher
10+
self.pages = pages
11+
self.price = price
12+
self.copies = copies
13+
14+
def display(self):
15+
print(self.title)
16+
print(f'ISBN : {self.isbn}')
17+
print(f'Price : {self._price}')
18+
print(f'Number of copies : {self.copies}')
19+
print('.' * 50)
20+
21+
@property
22+
def price(self):
23+
return self._price
24+
25+
@price.setter
26+
def price(self, new_price):
27+
if 10 <= new_price <= 500:
28+
self._price = new_price
29+
else:
30+
raise ValueError('Price cannot be less than 10 or more than 500')
31+
32+
book1 = Book('957-4-36-547417-1', 'Learn Physics','Stephen', 'CBC', 350, 200,10)
33+
book2 = Book('652-6-86-748413-3', 'Learn Chemistry','Jack', 'CBC', 400, 220,20)
34+
book3 = Book('957-7-39-347216-2', 'Learn Maths','John', 'XYZ', 500, 300,5)
35+
book4 = Book('957-7-39-347216-2', 'Learn Biology','Jack', 'XYZ', 400, 20,6)
36+
37+
book1.display()
38+
book2.display()
39+
book3.display()
40+
book4.display()
41+
42+

‎exercise-code/E2-6.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Fraction:
5+
def __init__(self,nr,dr=1):
6+
self.nr = nr
7+
self.dr = dr
8+
if self.dr < 0:
9+
self.nr *= -1
10+
self.dr *= -1
11+
12+
def show(self):
13+
print(f'{self.nr}/{self.dr}')
14+
15+
f1 = Fraction(2,3)
16+
f1.show()
17+
f2 = Fraction(2,-3)
18+
f2.show()
19+
f3 = Fraction(-5,-6)
20+
f3.show()

‎exercise-code/E2-7.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Fraction:
5+
def __init__(self,nr,dr=1):
6+
self.nr = nr
7+
self.dr = dr
8+
if self.dr < 0:
9+
self.nr *= -1
10+
self.dr *= -1
11+
12+
def show(self):
13+
print(f'{self.nr}/{self.dr}')
14+
15+
def multiply(self,other):
16+
if isinstance(other,int):
17+
other = Fraction(other)
18+
return Fraction(self.nr * other.nr , self.dr * other.dr)
19+
20+
def add(self,other):
21+
if isinstance(other,int):
22+
other = Fraction(other)
23+
return Fraction(self.nr * other.dr + other.nr * self.dr, self.dr * other.dr)
24+
25+
26+
f1 = Fraction(2,3)
27+
f1.show()
28+
f2 = Fraction(2,-3)
29+
f2.show()
30+
f3 = Fraction(-5,-6)
31+
f3.show()
32+
33+

‎exercise-code/E2-8.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Product():
5+
def __init__(self, id, marked_price, discount):
6+
self.id = id
7+
self.marked_price = marked_price
8+
self.discount = discount
9+
10+
@property
11+
def selling_price(self):
12+
return self.marked_price - 0.01 * self.discount * self.marked_price
13+
14+
def display(self):
15+
print(self.id, self.marked_price, self.discount)
16+
17+
p1 = Product('A234', 100, 5)
18+
p2 = Product('X879', 400, 6)
19+
p3 = Product('B987', 990, 4)
20+
p4 = Product('H456', 800, 6)
21+
22+
print(p1.id, p1.selling_price)
23+
print(p2.id, p2.selling_price)
24+
print(p3.id, p3.selling_price)
25+
print(p4.id, p4.selling_price)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /