Skip to content

Commit 2e6398d

Browse files
committed
Tricky Python Questions
1 parent 6b7e5f9 commit 2e6398d

File tree

1 file changed

+294
-0
lines changed

1 file changed

+294
-0
lines changed

tricky questions.ipynb

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 5,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"text/plain": [
11+
"False"
12+
]
13+
},
14+
"execution_count": 5,
15+
"metadata": {},
16+
"output_type": "execute_result"
17+
}
18+
],
19+
"source": [
20+
"d = {\"john\": 40, \"peter\":45}\n",
21+
"\"John\" in d"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 11,
27+
"metadata": {},
28+
"outputs": [
29+
{
30+
"name": "stdout",
31+
"output_type": "stream",
32+
"text": [
33+
"5.5\n"
34+
]
35+
}
36+
],
37+
"source": [
38+
"x = 1/2.0 + 3 //3 + 4 ** 1\n",
39+
"print(x)"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 37,
45+
"metadata": {},
46+
"outputs": [
47+
{
48+
"ename": "NameError",
49+
"evalue": "name 'a' is not defined",
50+
"output_type": "error",
51+
"traceback": [
52+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
53+
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
54+
"\u001b[1;32m<ipython-input-37-5206caf6d3cc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;34m\"A\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;34m\"B\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;34m\"C\"\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
55+
"\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
56+
]
57+
}
58+
],
59+
"source": [
60+
"a = {1: \"A\", 2: \"B\", 3: \"C\"}\n",
61+
"del a\n",
62+
"print(a)"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 36,
68+
"metadata": {},
69+
"outputs": [
70+
{
71+
"name": "stdout",
72+
"output_type": "stream",
73+
"text": [
74+
"{1: 5, 2: 3, 5: 6}\n",
75+
"{1: 5, 2: 3}\n"
76+
]
77+
}
78+
],
79+
"source": [
80+
"a = {1:5, 2:3, 3:4, 5:6}\n",
81+
"a.pop(3)\n",
82+
"print(a)\n",
83+
"a.popitem()\n",
84+
"print(a)"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 19,
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"name": "stdout",
94+
"output_type": "stream",
95+
"text": [
96+
"4\n"
97+
]
98+
}
99+
],
100+
"source": [
101+
"a = {}\n",
102+
"a[1]=1\n",
103+
"a['1']=2\n",
104+
"a[1] = a[1]+1\n",
105+
"count= 0\n",
106+
"for i in a:\n",
107+
" count+=a[i]\n",
108+
"print(count)"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": 20,
114+
"metadata": {},
115+
"outputs": [
116+
{
117+
"name": "stdout",
118+
"output_type": "stream",
119+
"text": [
120+
"[2]\n"
121+
]
122+
}
123+
],
124+
"source": [
125+
"lst = [1,2,3,4]\n",
126+
"print(lst[-3:-2])"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": 25,
132+
"metadata": {},
133+
"outputs": [
134+
{
135+
"name": "stdout",
136+
"output_type": "stream",
137+
"text": [
138+
"[(3, 1), (4, 2), (4, 1), (5, 2)]\n"
139+
]
140+
}
141+
],
142+
"source": [
143+
"s1=[3,4]\n",
144+
"s2=[1,2]\n",
145+
"s3=list()\n",
146+
"i=0\n",
147+
"j=0\n",
148+
"for i in s1:\n",
149+
" for j in s2:\n",
150+
" s3.append((i,j))\n",
151+
" i+=1\n",
152+
" j+=1\n",
153+
"print(s3)"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 26,
159+
"metadata": {},
160+
"outputs": [
161+
{
162+
"name": "stdout",
163+
"output_type": "stream",
164+
"text": [
165+
"{'Numbers': {1: 56, 3: 7}, 'Letters': {4: 'B'}}\n"
166+
]
167+
}
168+
],
169+
"source": [
170+
"numbers = {}\n",
171+
"letters = {}\n",
172+
"comb = {}\n",
173+
"numbers[1] = 56\n",
174+
"numbers[3] = 7\n",
175+
"letters[4]='B'\n",
176+
"comb['Numbers'] = numbers\n",
177+
"comb['Letters'] = letters\n",
178+
"print(comb)"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": 27,
184+
"metadata": {},
185+
"outputs": [
186+
{
187+
"data": {
188+
"text/plain": [
189+
"[1, 3, 2, 1, 3, 2]"
190+
]
191+
},
192+
"execution_count": 27,
193+
"metadata": {},
194+
"output_type": "execute_result"
195+
}
196+
],
197+
"source": [
198+
"list1 = [1,3,2]\n",
199+
"list1*2"
200+
]
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": 28,
205+
"metadata": {},
206+
"outputs": [
207+
{
208+
"data": {
209+
"text/plain": [
210+
"0"
211+
]
212+
},
213+
"execution_count": 28,
214+
"metadata": {},
215+
"output_type": "execute_result"
216+
}
217+
],
218+
"source": [
219+
"A = 16\n",
220+
"B = 15\n",
221+
"A%B//A"
222+
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": 31,
227+
"metadata": {},
228+
"outputs": [
229+
{
230+
"name": "stdout",
231+
"output_type": "stream",
232+
"text": [
233+
"5\n"
234+
]
235+
}
236+
],
237+
"source": [
238+
"numbers = [1,2,3,4]\n",
239+
"numbers.append([5,6,7,8])\n",
240+
"print(len(numbers))"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": 32,
246+
"metadata": {},
247+
"outputs": [
248+
{
249+
"data": {
250+
"text/plain": [
251+
"False"
252+
]
253+
},
254+
"execution_count": 32,
255+
"metadata": {},
256+
"output_type": "execute_result"
257+
}
258+
],
259+
"source": [
260+
"list1 = [11,2,23]\n",
261+
"list2 = [11,2,2]\n",
262+
"list1<list2"
263+
]
264+
},
265+
{
266+
"cell_type": "code",
267+
"execution_count": null,
268+
"metadata": {},
269+
"outputs": [],
270+
"source": []
271+
}
272+
],
273+
"metadata": {
274+
"kernelspec": {
275+
"display_name": "Python 3",
276+
"language": "python",
277+
"name": "python3"
278+
},
279+
"language_info": {
280+
"codemirror_mode": {
281+
"name": "ipython",
282+
"version": 3
283+
},
284+
"file_extension": ".py",
285+
"mimetype": "text/x-python",
286+
"name": "python",
287+
"nbconvert_exporter": "python",
288+
"pygments_lexer": "ipython3",
289+
"version": "3.7.3"
290+
}
291+
},
292+
"nbformat": 4,
293+
"nbformat_minor": 2
294+
}

0 commit comments

Comments
 (0)