Skip to content

Commit 6fa6e6b

Browse files
authored
PART-01-Basics/CH-09
Added chapter 09 files.
1 parent 0b5c0b8 commit 6fa6e6b

27 files changed

+3789
-0
lines changed
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Try It Yourself"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"<pre>\n",
15+
"<strong>9-1. Restaurant:</strong> Make a class called Restaurant. The __init__() method for\n",
16+
"Restaurant should store two attributes: a restaurant_name and a cuisine_type.\n",
17+
"Make a method called describe_restaurant() that prints these two pieces of\n",
18+
"information, and a method called open_restaurant() that prints a message \n",
19+
"indicating that the restaurant is open. Make an instance called restaurant \n",
20+
"from your class. Print the two attributes individually, and then call both methods.\n",
21+
"</pre>\n"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 15,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"# restaurant.py\n",
31+
"\n",
32+
"class Restaurant:\n",
33+
" \"\"\"Model of a Restaurant\"\"\"\n",
34+
" def __init__(self, restaurant_name, cuisine_type):\n",
35+
" \"\"\"Initializing attributes for restaurants.\"\"\"\n",
36+
" self.restaurant_name = restaurant_name\n",
37+
" self.cuisine_type = cuisine_type\n",
38+
" \n",
39+
" def describe_restaurant(self):\n",
40+
" \"\"\"Describing restaurant name and cuisine type.\"\"\"\n",
41+
" print(self.restaurant_name)\n",
42+
" print(self.cuisine_type)\n",
43+
" \n",
44+
" def open_restaurant(self):\n",
45+
" \"\"\"Show the status of restaurant -i.e.- open/close.\"\"\"\n",
46+
" print(f\"The restaurant {self.restaurant_name} is open.\")"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 16,
52+
"metadata": {},
53+
"outputs": [
54+
{
55+
"name": "stdout",
56+
"output_type": "stream",
57+
"text": [
58+
"Kwality\n",
59+
"italian\n"
60+
]
61+
}
62+
],
63+
"source": [
64+
"restaurant = Restaurant('Kwality', 'italian')\n",
65+
"\n",
66+
"print(restaurant.restaurant_name)\n",
67+
"print(restaurant.cuisine_type)"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 22,
73+
"metadata": {},
74+
"outputs": [
75+
{
76+
"name": "stdout",
77+
"output_type": "stream",
78+
"text": [
79+
"Kwality\n",
80+
"italian\n",
81+
"The restaurant Kwality is open.\n"
82+
]
83+
}
84+
],
85+
"source": [
86+
"restaurant.describe_restaurant()\n",
87+
"restaurant.open_restaurant()"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 18,
93+
"metadata": {},
94+
"outputs": [
95+
{
96+
"name": "stdout",
97+
"output_type": "stream",
98+
"text": [
99+
"Kwality\n",
100+
"italian\n"
101+
]
102+
}
103+
],
104+
"source": [
105+
"Restaurant.describe_restaurant(restaurant) # Alternate way of calling a class method for an object"
106+
]
107+
},
108+
{
109+
"cell_type": "markdown",
110+
"metadata": {},
111+
"source": [
112+
"<pre>\n",
113+
"<strong>9-2. Three Restaurants:</strong> Start with your class from Exercise 9-1. Create three\n",
114+
"different instances from the class, and call describe_restaurant() for each\n",
115+
"instance.\n",
116+
"</pre>\n"
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": 23,
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"name": "stdout",
126+
"output_type": "stream",
127+
"text": [
128+
"kebabsville\n",
129+
"('indian', 'barbecue')\n",
130+
"None\n",
131+
"the square\n",
132+
"['indian', 'asian']\n",
133+
"None\n",
134+
"shreemaya celebrity\n",
135+
"['italian', 'chinese']\n",
136+
"None\n"
137+
]
138+
}
139+
],
140+
"source": [
141+
"r1 = Restaurant('kebabsville', ['indian', 'barbecue'])\n",
142+
"r2 = Restaurant('the square', ['indian', 'asian'])\n",
143+
"r3 = Restaurant('shreemaya celebrity', ['italian', 'chinese'])\n",
144+
"\n",
145+
"print(r1.describe_restaurant())\n",
146+
"print(r2.describe_restaurant()) \n",
147+
"print(r3.describe_restaurant()) # why None gets printed at last???"
148+
]
149+
},
150+
{
151+
"cell_type": "markdown",
152+
"metadata": {},
153+
"source": [
154+
"<pre>\n",
155+
"<strong>9-3. Users:</strong> Make a class called User. Create two attributes called first_name\n",
156+
"and last_name, and then create several other attributes that are typically stored\n",
157+
"in a user profile. Make a method called describe_user() that prints a summary\n",
158+
"of the user’s information. Make another method called greet_user() that prints\n",
159+
"a personalized greeting to the user.\n",
160+
"Create several instances representing different users, and call both methods\n",
161+
"for each user.\n",
162+
"</pre>"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 36,
168+
"metadata": {},
169+
"outputs": [],
170+
"source": [
171+
"# users.py\n",
172+
"\n",
173+
"class User:\n",
174+
" \"\"\"Model of a user.\"\"\"\n",
175+
" def __init__(self, first_name, last_name):\n",
176+
" \"\"\"Initializing attributes of user.\"\"\"\n",
177+
" self.first_name = first_name\n",
178+
" self.last_name = last_name\n",
179+
" self.location = 'india'\n",
180+
" self.gender = 'male'\n",
181+
" self.age = 18\n",
182+
" \n",
183+
" \n",
184+
" def describe_user(self):\n",
185+
" \"\"\"Print a summary of user's introduction.\"\"\"\n",
186+
" print(f\"User: {self.first_name.title()} {self.last_name.title()}\\n\\tLocation: {self.location.title()}\\\n",
187+
" \\n\\tGender: {self.gender.title()}\\n\\tAge: {self.age} years\")\n",
188+
" \n",
189+
" def greet_user(self):\n",
190+
" \"\"\"Greet the user.\"\"\"\n",
191+
" print(f\"\\nHi {self.first_name.title()} {self.last_name.title()}. Welcome with us.\")"
192+
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": 37,
197+
"metadata": {},
198+
"outputs": [
199+
{
200+
"name": "stdout",
201+
"output_type": "stream",
202+
"text": [
203+
"\n",
204+
"Hi Ishan Karn. Welcome with us.\n",
205+
"User: Ishan Karn\n",
206+
"\tLocation: India \n",
207+
"\tGender: Male\n",
208+
"\tAge: 18 years\n"
209+
]
210+
}
211+
],
212+
"source": [
213+
"user_1 = User('ishan', 'karn')\n",
214+
"\n",
215+
"user_1.greet_user()\n",
216+
"user_1.describe_user()"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": 39,
222+
"metadata": {},
223+
"outputs": [
224+
{
225+
"name": "stdout",
226+
"output_type": "stream",
227+
"text": [
228+
"\n",
229+
"Hi John Criset. Welcome with us.\n",
230+
"User: John Criset\n",
231+
"\tLocation: America \n",
232+
"\tGender: Male\n",
233+
"\tAge: 18 years\n"
234+
]
235+
}
236+
],
237+
"source": [
238+
"user_2 = User('john', 'criset')\n",
239+
"\n",
240+
"user_2.location = 'america'\n",
241+
"user_2.greet_user()\n",
242+
"user_2.describe_user()"
243+
]
244+
},
245+
{
246+
"cell_type": "markdown",
247+
"metadata": {},
248+
"source": [
249+
"<hr>"
250+
]
251+
}
252+
],
253+
"metadata": {
254+
"kernelspec": {
255+
"display_name": "Python 3",
256+
"language": "python",
257+
"name": "python3"
258+
},
259+
"language_info": {
260+
"codemirror_mode": {
261+
"name": "ipython",
262+
"version": 3
263+
},
264+
"file_extension": ".py",
265+
"mimetype": "text/x-python",
266+
"name": "python",
267+
"nbconvert_exporter": "python",
268+
"pygments_lexer": "ipython3",
269+
"version": "3.8.3"
270+
}
271+
},
272+
"nbformat": 4,
273+
"nbformat_minor": 4
274+
}

0 commit comments

Comments
 (0)