Skip to content

Commit d071063

Browse files
committed
updated LABs notes
1 parent 6208871 commit d071063

1 file changed

Lines changed: 292 additions & 0 deletions

File tree

LAB/31-OOPS_Concept.ipynb

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 43,
6+
"id": "2611322e-4924-4211-a78f-560943e7c507",
7+
"metadata": {
8+
"jupyter": {
9+
"source_hidden": true
10+
}
11+
},
12+
"outputs": [
13+
{
14+
"data": {
15+
"text/html": [
16+
"<!-- CSS settings for this notebook -->\n",
17+
"<style>\n",
18+
" h1 {color:#EE0000} /* Red Hat red */\n",
19+
" hr { \n",
20+
" border: 0;\n",
21+
" height: 3px;\n",
22+
" background: #333;\n",
23+
" background-image: linear-gradient(to right, #ccc, #EE0000, #ccc);\n",
24+
" }\n",
25+
"</style>\n"
26+
],
27+
"text/plain": [
28+
"<IPython.core.display.HTML object>"
29+
]
30+
},
31+
"metadata": {},
32+
"output_type": "display_data"
33+
}
34+
],
35+
"source": [
36+
"%%html\n",
37+
"<!-- CSS settings for this notebook -->\n",
38+
"<style>\n",
39+
" h1 {color:#EE0000} /* Red Hat red */\n",
40+
" hr { \n",
41+
" border: 0;\n",
42+
" height: 3px;\n",
43+
" background: #333;\n",
44+
" background-image: linear-gradient(to right, #ccc, #EE0000, #ccc);\n",
45+
" }\n",
46+
"</style>"
47+
]
48+
},
49+
{
50+
"cell_type": "markdown",
51+
"id": "2f81a5b1-4f7a-4e56-b4e7-17a646e4d792",
52+
"metadata": {},
53+
"source": [
54+
"# What is OOPs?"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"id": "abbcbf99-0e83-49f7-8828-d0c1c5cb5725",
60+
"metadata": {},
61+
"source": [
62+
"- OOPs (Object-Oriented Programming System) is a programming paradigm where the main focus is on objects rather than just functions and logic.\n",
63+
"- Think of objects as real-world things:\n",
64+
" - A Car is an object → it has properties (color, speed, model) and behaviors (drive, brake).\n",
65+
" - A Student is an object → it has properties (name, roll number, age) and behaviors (study, write exam).\n",
66+
"- OOPs allows us to model such real-world entities in our programs, making them easy to understand, reuse, and maintain."
67+
]
68+
},
69+
{
70+
"cell_type": "markdown",
71+
"id": "3dee0f68-c087-48d2-9be2-0c0b227d9551",
72+
"metadata": {},
73+
"source": [
74+
"### Core Concepts of OOPS"
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"id": "213289be-3889-4198-8e64-ad587c766d74",
80+
"metadata": {},
81+
"source": [
82+
"1. Class\n",
83+
"- A class is like a blueprint or template. It does not occupy memory directly. It just defines what properties and behaviors objects created from it will have.\n",
84+
"- Example:\n",
85+
" - A Car class defines what every car should have (engine, color, brand) and what it can do (drive, stop)."
86+
]
87+
},
88+
{
89+
"cell_type": "markdown",
90+
"id": "0ffefad1-5532-486d-bd42-f7c9dda7e782",
91+
"metadata": {},
92+
"source": [
93+
"2. Object\n",
94+
"- An object is an instance of a class. When you create an object, you bring the class to life.\n",
95+
"- Example:\n",
96+
" - Car myCar = new Car(\"Red\", \"Tesla\");\n",
97+
" - Here, myCar is an object of the Car class."
98+
]
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"id": "6cb571b0-1b5a-4c7c-ae1e-9621d1bbf501",
103+
"metadata": {},
104+
"source": [
105+
"3. Encapsulation\n",
106+
"- Encapsulation means wrapping data and methods together inside a class and controlling access to them. This ensures data security and avoids accidental modifications.\n",
107+
"- Example:\n",
108+
" - A bank account object hides its balance. You cannot change it directly; you can only deposit or withdraw using the available the methods."
109+
]
110+
},
111+
{
112+
"cell_type": "markdown",
113+
"id": "6d8a73f1-410e-43fc-aa25-7f5b15f5a58a",
114+
"metadata": {},
115+
"source": [
116+
"4. Abstraction\n",
117+
"- Abstraction means showing only the essential features and hiding unnecessary details.\n",
118+
"- Example:\n",
119+
" - When you drive a car, you just use the steering wheel, brakes, and accelerator. You don’t need to know how the engine works internally."
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"id": "9f783c2f-57d1-4297-b9ef-9fcca5cf207f",
125+
"metadata": {},
126+
"source": [
127+
"5. Inheritance\n",
128+
"- Inheritance allows a class to acquire properties and behaviors of another class. This promotes code reuse.\n",
129+
"- Example:\n",
130+
" - A Dog class can inherit from an Animal class.\n",
131+
" - Animal may have properties like name, age and behaviors like eat(), sleep().\n",
132+
" - Dog automatically gets these features and can add more like bark()."
133+
]
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"id": "bc5932ac-ee85-489f-b1e0-94cca3d32247",
138+
"metadata": {},
139+
"source": [
140+
"6. Polymorphism\n",
141+
"- Polymorphism means one thing, many forms. In OOPS, it allows the same function to behave differently depending on the object.\n",
142+
"- Example:\n",
143+
" - A method makeSound() could make a Dog bark and a Cat meow.\n",
144+
" - Same function name, different behaviors."
145+
]
146+
},
147+
{
148+
"cell_type": "markdown",
149+
"id": "b1dd5804-19e1-40d2-817c-5ff058cc006d",
150+
"metadata": {},
151+
"source": [
152+
"## Why OOPS is Important?\n",
153+
"- Reusability → Code can be reused through inheritance.\n",
154+
"- Scalability → Easy to extend and modify without breaking everything.\n",
155+
"- Security → Encapsulation protects data.\n",
156+
"- Real-world modeling → Closer to how humans think about problems.\n",
157+
"- Maintenance → Easier to debug and update."
158+
]
159+
},
160+
{
161+
"cell_type": "markdown",
162+
"id": "82cbc22a-f807-4ac3-8f83-28356ebf3cc9",
163+
"metadata": {},
164+
"source": [
165+
"# Classes and Objects in Python"
166+
]
167+
},
168+
{
169+
"cell_type": "markdown",
170+
"id": "3906d83f-ba12-4bef-b32a-503470ddb770",
171+
"metadata": {},
172+
"source": [
173+
"## Class\n",
174+
"- In Python, a Class is like a blueprint or a template for creating Objects. Since in Python, everything is considered an object.\n",
175+
"- If a \"Car\" is a class, then your specific car, a \"Red Tesla,\" is an object (an instance) of that class.\n",
176+
"- It does not occupy memory directly. It just defines what properties and behaviors objects created from it will have.\n",
177+
"## Object\n",
178+
"- An object is an instance of a class. When you create an object, you bring the class to life.\n",
179+
"- It is a specific instance created from the class blueprint/template.\n",
180+
"- Each object has its own unique set of data, and data can be physical data or logical data"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 132,
186+
"id": "24320f40-f22b-4ac9-90de-6e2dc783cc4a",
187+
"metadata": {},
188+
"outputs": [
189+
{
190+
"name": "stdout",
191+
"output_type": "stream",
192+
"text": [
193+
"Calculating Salary\n",
194+
"name: amit - role: devops\n",
195+
"salary: 25500\n",
196+
"*********************\n",
197+
"name: sam - role: HR\n",
198+
"salary: 54000\n",
199+
"\n",
200+
"\n",
201+
"---Class/Object/Methods infomation---\n",
202+
"---------------------------------------------------------\n",
203+
"emp is a: <class '__main__.emp'>\n",
204+
"method resolution order \"mro\" is a: <built-in method mro of type object at 0x00000185AC9E8CF0> and In Python 3, every class you create automatically inherits from object, which provides built-in attributes.\n",
205+
"[<class '__main__.emp'>, <class 'object'>]\n",
206+
"emp class has two functions: <function emp.data at 0x00000185AE282980> and <function emp.sal at 0x00000185AE283420>\n",
207+
"---------------------------------------------------------\n",
208+
"ops is a class attributes and can be referenced via class instsance as emp.ops and value is: Calculating Salary\n",
209+
"ops is a class attribute, and can be referenced via object instance as e1.ops and value is: Calculating Salary\n",
210+
"---------------------------------------------------------\n",
211+
"e1 is object and stored at memory location: <__main__.emp object at 0x00000185AE2F2270>\n",
212+
"e2 is object and stored at memory location: <__main__.emp object at 0x00000185AE180050>\n",
213+
"---------------------------------------------------------\n"
214+
]
215+
}
216+
],
217+
"source": [
218+
"class emp:\n",
219+
" ops = \"Calculating Salary\"\n",
220+
" print(ops)\n",
221+
" \n",
222+
" def data(self,n,r):\n",
223+
" self.name=n\n",
224+
" self.name=r\n",
225+
" print(\"name:\",n,\"- role:\",r)\n",
226+
"\n",
227+
" def sal(self,t,a,p):\n",
228+
" self.ctc=t\n",
229+
" self.allow=a\n",
230+
" self.pf=p\n",
231+
" print(\"salary:\",t+a-p)\n",
232+
"\n",
233+
"e1=emp()\n",
234+
"e1.data(\"amit\",\"devops\")\n",
235+
"e1.sal(23000,7500,5000)\n",
236+
"print(\"*********************\")\n",
237+
"e2 = emp()\n",
238+
"e2.data('sam','HR')\n",
239+
"e2.sal(45000,12500,3500)\n",
240+
"\n",
241+
"print('\\n')\n",
242+
"print(\"---Class/Object/Methods infomation---\")\n",
243+
"print('---------------------------------------------------------')\n",
244+
"print(f'emp is a: {emp}')\n",
245+
"print(f'method resolution order \"mro\" is a: {emp.mro} and In Python 3, every class you create automatically inherits from object, which provides built-in attributes.')\n",
246+
"print(emp.mro()) # to print the MRO order\n",
247+
"print(f'emp class has two functions: {emp.data} and {emp.sal}')\n",
248+
"print('---------------------------------------------------------')\n",
249+
"print(f'ops is a class attributes and can be referenced via class instsance as emp.ops and value is: {emp.ops}')\n",
250+
"print(f'ops is a class attribute, and can be referenced via object instance as e1.ops and value is: {e1.ops}')\n",
251+
"print('---------------------------------------------------------')\n",
252+
"print(f'e1 is object and stored at memory location: {e1}')\n",
253+
"print(f'e2 is object and stored at memory location: {e2}')\n",
254+
"print('---------------------------------------------------------')"
255+
]
256+
},
257+
{
258+
"cell_type": "markdown",
259+
"id": "d36e459c-0c5b-4ce1-b2bd-1e7338c50d75",
260+
"metadata": {},
261+
"source": [
262+
"- class is a keyword that is used to create a class; emp is a class in the above example.\n",
263+
"- Objects are instances created from a class, where all methods defined under the class can be referenced. E1 is an object in the above example\n",
264+
"- `__int__` is a special method that we do not call explicitly, and it is used to call the variable\n",
265+
"- self is a variable which reference to itself in the current class; it is always the first parameter in a method definition\n",
266+
"- Class Attributes: are shared among all objects of the class, must be defined outside of a method, directly in a class\n",
267+
"- Instance Attributes: are specific to each object and defined within the __init__ method"
268+
]
269+
}
270+
],
271+
"metadata": {
272+
"kernelspec": {
273+
"display_name": "Python 3 (ipykernel)",
274+
"language": "python",
275+
"name": "python3"
276+
},
277+
"language_info": {
278+
"codemirror_mode": {
279+
"name": "ipython",
280+
"version": 3
281+
},
282+
"file_extension": ".py",
283+
"mimetype": "text/x-python",
284+
"name": "python",
285+
"nbconvert_exporter": "python",
286+
"pygments_lexer": "ipython3",
287+
"version": "3.13.9"
288+
}
289+
},
290+
"nbformat": 4,
291+
"nbformat_minor": 5
292+
}

0 commit comments

Comments
 (0)