Skip to content

Commit 7870903

Browse files
committed
Added Chapter 11 Practice Set and Project 2
Part 1 Finished, i will be taking a break for now.
1 parent 280adc3 commit 7870903

16 files changed

+280
-7
lines changed

Chapter 11/0105_Type_of_Inheritance.md

-5
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@ Advantages of Inheritance
1313
3. Readability: By implementing concepts of inheritance, the program looks more concise and structured. Which makes it easy to read. This way inheritance also improves the readability of code.
1414

1515
================================
16-
1716
Types of Inheritance in Python Programming
1817
Types of inheritance: There are five types of inheritance in python programming:
1918

2019
1). Single inheritance
21-
2220
2). Multiple inheritances
23-
2421
3). Multilevel inheritance
25-
2622
4). Hierarchical inheritance
27-
2823
5). Hybrid inheritance
2924

3025
(i). Single inheritance: When child class is derived from only one parent class.

Chapter 11/Practice Set/Q1.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#Practice set Q1
2+
#Author: Prakash
3+
4+
import os
5+
6+
class C2DVector:
7+
def __init__(self, i, j):
8+
self.icap = i
9+
self.jcap = j
10+
11+
def __str__(self):
12+
return (f"{self.icap}i + {self.jcap}j.")
13+
14+
15+
'''
16+
The __str__ method in Python represents the class objects as a string, it can be used for classes. The __str__ method
17+
should be defined in a way that is easy to read and outputs all the members of the class.
18+
'''
19+
20+
21+
class C3dVector(C2DVector):
22+
def __init__(self, i, j, k):
23+
super().__init__(i, j)
24+
self.kcap = k
25+
26+
def __str__(self):
27+
return (f"{self.icap}i + {self.jcap}j + {self.kcap}k.")
28+
29+
V2 = C2DVector(4, 8)
30+
31+
V3 = C3dVector(3, 4, 5)
32+
33+
print(V2)
34+
print(V3)

Chapter 11/Practice Set/Q2.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#Practice set Q2
2+
#Author: Prakash
3+
#This is a Multilevel Inheritance
4+
5+
import os
6+
7+
class Animals:
8+
AnimalType = "Mammal"
9+
10+
class Pets(Animals):
11+
Color = "White"
12+
13+
class Dogs(Pets):
14+
15+
@staticmethod
16+
def bark():
17+
print("woof!")
18+
19+
D1 = Dogs()
20+
D1.bark()
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#Practice set Q2
2+
#Author: Prakash
3+
#This is a Multilevel Inheritance
4+
5+
import os
6+
7+
class Animals:
8+
AnimalType = "Mammal"
9+
10+
class Pets(Animals):
11+
Color = "White"
12+
13+
class Dogs(Pets):
14+
15+
def bark(self, barksound):
16+
self.bark = barksound
17+
18+
def __str__(self):
19+
return (self.bark)
20+
21+
D1 = Dogs()
22+
D1.bark("WOOF!")
23+
print(D1)
24+

Chapter 11/Practice Set/Q3.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#Practice set Q3
2+
#Author: Prakash
3+
4+
import os
5+
6+
class Employee:
7+
salary = 1200
8+
increment = 1.5
9+
10+
@property
11+
def salaryAfterIncrement(self):
12+
return self.salary*self.increment
13+
14+
@salaryAfterIncrement.setter
15+
def salaryAfterIncrement(self, val):
16+
self.increment = val/self.salary
17+
18+
e = Employee()
19+
print(e.salaryAfterIncrement)
20+
21+
e.salaryAfterIncrement = 2000
22+
23+
print(e.salary)
24+
print(e.salaryAfterIncrement)
25+
print(round(e.increment, 3))

Chapter 11/Practice Set/Q4.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#Practice set Q4
2+
#Author: Prakash
3+
4+
import os
5+
6+
class Complex:
7+
def __init__(self, r, i):
8+
self.real = r
9+
self.image = i
10+
11+
def __add__(self, c):
12+
print("Let's Add")
13+
return Complex(self.real + c.real, self.image + c.image)
14+
15+
def __str__(self):
16+
if self.image<0:
17+
return f"{self.real} - {-self.image}i"
18+
else:
19+
return f"{self.real} + {self.image}i"
20+
21+
def __mul__(self, c):
22+
print("Let's Multiply")
23+
mulReal = self.real * c.real - self.image * c.image
24+
mulImage = self.real * c.image + self.image * c.real
25+
return Complex(mulReal, mulImage)
26+
27+
28+
n1 = Complex(3, 2)
29+
n2 = Complex(1, 7)
30+
31+
print(n1 + n2)
32+
print(n1 * n2)

