Skip to content

Commit a577bc3

Browse files
authored
Add files via upload
1 parent d4d8807 commit a577bc3

File tree

1 file changed

+348
-0
lines changed

1 file changed

+348
-0
lines changed
Lines changed: 348 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,348 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": []
7+
},
8+
"kernelspec": {
9+
"name": "python3",
10+
"display_name": "Python 3"
11+
},
12+
"language_info": {
13+
"name": "python"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "markdown",
19+
"source": [
20+
"# **Exercise: Python Dict and Tuples**"
21+
],
22+
"metadata": {
23+
"id": "5bFvfO87fhws"
24+
}
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"source": [
29+
"1.We have following information on countries and their population (population is in crores),\n",
30+
"\n",
31+
"Country\t Population\n",
32+
"\n",
33+
"China\t 143\n",
34+
"\n",
35+
"India\t 136\n",
36+
"\n",
37+
"USA\t 32\n",
38+
"\n",
39+
"Pakistan\t 21\n",
40+
"\n",
41+
"\n",
42+
"\n"
43+
],
44+
"metadata": {
45+
"id": "qvqt6FmsfmnA"
46+
}
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"source": [
51+
"a.Using above create a dictionary of countries and its population"
52+
],
53+
"metadata": {
54+
"id": "lDcuFvBigHwb"
55+
}
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"colab": {
62+
"base_uri": "https://localhost:8080/"
63+
},
64+
"id": "eXXBaycKfZEe",
65+
"outputId": "d091de15-7300-4836-ae27-c80a58e19dde"
66+
},
67+
"outputs": [
68+
{
69+
"output_type": "stream",
70+
"name": "stdout",
71+
"text": [
72+
"{'china': 143, 'india': 136, 'USA': 32, 'Pakistan': 21}\n"
73+
]
74+
}
75+
],
76+
"source": [
77+
"d = {\"china\":143,\"india\":136,\"USA\":32,\"Pakistan\":21}\n",
78+
"print(d)"
79+
]
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"source": [
84+
"b.Write a program that asks user for three type of inputs,\n",
85+
"a.print: if user enter print then it should print all countries with their population in this format,\n",
86+
"\n",
87+
"china==>143\n",
88+
"\n",
89+
"india==>136\n",
90+
"\n",
91+
"usa==>32\n",
92+
"\n",
93+
"pakistan==>21\n",
94+
"\n",
95+
"b.add: if user input add then it should further ask for a country name to add. If country already exist in our dataset then it should print that it exist and do nothing. If it doesn't then it asks for population and add that new country/population in our dictionary and print it\n",
96+
"\n",
97+
"c.remove: when user inputs remove it should ask for a country to remove. If country exist in our dictionary then remove it and print new dictionary using format shown above in (a). Else print that country doesn't exist!\n",
98+
"\n",
99+
"d.query: on this again ask user for which country he or she wants to query. When user inputs that country it will print population of that country."
100+
],
101+
"metadata": {
102+
"id": "jESUFmKzg2ok"
103+
}
104+
},
105+
{
106+
"cell_type": "code",
107+
"source": [
108+
"population = {\n",
109+
" 'china': 143,\n",
110+
" 'india': 136,\n",
111+
" 'usa': 32,\n",
112+
" 'pakistan': 21\n",
113+
"}\n",
114+
"\n",
115+
"def add():\n",
116+
" country=input(\"Enter country name to add:\")\n",
117+
" country=country.lower()\n",
118+
" if country in population:\n",
119+
" print(\"Country already exist in our dataset. Terminating\")\n",
120+
" return\n",
121+
" p=input(f\"Enter population for {country}\")\n",
122+
" p=float(p)\n",
123+
" population[country]=p # Adds new key value pair to dictionary\n",
124+
" print_all()\n",
125+
"\n",
126+
"def remove():\n",
127+
" country = input(\"Enter country name to remove:\")\n",
128+
" country = country.lower()\n",
129+
" if country not in population:\n",
130+
" print(\"Country doesn't exist in our dataset. Terminating\")\n",
131+
" return\n",
132+
" del population[country]\n",
133+
" print_all()\n",
134+
"\n",
135+
"def query():\n",
136+
" country = input(\"Enter country name to query:\")\n",
137+
" country = country.lower()\n",
138+
" if country not in population:\n",
139+
" print(\"Country doesn't exist in our dataset. Terminating\")\n",
140+
" return\n",
141+
" print(f\"Population of {country} is: {population[country]} crore\")\n",
142+
"\n",
143+
"def print_all():\n",
144+
" for country, p in population.items():\n",
145+
" print(f\"{country}==>{p}\")\n",
146+
"\n",
147+
"def main():\n",
148+
" op=input(\"Enter operation (add, remove, query or print):\")\n",
149+
" if op.lower() == 'add':\n",
150+
" add()\n",
151+
" elif op.lower() == 'remove':\n",
152+
" remove()\n",
153+
" elif op.lower() == 'query':\n",
154+
" query()\n",
155+
" elif op.lower() == 'print':\n",
156+
" print_all()\n",
157+
"\n",
158+
"if __name__ == '__main__':\n",
159+
" main()"
160+
],
161+
"metadata": {
162+
"colab": {
163+
"base_uri": "https://localhost:8080/"
164+
},
165+
"id": "z_LzisbNjsBC",
166+
"outputId": "817b1bf8-6c28-4e51-9049-51bb6ca63d78"
167+
},
168+
"execution_count": 1,
169+
"outputs": [
170+
{
171+
"output_type": "stream",
172+
"name": "stdout",
173+
"text": [
174+
"Enter operation (add, remove, query or print):add\n",
175+
"Enter country name to add:england\n",
176+
"Enter population for england345\n",
177+
"china==>143\n",
178+
"india==>136\n",
179+
"usa==>32\n",
180+
"pakistan==>21\n",
181+
"england==>345.0\n"
182+
]
183+
}
184+
]
185+
},
186+
{
187+
"cell_type": "markdown",
188+
"source": [
189+
"**How it works:**\n",
190+
"Uses a dictionary to store country => population.\n",
191+
"\n",
192+
"Based on user input:\n",
193+
"\n",
194+
"print → shows all countries and their populations.\n",
195+
"\n",
196+
"add → adds a new country if not already present.\n",
197+
"\n",
198+
"remove → removes a country if it exists.\n",
199+
"\n",
200+
"query → returns the population of the requested country.\n",
201+
"\n",
202+
"Handles case-insensitive inputs using .lower().\n",
203+
"\n"
204+
],
205+
"metadata": {
206+
"id": "C1Oygtg20YBS"
207+
}
208+
},
209+
{
210+
"cell_type": "markdown",
211+
"source": [
212+
"You are given following list of stocks and their prices in last 3 days,\n",
213+
"\n",
214+
"Stock\tPrices\n",
215+
"\n",
216+
"info\t[600,630,620]\n",
217+
"\n",
218+
"ril\t[1430,1490,1567]\n",
219+
"\n",
220+
"mtl\t[234,180,160]\n",
221+
"\n",
222+
"Write a program that asks user for operation. Value of operations could be,\n",
223+
"\n",
224+
"**print:** When user enters print it should print following,\n",
225+
"\n",
226+
"info ==> [600, 630, 620] ==> avg: 616.67\n",
227+
"\n",
228+
"ril ==> [1430, 1490, 1567] ==> avg: 1495.67\n",
229+
"\n",
230+
"mtl ==> [234, 180, 160] ==> avg: 191.33\n",
231+
"\n",
232+
"**add:** When user enters 'add', it asks for stock ticker and price. If stock already exist in your list (like info, ril etc) then it will append the price to the list. Otherwise it will create new entry in your dictionary. For example entering 'tata' and 560 will add tata ==> [560] to the dictionary of stocks."
233+
],
234+
"metadata": {
235+
"id": "G9o_uvSM2usr"
236+
}
237+
},
238+
{
239+
"cell_type": "code",
240+
"source": [
241+
"import statistics\n",
242+
"\n",
243+
"stocks = {\n",
244+
" 'info': [600,630,620],\n",
245+
" 'ril': [1430,1490,1567],\n",
246+
" 'mtl': [234,180,160]\n",
247+
"}\n",
248+
"\n",
249+
"def print_all():\n",
250+
" for stock,price_list in stocks.items():\n",
251+
" avg = statistics.mean(price_list)\n",
252+
" print(f\"{stock} ==> {price_list} ==> avg: \",round(avg,2))\n",
253+
"\n",
254+
"\n",
255+
"def add():\n",
256+
" s = input(\"Enter a stock ticker to add:\")\n",
257+
" p = input(\"Enter price of this stock:\")\n",
258+
" p=float(p)\n",
259+
" if s in stocks:\n",
260+
" stocks[s].append(p)\n",
261+
" else:\n",
262+
" stocks[s] = [p]\n",
263+
" print_all()\n",
264+
"\n",
265+
"\n",
266+
"def main():\n",
267+
" op=input(\"Enter operation (print, add or amend):\")\n",
268+
" if op.lower() == 'print':\n",
269+
" print_all()\n",
270+
" elif op.lower() == 'add':\n",
271+
" add()\n",
272+
" else:\n",
273+
" print(\"Unsupported operation:\",op)\n",
274+
"\n",
275+
"if __name__ == '__main__':\n",
276+
" main()"
277+
],
278+
"metadata": {
279+
"colab": {
280+
"base_uri": "https://localhost:8080/"
281+
},
282+
"id": "-TIEsapq0c0o",
283+
"outputId": "3d3767ad-c061-4f6b-c17d-0f95d6dd4968"
284+
},
285+
"execution_count": 3,
286+
"outputs": [
287+
{
288+
"output_type": "stream",
289+
"name": "stdout",
290+
"text": [
291+
"Enter operation (print, add or amend):add\n",
292+
"Enter a stock ticker to add:gfd\n",
293+
"Enter price of this stock:678\n",
294+
"info ==> [600, 630, 620] ==> avg: 616.67\n",
295+
"ril ==> [1430, 1490, 1567] ==> avg: 1495.67\n",
296+
"mtl ==> [234, 180, 160] ==> avg: 191.33\n",
297+
"gfd ==> [678.0] ==> avg: 678.0\n"
298+
]
299+
}
300+
]
301+
},
302+
{
303+
"cell_type": "markdown",
304+
"source": [
305+
"3.Write circle_calc() function that takes radius of a circle as an input from user and then it calculates and returns area, circumference and diameter. You should get these values in your main program by calling circle_calc function and then print them"
306+
],
307+
"metadata": {
308+
"id": "MDpDKhAD4QUt"
309+
}
310+
},
311+
{
312+
"cell_type": "code",
313+
"source": [
314+
"import math\n",
315+
"\n",
316+
"def circle_calc(radius):\n",
317+
" area=math.pi*(radius**2)\n",
318+
" circumference=2*math.pi*radius\n",
319+
" diameter=2*radius\n",
320+
" return area, circumference,diameter\n",
321+
"\n",
322+
"if __name__==\"__main__\":\n",
323+
" r=input(\"Enter a radius:\")\n",
324+
" r=float(r)\n",
325+
" area, c, d = circle_calc(r)\n",
326+
" print(f\"area {area}, circumference {c}, diameter {d}\")"
327+
],
328+
"metadata": {
329+
"colab": {
330+
"base_uri": "https://localhost:8080/"
331+
},
332+
"id": "mpjFQCbB4Xp0",
333+
"outputId": "2d426575-ef59-43c4-cfe0-3cbdf91c4e43"
334+
},
335+
"execution_count": 4,
336+
"outputs": [
337+
{
338+
"output_type": "stream",
339+
"name": "stdout",
340+
"text": [
341+
"Enter a radius:5\n",
342+
"area 78.53981633974483, circumference 31.41592653589793, diameter 10.0\n"
343+
]
344+
}
345+
]
346+
}
347+
]
348+
}

0 commit comments

Comments
 (0)