Skip to content

Commit 072d32f

Browse files
Add files via upload
1 parent a1984a2 commit 072d32f

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed

day5-oop's.py

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
"""OOP in python
2+
To map with real world scenarios, we started using objects in code.
3+
This is called object oriented programming
4+
Procedral - seq code
5+
function - redundancy < , reusablity >
6+
oop - we can also: redundancy < , reusablity >
7+
8+
class :1st we create class [ class is bluePrint for creathing objects ]
9+
object :"""
10+
11+
#creating class
12+
class Student:
13+
name = "Sakshi"
14+
15+
s1 = Student()
16+
print(s1) #op - <__main__.Student object at 0x0000014C58275850>
17+
print(s1.name) #Sakshi
18+
19+
s2 = Student()
20+
print(s2.name) #Sakshi
21+
22+
class Car:
23+
color = "pink"
24+
brand = "BMW"
25+
26+
c1 = Car()
27+
print(c1.color)
28+
print(c1.brand)
29+
30+
"""
31+
__init__ Function
32+
constructor is called in every new obj
33+
All class have a function called __init__(),which is always executed
34+
when the object is being initiated"""
35+
36+
class Student2:
37+
college_name ="jain" #when we know the value is comman , class attr
38+
name = "anonymous" #class attr , when we have same name then obj attr is printed
39+
#default constructor
40+
def __init__(self):
41+
pass
42+
#parameterzied constructor
43+
def __init__(self,name,marks): #we have to pass the self arrg
44+
print("Adding new student database")
45+
self.name = name
46+
self.marks = marks
47+
48+
49+
#s3 = Student2() #<__main__.Student object at 0x000001C7E1C8B7A0>
50+
#print(s3) #<__main__.Student object at 0x000001C7E1C8B7A0>
51+
s3 = Student2("sakshi",89) #data is called attibutes
52+
print(s3.name,s3.marks)
53+
54+
s4 = Student2("Nida",89)
55+
print(s4.name,s4.marks)
56+
57+
print(s4.college_name) #jain
58+
print(Student2.college_name) #jain
59+
60+
"""
61+
Methods
62+
class is colletion of data(attr) and methods
63+
Method are function that belongs to objects"""
64+
65+
#ex for method
66+
class Student3:
67+
college1_name ="jain"
68+
69+
#parameterzied constructor
70+
def __init__(self,name1,marks1): #we have to pass the self arrg self.name1 = name1
71+
self.name1 = name1
72+
self.marks1 = marks1
73+
74+
def wlcm(self):
75+
print("wlcm student",self.name1)
76+
77+
def get_marks(self):
78+
return self.marks1
79+
80+
81+
s7 = Student3("sak",99)
82+
print(s7.name1,s7.marks1)
83+
s7.wlcm() #calling the method
84+
print(Student3.get_marks1())
85+
86+
#crete student class that takes name and marks of 3 sub as aruguments
87+
#in constructor then create a mentod to print the average
88+
89+
90+
91+
class Std:
92+
def __init__(self,name9,marks7):
93+
self.name9 = name9
94+
self.marks7 = marks7
95+
96+
def avg(self):
97+
sum = 0
98+
for val in self.marks7:
99+
sum += val
100+
print("hi",self.name9,sum/3)
101+
102+
s5 =Std("sakshi",[99,98,97])
103+
s5.avg() #hi sakshi 98.0
104+
105+
s5.name2 ="Sak"
106+
s5.avg() #hi Sak 98.0
107+
108+
109+
"""
110+
Static Methods
111+
methods that dont use the self parameter(work at class level)
112+
@staticmethod -> convert a function to be static methods
113+
*when to use > This is comman for all class(obj)
114+
> whn there is no use of class attr or instances attr
115+
EX:
116+
@staticmethod
117+
def start():
118+
print("str")
119+
"""
120+
121+
class Hello:
122+
@staticmethod #decorator
123+
def hello():
124+
print("hello")
125+
126+
127+
s0 =Hello()
128+
s0.hello()

day6-Abstraction&Encapsulation.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
Abstraction
3+
Hiding the implementation details of class and only
4+
showing the essential feature to the user
5+
"""
6+
class Car:
7+
def __init__(self):
8+
self.acc =False
9+
self.brk =False
10+
self.clutch = False
11+
12+
def start(self):
13+
self.acc =True
14+
self.acc =True
15+
print("car started")
16+
17+
car1 = Car()
18+
car1.start()
19+
"""
20+
Encapsulation
21+
Wrapping data and function into single unit(obj)
22+
"""
23+
24+
#practice
25+
"""
26+
create Account class with 2 attributes - balance and account no
27+
create method for debit,credit and printing the balance
28+
"""
29+
30+
class Account:
31+
balance = 5000
32+
account_no = 123
33+
34+
def __init__(self):
35+
pass
36+
37+
@staticmethod
38+
def debit(): #sub
39+
acc = int(input("Enter the debit amt:"))
40+
Account.balance -= acc
41+
print(f"Debited {acc} from account. Updated balance: {Account.balance}")
42+
43+
44+
@staticmethod
45+
def credit(): #add
46+
acc= int(input("enter your credit amount:"))
47+
Account.balance += acc
48+
print(f"Credited {acc} to account. Updated balance: {Account.balance}") #{} in python work as an place holder for var
49+
50+
acc = Account()
51+
acc.debit()
52+
acc.credit()
53+
54+
#other EX2
55+
56+
class Acc:
57+
def __init__(self,bal,acc_no):
58+
self.bal = bal
59+
self.acc_no = acc_no
60+
61+
def D(self,amt):
62+
self.bal -= amt
63+
print("the D amt is :",amt)
64+
print("Total:",self.get_bal())
65+
66+
67+
def C(self,amt):
68+
self.bal+=amt
69+
print("the C amm is :",amt)
70+
print("Total:",self.get_bal())
71+
72+
73+
def get_bal(self):
74+
return self.bal
75+
76+
77+
acc1 = Acc(5000,1)
78+
acc1.D(1000)
79+
acc1.C(500)

0 commit comments

Comments
 (0)