Skip to content

Commit 905d3bc

Browse files
authored
Add files via upload
1 parent 5afbac1 commit 905d3bc

1 file changed

Lines changed: 182 additions & 0 deletions

File tree

Tutorial 22-encapsulation.ipynb

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
{
2+
"cells": [
3+
{
4+
"attachments": {},
5+
"cell_type": "markdown",
6+
"metadata": {},
7+
"source": [
8+
"## Encapsulation In Python\n",
9+
"\n",
10+
"Encapsulation is a fundamental principle in object-oriented programming that focuses on bundling data and the methods that operate on that data into a single unit called a class. It allows you to control the access and visibility of the data and methods, providing a way to protect and organize your code"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 1,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"### Access Modifier--->Encapsulation\n",
20+
"## private\n",
21+
"\n",
22+
"class Person:\n",
23+
" ## constructor\n",
24+
" def __init__(self,name,age):\n",
25+
" self.__name=name\n",
26+
" self.__age=age\n",
27+
"\n",
28+
" def display_info(self):\n",
29+
" print(f\"the person name is {self.__name} and the age is {self.__age}\")\n",
30+
"\n",
31+
" "
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 8,
37+
"metadata": {},
38+
"outputs": [
39+
{
40+
"name": "stdout",
41+
"output_type": "stream",
42+
"text": [
43+
"the person name is Krish and the age is 32\n"
44+
]
45+
}
46+
],
47+
"source": [
48+
"person=Person(\"Krish\",32)\n",
49+
"\n",
50+
"person.display_info()"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 13,
56+
"metadata": {},
57+
"outputs": [],
58+
"source": [
59+
"### Access Modifier--->Encapsulation\n",
60+
"## Protected \n",
61+
"\n",
62+
"class Person:\n",
63+
" ## constructor\n",
64+
" def __init__(self,name,age):\n",
65+
" self._name=name\n",
66+
" self._age=age\n",
67+
"\n",
68+
" "
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 14,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"person=Person(\"Krish\",32)"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 15,
83+
"metadata": {},
84+
"outputs": [
85+
{
86+
"data": {
87+
"text/plain": [
88+
"32"
89+
]
90+
},
91+
"execution_count": 15,
92+
"metadata": {},
93+
"output_type": "execute_result"
94+
}
95+
],
96+
"source": [
97+
"dir(person)\n",
98+
"person._age"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 16,
104+
"metadata": {},
105+
"outputs": [],
106+
"source": [
107+
"class Student(Person):\n",
108+
" def __init__(self,name,age):\n",
109+
" super().__init__(name,age)\n",
110+
"\n",
111+
" def display_info(self):\n",
112+
" print(f\"the person name is {self._name} and the age is {self._age}\")\n",
113+
"\n",
114+
" \n",
115+
"\n",
116+
" "
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": 17,
122+
"metadata": {},
123+
"outputs": [],
124+
"source": [
125+
"student1=Student(\"Krish\",33)"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 18,
131+
"metadata": {},
132+
"outputs": [
133+
{
134+
"name": "stdout",
135+
"output_type": "stream",
136+
"text": [
137+
"the person name is Krish and the age is 33\n"
138+
]
139+
}
140+
],
141+
"source": [
142+
"student1.display_info()"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": null,
148+
"metadata": {},
149+
"outputs": [],
150+
"source": [
151+
"## public access modifiers\n",
152+
"class Person:\n",
153+
" ## constructor\n",
154+
" def __init__(self,name,age):\n",
155+
" self.name=name\n",
156+
" self.age=age\n"
157+
]
158+
}
159+
],
160+
"metadata": {
161+
"kernelspec": {
162+
"display_name": "Python 3",
163+
"language": "python",
164+
"name": "python3"
165+
},
166+
"language_info": {
167+
"codemirror_mode": {
168+
"name": "ipython",
169+
"version": 3
170+
},
171+
"file_extension": ".py",
172+
"mimetype": "text/x-python",
173+
"name": "python",
174+
"nbconvert_exporter": "python",
175+
"pygments_lexer": "ipython3",
176+
"version": "3.9.16"
177+
},
178+
"orig_nbformat": 4
179+
},
180+
"nbformat": 4,
181+
"nbformat_minor": 2
182+
}

0 commit comments

Comments
 (0)