Skip to content

Commit 60d8cde

Browse files
authored
Add files via upload
OOPs
1 parent a7b74a9 commit 60d8cde

14 files changed

+3043
-0
lines changed

OOPs/Classes/Fraction.ipynb

Lines changed: 368 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,368 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 11,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"class Fraction():\n",
10+
"\n",
11+
" def __init__(self, n, d):\n",
12+
"\n",
13+
" self.num = n\n",
14+
" self.den = d"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 12,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"x = Fraction(4, 5)"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 13,
29+
"metadata": {},
30+
"outputs": [
31+
{
32+
"data": {
33+
"text/plain": [
34+
"__main__.Fraction"
35+
]
36+
},
37+
"execution_count": 13,
38+
"metadata": {},
39+
"output_type": "execute_result"
40+
}
41+
],
42+
"source": [
43+
"type(x)"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 18,
49+
"metadata": {},
50+
"outputs": [],
51+
"source": [
52+
"class Fraction():\n",
53+
"\n",
54+
" def __init__(self, n, d):\n",
55+
"\n",
56+
" self.num = n\n",
57+
" self.den = d\n",
58+
"\n",
59+
" def __str__(self):\n",
60+
" \n",
61+
" return \"{}/{}\".format(self.num, self.den)"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 20,
67+
"metadata": {},
68+
"outputs": [],
69+
"source": [
70+
"x = Fraction(1, 2)"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": 21,
76+
"metadata": {},
77+
"outputs": [
78+
{
79+
"name": "stdout",
80+
"output_type": "stream",
81+
"text": [
82+
"1/2\n"
83+
]
84+
}
85+
],
86+
"source": [
87+
"print(x)"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"metadata": {},
94+
"outputs": [],
95+
"source": [
96+
"# Jaise hi obj ko print function ke andar daal rhe ho, \n",
97+
"# automatically __str__ run hogya or Hello print hogya "
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 22,
103+
"metadata": {},
104+
"outputs": [],
105+
"source": [
106+
"y = Fraction(4, 5)"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 23,
112+
"metadata": {},
113+
"outputs": [
114+
{
115+
"data": {
116+
"text/plain": [
117+
"<__main__.Fraction at 0x26379270680>"
118+
]
119+
},
120+
"execution_count": 23,
121+
"metadata": {},
122+
"output_type": "execute_result"
123+
}
124+
],
125+
"source": [
126+
"y"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": 24,
132+
"metadata": {},
133+
"outputs": [
134+
{
135+
"name": "stdout",
136+
"output_type": "stream",
137+
"text": [
138+
"4/5\n"
139+
]
140+
}
141+
],
142+
"source": [
143+
"print(y)"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": 25,
149+
"metadata": {},
150+
"outputs": [
151+
{
152+
"ename": "TypeError",
153+
"evalue": "unsupported operand type(s) for +: 'Fraction' and 'Fraction'",
154+
"output_type": "error",
155+
"traceback": [
156+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
157+
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
158+
"Cell \u001b[1;32mIn[25], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m)\n",
159+
"\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'Fraction' and 'Fraction'"
160+
]
161+
}
162+
],
163+
"source": [
164+
"print(x + y)"
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": 27,
170+
"metadata": {},
171+
"outputs": [],
172+
"source": [
173+
"# What is we need to add the fractions\n",
174+
"\n",
175+
"__add__ #automatically triggers it when two objects are operated \n",
176+
"# with + inside print function\n",
177+
"\n"
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": 54,
183+
"metadata": {},
184+
"outputs": [],
185+
"source": [
186+
"class Fraction():\n",
187+
"\n",
188+
" def __init__(self, n, d):\n",
189+
"\n",
190+
" self.num = n\n",
191+
" self.den = d\n",
192+
"\n",
193+
" def __str__(self):\n",
194+
" \n",
195+
" return \"{}/{}\".format(self.num, self.den)\n",
196+
"\n",
197+
" def __add__(self, other):\n",
198+
"\n",
199+
" temp_num = self.num*other.den + other.num*self.den \n",
200+
" temp_den = self.den*other.den \n",
201+
"\n",
202+
" return \"{}/{}\".format(temp_num, temp_den)\n",
203+
"\n",
204+
" def __sub__(self, other):\n",
205+
"\n",
206+
" temp_num = self.num*other.den - other.num*self.den\n",
207+
" temp_den = self.den*other.den\n",
208+
"\n",
209+
" return \"{}/{}\".format(temp_num, temp_den)\n",
210+
"\n",
211+
" def __mul__(self, other):\n",
212+
"\n",
213+
" temp_num = self.num*other.num\n",
214+
" temp_den = self.den*other.den\n",
215+
"\n",
216+
" return \"{}/{}\".format(temp_num, temp_den)\n",
217+
" \n",
218+
" def __truediv__(self, other):\n",
219+
"\n",
220+
" temp_num = self.num*other.den\n",
221+
" temp_den = self.den*other.num\n",
222+
"\n",
223+
" return \"{}/{}\".format(temp_num, temp_den)\n"
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": 55,
229+
"metadata": {},
230+
"outputs": [],
231+
"source": [
232+
"x = Fraction(3, 4)"
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"execution_count": 56,
238+
"metadata": {},
239+
"outputs": [],
240+
"source": [
241+
"y = Fraction(5, 6)\n"
242+
]
243+
},
244+
{
245+
"cell_type": "code",
246+
"execution_count": 45,
247+
"metadata": {},
248+
"outputs": [
249+
{
250+
"name": "stdout",
251+
"output_type": "stream",
252+
"text": [
253+
"3/4\n"
254+
]
255+
}
256+
],
257+
"source": [
258+
"print(x)"
259+
]
260+
},
261+
{
262+
"cell_type": "code",
263+
"execution_count": 46,
264+
"metadata": {},
265+
"outputs": [
266+
{
267+
"name": "stdout",
268+
"output_type": "stream",
269+
"text": [
270+
"5/6\n"
271+
]
272+
}
273+
],
274+
"source": [
275+
"print(y)"
276+
]
277+
},
278+
{
279+
"cell_type": "code",
280+
"execution_count": 47,
281+
"metadata": {},
282+
"outputs": [
283+
{
284+
"name": "stdout",
285+
"output_type": "stream",
286+
"text": [
287+
"38/24\n"
288+
]
289+
}
290+
],
291+
"source": [
292+
"print(x+y)"
293+
]
294+
},
295+
{
296+
"cell_type": "code",
297+
"execution_count": 52,
298+
"metadata": {},
299+
"outputs": [
300+
{
301+
"name": "stdout",
302+
"output_type": "stream",
303+
"text": [
304+
"-2/24\n"
305+
]
306+
}
307+
],
308+
"source": [
309+
"print(x - y)"
310+
]
311+
},
312+
{
313+
"cell_type": "code",
314+
"execution_count": 57,
315+
"metadata": {},
316+
"outputs": [
317+
{
318+
"name": "stdout",
319+
"output_type": "stream",
320+
"text": [
321+
"15/24\n"
322+
]
323+
}
324+
],
325+
"source": [
326+
"print(x*y)"
327+
]
328+
},
329+
{
330+
"cell_type": "code",
331+
"execution_count": 58,
332+
"metadata": {},
333+
"outputs": [
334+
{
335+
"name": "stdout",
336+
"output_type": "stream",
337+
"text": [
338+
"18/20\n"
339+
]
340+
}
341+
],
342+
"source": [
343+
"print(x/y)"
344+
]
345+
}
346+
],
347+
"metadata": {
348+
"kernelspec": {
349+
"display_name": "Python 3",
350+
"language": "python",
351+
"name": "python3"
352+
},
353+
"language_info": {
354+
"codemirror_mode": {
355+
"name": "ipython",
356+
"version": 3
357+
},
358+
"file_extension": ".py",
359+
"mimetype": "text/x-python",
360+
"name": "python",
361+
"nbconvert_exporter": "python",
362+
"pygments_lexer": "ipython3",
363+
"version": "3.12.5"
364+
}
365+
},
366+
"nbformat": 4,
367+
"nbformat_minor": 2
368+
}

0 commit comments

Comments
 (0)