Skip to content

Commit e0caa5a

Browse files
committed
25/12 changes, daily practice
1 parent 3a6ede2 commit e0caa5a

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

python_concepts/dictionary.ipynb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,39 @@
6161
"\n",
6262
"print(dict2.get('address', 'unknown'))"
6363
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 5,
68+
"metadata": {},
69+
"outputs": [
70+
{
71+
"name": "stdout",
72+
"output_type": "stream",
73+
"text": [
74+
"30\n"
75+
]
76+
},
77+
{
78+
"data": {
79+
"text/plain": [
80+
"['a', 'b', 'c', 'd', 'e']"
81+
]
82+
},
83+
"execution_count": 5,
84+
"metadata": {},
85+
"output_type": "execute_result"
86+
}
87+
],
88+
"source": [
89+
"# We can get the values using .values( method)\n",
90+
"dict1 = {'a':2, 'b':4 , 'c':6, 'd':8 , 'e':10}\n",
91+
"#get total values of the dictionary\n",
92+
"total_value = sum(dict1.values())\n",
93+
"print(total_value)\n",
94+
"# convert dictionary keys into a list using keys() method\n",
95+
"list(dict1.keys())"
96+
]
6497
}
6598
],
6699
"metadata": {
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"['e', 'n', 't', 'r', 'p', 'u', 'i', 'a', 'l']\n"
13+
]
14+
}
15+
],
16+
"source": [
17+
"# Day 1 question from twitter https://twitter.com/CodingComputing/status/1738549517077614877\n",
18+
"# It deals with concept of set i.e. it cannot contain duplicate\n",
19+
"# It does not maintain sort order\n",
20+
"# list.sort has key parameter which can be used to sort the list using specific way\n",
21+
"def puzzling_fun(my_list):\n",
22+
" my_set = set(my_list) # this will get the unique elements from my_list\n",
23+
" my_new_list = list(my_set) # convert it to list again\n",
24+
" my_new_list.sort(key=my_list.index) # because set does not maintain order \n",
25+
" # # we will sort the list again with index of initial list\n",
26+
" return my_new_list\n",
27+
"\n",
28+
"my_list = list('entrepreneurial')\n",
29+
"print(puzzling_fun(my_list))\n",
30+
"# ['e', 'n', 't', 'r', 'p', 'u', 'i', 'a', 'l']\n",
31+
"\n"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 3,
37+
"metadata": {},
38+
"outputs": [
39+
{
40+
"name": "stdout",
41+
"output_type": "stream",
42+
"text": [
43+
"FDX\n",
44+
"-22.42%\n"
45+
]
46+
}
47+
],
48+
"source": [
49+
"# Day 1 question from twitter https://twitter.com/bbelderbos/status/1736790788544057552\n",
50+
"# Tuple unpacking and how to discard elements from the list using throway *_ variable\n",
51+
"\n",
52+
"fields = ['FDX', 'FedEx Corporation', '158.94', '-45.93', '-22.42%']\n",
53+
"symbol, name, price, *_ = fields # except first 3 elements discard rest of the elements of the list\n",
54+
"print(symbol)\n",
55+
"symbol, *_, value1 = fields # except first and last elements of the list discard all other values\n",
56+
"print(value1) \n"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 8,
62+
"metadata": {},
63+
"outputs": [
64+
{
65+
"name": "stdout",
66+
"output_type": "stream",
67+
"text": [
68+
"0.03952956199645996\n",
69+
"0.0016777515411376953\n"
70+
]
71+
}
72+
],
73+
"source": [
74+
"# Day 1 efficient method of joining elements of the list using .join https://twitter.com/bbelderbos/status/1736726322880151590\n",
75+
"\n",
76+
"import time\n",
77+
"\n",
78+
"words = [\"word\"] * 100_000\n",
79+
"\n",
80+
"# inefficient string concatenation\n",
81+
"start_time = time.time()\n",
82+
"sentence = \"\"\n",
83+
"for w in words:\n",
84+
" sentence += w\n",
85+
"print(time.time() - start_time) # 0.9659469127655029\n",
86+
"\n",
87+
"# more efficient string concat using .join() on list\n",
88+
"start_time = time.time()\n",
89+
"sentence = \" \".join(words)\n",
90+
"print(time.time() - start_time) # 0.001013040542602539 \n",
91+
"#print(sentence)"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": 9,
97+
"metadata": {},
98+
"outputs": [
99+
{
100+
"name": "stdout",
101+
"output_type": "stream",
102+
"text": [
103+
"[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]\n",
104+
"1 2\n",
105+
"3 4\n",
106+
"5 6\n",
107+
"7 8\n",
108+
"9 10\n"
109+
]
110+
}
111+
],
112+
"source": [
113+
"numbers = range(1, 11)\n",
114+
"pairs = list(zip(numbers[::2], numbers[1::2]))\n",
115+
"print(pairs) # [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]\n",
116+
"\n",
117+
"for i , j in zip(numbers[::2], numbers[1::2]):\n",
118+
" print(i, j)"
119+
]
120+
}
121+
],
122+
"metadata": {
123+
"kernelspec": {
124+
"display_name": "dashboard",
125+
"language": "python",
126+
"name": "python3"
127+
},
128+
"language_info": {
129+
"codemirror_mode": {
130+
"name": "ipython",
131+
"version": 3
132+
},
133+
"file_extension": ".py",
134+
"mimetype": "text/x-python",
135+
"name": "python",
136+
"nbconvert_exporter": "python",
137+
"pygments_lexer": "ipython3",
138+
"version": "3.10.6"
139+
}
140+
},
141+
"nbformat": 4,
142+
"nbformat_minor": 2
143+
}

0 commit comments

Comments
 (0)