Skip to content

Commit 89239f9

Browse files
datetime and tips and tricks
1 parent aeeb4ac commit 89239f9

File tree

3 files changed

+961
-0
lines changed

3 files changed

+961
-0
lines changed
Lines changed: 380 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Tips & Tricks"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 3,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"1\n"
20+
]
21+
}
22+
],
23+
"source": [
24+
"condition=True\n",
25+
"if condition:\n",
26+
" x=1\n",
27+
"else:\n",
28+
" x=0\n",
29+
"print(x)\n"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 4,
35+
"metadata": {},
36+
"outputs": [
37+
{
38+
"name": "stdout",
39+
"output_type": "stream",
40+
"text": [
41+
"0\n"
42+
]
43+
}
44+
],
45+
"source": [
46+
"condition=False\n",
47+
"x=1 if condition else 0\n",
48+
"print(x)"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": 5,
54+
"metadata": {},
55+
"outputs": [
56+
{
57+
"name": "stdout",
58+
"output_type": "stream",
59+
"text": [
60+
"10100000000\n"
61+
]
62+
}
63+
],
64+
"source": [
65+
"num1=10000000000\n",
66+
"num2=100000000\n",
67+
"print(num1+num2)"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 8,
73+
"metadata": {},
74+
"outputs": [
75+
{
76+
"name": "stdout",
77+
"output_type": "stream",
78+
"text": [
79+
"10100000000\n",
80+
"10,100,000,000\n"
81+
]
82+
}
83+
],
84+
"source": [
85+
"num1=10_000_000_000\n",
86+
"num2=100_000_000\n",
87+
"print(num1+num2)\n",
88+
"print(f'{num1+num2:,}')"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 9,
94+
"metadata": {},
95+
"outputs": [
96+
{
97+
"name": "stdout",
98+
"output_type": "stream",
99+
"text": [
100+
"0 alex\n",
101+
"1 john\n",
102+
"2 jane\n",
103+
"3 richard\n",
104+
"4 ross\n",
105+
"5 micahael\n"
106+
]
107+
}
108+
],
109+
"source": [
110+
"names=['alex','john','jane','richard','ross','micahael']\n",
111+
"index=0\n",
112+
"for name in names:\n",
113+
" print(index,name)\n",
114+
" index+=1"
115+
]
116+
},
117+
{
118+
"cell_type": "code",
119+
"execution_count": 10,
120+
"metadata": {},
121+
"outputs": [
122+
{
123+
"name": "stdout",
124+
"output_type": "stream",
125+
"text": [
126+
"0 alex\n",
127+
"1 john\n",
128+
"2 jane\n",
129+
"3 richard\n",
130+
"4 ross\n",
131+
"5 micahael\n"
132+
]
133+
}
134+
],
135+
"source": [
136+
"names=['alex','john','jane','richard','ross','micahael']\n",
137+
"for index,name in enumerate(names):\n",
138+
" print(index,name)\n",
139+
" "
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": 11,
145+
"metadata": {},
146+
"outputs": [
147+
{
148+
"name": "stdout",
149+
"output_type": "stream",
150+
"text": [
151+
"Stark is Iron-Man\n",
152+
"Bucky is Winter Soldier\n",
153+
"Banner is Hulk\n",
154+
"natasha is Black Widow\n",
155+
"Peter Parker is Spider Man\n"
156+
]
157+
}
158+
],
159+
"source": [
160+
"names=['Stark','Bucky','Banner','natasha','Peter Parker']\n",
161+
"heroes=['Iron-Man','Winter Soldier','Hulk','Black Widow','Spider Man']\n",
162+
"for name,hero in zip(names,heroes):\n",
163+
" print(name,'is',hero)"
164+
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": 12,
169+
"metadata": {},
170+
"outputs": [
171+
{
172+
"name": "stdout",
173+
"output_type": "stream",
174+
"text": [
175+
"1 2\n",
176+
"1\n"
177+
]
178+
}
179+
],
180+
"source": [
181+
"a,b=1,2\n",
182+
"print(a,b)\n",
183+
"# if varaible b value is not used anywhere we can skip it by replacing b by underscore(_)\n",
184+
"a,_=1,2\n",
185+
"print(a)"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": 14,
191+
"metadata": {},
192+
"outputs": [
193+
{
194+
"ename": "ValueError",
195+
"evalue": "too many values to unpack (expected 3)",
196+
"output_type": "error",
197+
"traceback": [
198+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
199+
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
200+
"\u001b[1;32m<ipython-input-14-d68b62ee792e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
201+
"\u001b[1;31mValueError\u001b[0m: too many values to unpack (expected 3)"
202+
]
203+
}
204+
],
205+
"source": [
206+
"a,b,c=1,2,3,4,5\n",
207+
"print(a,b,c)"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": 15,
213+
"metadata": {},
214+
"outputs": [
215+
{
216+
"name": "stdout",
217+
"output_type": "stream",
218+
"text": [
219+
"1 2 [3, 4, 5]\n"
220+
]
221+
}
222+
],
223+
"source": [
224+
"a,b,*c=1,2,3,4,5\n",
225+
"print(a,b,c)"
226+
]
227+
},
228+
{
229+
"cell_type": "code",
230+
"execution_count": 16,
231+
"metadata": {},
232+
"outputs": [
233+
{
234+
"name": "stdout",
235+
"output_type": "stream",
236+
"text": [
237+
"1 2\n"
238+
]
239+
}
240+
],
241+
"source": [
242+
"a,b,*_=1,2,3,4,5\n",
243+
"print(a,b)"
244+
]
245+
},
246+
{
247+
"cell_type": "code",
248+
"execution_count": 18,
249+
"metadata": {},
250+
"outputs": [
251+
{
252+
"name": "stdout",
253+
"output_type": "stream",
254+
"text": [
255+
"1 2 [3, 4, 5, 6, 7, 8, 9] 10\n"
256+
]
257+
}
258+
],
259+
"source": [
260+
"a,b,*c,d=1,2,3,4,5,6,7,8,9,10\n",
261+
"print(a,b,c,d)"
262+
]
263+
},
264+
{
265+
"cell_type": "code",
266+
"execution_count": 19,
267+
"metadata": {},
268+
"outputs": [
269+
{
270+
"name": "stdout",
271+
"output_type": "stream",
272+
"text": [
273+
"1 2 10\n"
274+
]
275+
}
276+
],
277+
"source": [
278+
"a,b,*_,d=1,2,3,4,5,6,7,8,9,10\n",
279+
"print(a,b,d)"
280+
]
281+
},
282+
{
283+
"cell_type": "code",
284+
"execution_count": 22,
285+
"metadata": {},
286+
"outputs": [
287+
{
288+
"name": "stdout",
289+
"output_type": "stream",
290+
"text": [
291+
"Praful 10000 BFSI\n",
292+
"Praful 10000 BFSI "
293+
]
294+
}
295+
],
296+
"source": [
297+
"class employee:\n",
298+
" pass\n",
299+
"emp=employee()\n",
300+
"dict_attr={'name':'Praful','salary':10000,'dept':'BFSI'}\n",
301+
"for key,value in dict_attr.items():\n",
302+
" setattr(employee,key,value)\n",
303+
"\n",
304+
"# access by attribute name\n",
305+
"print(emp.name,emp.salary,emp.dept) \n",
306+
"\n",
307+
"for key in dict_attr.keys():\n",
308+
" print(getattr(employee,key),end=' ')"
309+
]
310+
},
311+
{
312+
"cell_type": "code",
313+
"execution_count": 23,
314+
"metadata": {},
315+
"outputs": [
316+
{
317+
"name": "stdout",
318+
"output_type": "stream",
319+
"text": [
320+
"username:user\n",
321+
"password:password\n",
322+
"Logging in...\n",
323+
"Hello user\n"
324+
]
325+
}
326+
],
327+
"source": [
328+
"username=input('username:')\n",
329+
"password=input('password:')\n",
330+
"print('Logging in...')\n",
331+
"print('Hello ',username)"
332+
]
333+
},
334+
{
335+
"cell_type": "code",
336+
"execution_count": 27,
337+
"metadata": {},
338+
"outputs": [
339+
{
340+
"name": "stdout",
341+
"output_type": "stream",
342+
"text": [
343+
"username:user1\n",
344+
"password:········\n",
345+
"Logging in...\n",
346+
"Hello user1\n"
347+
]
348+
}
349+
],
350+
"source": [
351+
"from getpass import getpass\n",
352+
"username=input('username:')\n",
353+
"password=getpass('password:')\n",
354+
"print('Logging in...')\n",
355+
"print('Hello ',username)"
356+
]
357+
}
358+
],
359+
"metadata": {
360+
"kernelspec": {
361+
"display_name": "Python 3",
362+
"language": "python",
363+
"name": "python3"
364+
},
365+
"language_info": {
366+
"codemirror_mode": {
367+
"name": "ipython",
368+
"version": 3
369+
},
370+
"file_extension": ".py",
371+
"mimetype": "text/x-python",
372+
"name": "python",
373+
"nbconvert_exporter": "python",
374+
"pygments_lexer": "ipython3",
375+
"version": "3.7.1"
376+
}
377+
},
378+
"nbformat": 4,
379+
"nbformat_minor": 4
380+
}

0 commit comments

Comments
 (0)