Chapter 11/Practice Set/Q5.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#Practice set Q5
2+
#Author: Prakash
3+
4+
import os
5+
6+
class Vector:
7+
def __init__(self, vec):
8+
self.vec = vec
9+
10+
def __str__(self):
11+
str1 = ""
12+
index = 0
13+
for i in self.vec:
14+
str1 += f" {i}a{index} +"
15+
index +=1
16+
return str1[:-1]
17+
18+
def __add__(self, vec2):
19+
newList = []
20+
for i in range(len(self.vec)):
21+
newList.append(self.vec[i] + vec2.vec[i])
22+
return Vector(newList)
23+
24+
def __mul__(self, vec2):
25+
sum = 0
26+
for i in range(len(self.vec)):
27+
sum += self.vec[i] * vec2.vec[i]
28+
return sum
29+
30+
v1 = Vector([1, 4, 6])
31+
v2 = Vector([1, 6, 9])
32+
print(v1+v2)
33+
print(v1*v2)

Chapter 11/Practice Set/Q6.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#Practice set Q6
2+
#Author: Prakash
3+
4+
import os
5+
class Vector:
6+
def __init__(self, vec):
7+
self.vec = vec
8+
9+
def __str__(self):
10+
return f"{self.vec[0]}i + {self.vec[1]}j + {self.vec[2]}k"
11+
12+
v1 = Vector([1, 4, 6])
13+
v2 = Vector([1, 6, 9])
14+
print(v1)
15+
print(v2)

Chapter 11/Practice Set/Q7.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#Practice set Q7
2+
#Author: Prakash
3+
4+
import os
5+
6+
class Vector:
7+
def __init__(self, vec):
8+
self.vec = vec
9+
10+
def __str__(self):
11+
str1 = ""
12+
index = 0
13+
for i in self.vec:
14+
str1 += f" {i}a{index} +"
15+
index +=1
16+
return str1[:-1]
17+
18+
def __add__(self, vec2):
19+
newList = []
20+
for i in range(len(self.vec)):
21+
newList.append(self.vec[i] + vec2.vec[i])
22+
return Vector(newList)
23+
24+
def __mul__(self, vec2):
25+
sum = 0
26+
for i in range(len(self.vec)):
27+
sum += self.vec[i] * vec2.vec[i]
28+
return sum
29+
30+
def __len__(self):
31+
return len(self.vec)
32+
33+
v1 = Vector([1, 4, 6, 6])
34+
v2 = Vector([1, 6, 9])
35+
print(len(v1))
36+
print(len(v2))
33.5 MB
Binary file not shown.

Project 2/High_Score

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6

Project 2/Main_MyAttempt.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#Project 2
2+
#author Prakash
3+
4+
import os
5+
import random
6+
7+
randomNumber = random.randint(0, 100)
8+
userguess = None
9+
guesscount = 0
10+
11+
while(userguess != randomNumber):
12+
userguess = int(input("Enter Your Guess: "))
13+
guesscount = guesscount + 1
14+
15+
if(userguess>randomNumber):
16+
print("Your Guess is Too High, Pick a Lower Number.")
17+
18+
elif(userguess<randomNumber):
19+
print("Your Guess is Too Low, Pick a Higher Number.")
20+
21+
elif(userguess==randomNumber):
22+
print("Woah! You guessed it correctly!")
23+
24+
print(f"You took {guesscount} attempts to guess the no. correctly.")
25+
26+
with open("Project 2//High_Score", "r") as f:
27+
High_Score = int(f.read())
28+
29+
if(High_Score>guesscount):
30+
print("Wow! Congratulations, You have made a new HiGH SCORE.")
31+
32+
with open("Project 2//High_Score", "w") as f:
33+
f.write(str(guesscount))

Project 2/Project 2.pdf

310 KB
Binary file not shown.

Project 2/hiscore.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9

Project 2/main.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import random
2+
randNumber = random.randint(1, 100)
3+
userGuess = None
4+
guesses = 0
5+
6+
while(userGuess != randNumber):
7+
userGuess = int(input("Enter your guess: "))
8+
guesses += 1
9+
if(userGuess==randNumber):
10+
print("You guessed it right!")
11+
else:
12+
if(userGuess>randNumber):
13+
print("You guessed it wrong! Enter a smaller number")
14+
else:
15+
print("You guessed it wrong! Enter a larger number")
16+
17+
print(f"You guessed the number in {guesses} guesses")
18+
with open("Project 2//hiscore.txt", "r") as f:
19+
hiscore = int(f.read())
20+
21+
if(guesses<hiscore):
22+
print("You have just broken the high score!")
23+
with open("Project 2//hiscore.txt", "w") as f:
24+
f.write(str(guesses))

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ Chapter 10 - Practice Set✅
5252

5353
Chapter 11 - Inheritance✅
5454

55-
Chapter 11 - Practice Set🔲
55+
Chapter 11 - Practice Set
5656

57-
Project 2 - The Perfect Guess🔲
57+
Project 2 - The Perfect Guess
5858

5959
---------------------------END OF PART 1------------------------------
6060

0 commit comments

Comments
 (0)