You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The module pathlib is a clean way to intereact with the file system, far better than os.path or the glob module
101
103
@@ -113,7 +115,7 @@ print(path)
113
115
# threads/sub/sub-sub
114
116
```
115
117
116
-
5. **Type Hints (Python 3.5+)**
118
+
**Type Hints (Python 3.5+)**
117
119
118
120
Typing isnot necessary in Python, but it is useful for develpers after you, or you in few years. It makes your code cleaner and more understable.
119
121
@@ -128,7 +130,7 @@ Typing is not necessary in Python, but it is useful for develpers after you, or
128
130
returnself.width *self.height
129
131
```
130
132
131
-
6. **f-strings (3.6+)**
133
+
**f-strings (3.6+)**
132
134
133
135
Don't use .fomrat() to print your strings, f-strings is much more convinient and easier to maintain.
134
136
@@ -147,7 +149,7 @@ print(f"x = {x} and y = {y}.")
147
149
148
150
```
149
151
150
-
7. **Extendede iterable unpacking (Python 3.0+)**
152
+
**Extendede iterable unpacking (Python 3.0+)**
151
153
152
154
Using this trick, while unpacking an iterable, you can specify a "catch-all" variable that will be assigned a list of the items not assigned to a regular variable.
153
155
@@ -160,7 +162,7 @@ print(head, body, tail)
160
162
161
163
```
162
164
163
-
8. **Async IO5Python3.4+**
165
+
**Async IO5Python3.4+**
164
166
165
167
The asyncio is a good module to write asynchronous code.
166
168
```python
@@ -181,7 +183,7 @@ asyncio.un(hello())
181
183
# World
182
184
```
183
185
184
-
9. **Underrscoores in Numeric Literals (Python 3.6+)**
186
+
**Underrscoores in Numeric Literals (Python 3.6+)**
185
187
186
188
```python
187
189
@@ -192,7 +194,7 @@ printf(x, y, x == y)
192
194
193
195
```
194
196
195
-
10. **Swapping Two Variables**
197
+
**Swapping Two Variables**
196
198
197
199
First idea when you need to swipe variables is to use a temporary variable, and blabla. But there's a better method without use a temporary variable, as such:
198
200
@@ -212,7 +214,7 @@ a, b = b, a
212
214
# a = 60, b = 50
213
215
```
214
216
215
-
11. **Reversing a string**
217
+
**Reversing a string**
216
218
217
219
In addition of swapping quickly a variable you need (not often, ok) to reverse a string, and it can be done extremely quickly.
218
220
@@ -222,7 +224,7 @@ rev_string = my_string[::-1]
222
224
# ROT
223
225
```
224
226
225
-
12. **Splitting words in a line**
227
+
**Splitting words in a line**
226
228
227
229
Of course, the old method of using .split() is a good way to do it.
Imagine you have ['This', 'is', 'a', 'sentence'], what to do?
238
240
239
241
Use the ```python "".join(string)```
240
242
241
-
14. **Joining two string using addition operator**
243
+
**Joining two string using addition operator**
242
244
243
245
```python
244
246
a=" I think "
@@ -247,62 +249,66 @@ print(a+b)
247
249
# I think Python is great
248
250
```
249
251
250
-
15. **More than one conditionnal operators**
252
+
**More than one conditionnal operators**
251
253
252
254
In contrario of thers programming langage, using mathematically borned notation is accepted in Python :)
253
255
254
256
```python
255
257
if ( 1< a <20):
256
258
```
257
259
258
-
16.**Find most frequent element in a list**
260
+
**Find most frequent element in a list**
259
261
260
262
```python
261
-
list= [1, 2, 3, 2, 2, 1, 1]
262
-
frequent =max(set(list), key=list.count)
263
-
# 1
263
+
list= [1, 2, 3, 2, 2, 1, 1]
264
+
frequent =max(set(list), key=list.count)
265
+
# 1
264
266
```
265
267
266
-
17.**Count occurence of elements in a list**
268
+
**Count occurence of elements in a list**
267
269
268
270
```python
269
-
from collections import Counter
270
-
print(Counter(list))
271
+
from collections import Counter
272
+
273
+
print(Counter(list))
271
274
```
272
275
273
-
18.**Repeating the element multiple times**
276
+
**Repeating the element multiple times**
274
277
275
278
```python
276
279
my_list = [3]
277
280
my_list = my_list*5
278
281
# [3, 3, 3, 3, 3]
279
282
```
280
283
281
-
19.**Using Ternary Operator**
284
+
**Using Ternary Operator**
282
285
283
286
```python
284
287
print("Eligible") if age>20elseprint("Not Eligible")
285
288
# Eligible
286
289
```
287
290
288
-
20.**Rounding with Floor and Ceil**
291
+
**Rounding with Floor and Ceil**
289
292
290
293
```python
291
294
import math
295
+
292
296
my_number =18.7
293
297
print(math.floor(my_number))
298
+
# 18
294
299
print(math.ceil(my_number))
300
+
# 19
295
301
```
296
302
297
-
21.**Function in one line**
303
+
**Function in one line**
298
304
299
305
```python
300
306
x =lambdaa,b,c : a+b+c
301
307
print(x(10, 20, 30))
302
308
# 60
303
309
```
304
310
305
-
22.**Apply function for each element in a list**
311
+
**Apply function for each element in a list**
306
312
307
313
```python
308
314
l = ["a", "b"]
@@ -311,7 +317,7 @@ print(list(l))
311
317
# ['a', 'b']
312
318
```
313
319
314
-
23.**Lambda functon on each element in a list**
320
+
**Lambda functon on each element in a list**
315
321
316
322
```python
317
323
l = [1, 2, 3, 4, 5]
@@ -320,7 +326,7 @@ print(list(nl))
320
326
# [1, 4, 9, 16, 25]
321
327
```
322
328
323
-
24.**Return multiple values from a function**
329
+
**Return multiple values from a function**
324
330
```python
325
331
deffunction(n):
326
332
return1,2,3,4
@@ -329,7 +335,7 @@ print(a,b,c,d)
329
335
# 1 2 3 4
330
336
```
331
337
332
-
25.**Filtering the values using filter function**
338
+
**Filtering the values using filter function**
333
339
334
340
```python
335
341
defeligibility(age):
@@ -339,26 +345,27 @@ age = filter(eligibility, list_of_age)print(list(age))
339
345
# [24, 27, 33, 30, 26, 25]
340
346
```
341
347
342
-
26.**Merging two dictionnaries**
348
+
**Merging two dictionnaries**
343
349
344
350
```python
345
-
d1 = {'One':1, 'Two':2}
346
-
d2 = {'Two':2, 'Three':3}
351
+
d1 = {'Abra':1, 'Cadabra':2}
352
+
d2 = {'Cadabra':2, 'World':3}
347
353
dicti = {**d1, **d2}
348
354
print(dicti)
349
-
# {'One': 1, 'Two': 2, 'Three': 3}
355
+
# {'Abra': 1, 'Cadabra': 2, 'World': 3}
350
356
```
351
357
352
-
27.**Getting size of an object**
358
+
**Getting size of an object**
353
359
354
360
```python
355
361
import sys
362
+
356
363
a =5
357
364
print(sys.getsizeof(a))
358
365
# 28
359
366
```
360
367
361
-
28.**Two lists into diictionnary**
368
+
**Two lists into dictionnary**
362
369
363
370
```python
364
371
l1 = ["One","Two","Three"]
@@ -368,20 +375,22 @@ print(dicti)
368
375
# {'Two': 2, 'One': 1, 'Three': 3}
369
376
```
370
377
371
-
29.**Calculating execution time for a program**
378
+
**Calculating execution time for a program**
372
379
373
380
```python
374
381
import time
382
+
375
383
start = time.clock()
376
384
for x inrange(1000):
377
385
pass
386
+
378
387
end = time.clock()
379
-
total = end - start
380
-
print(total)
381
-
# 0.00011900000000000105
388
+
389
+
print(end - start)
390
+
# 0.0002196058585480000
382
391
```
383
392
384
-
30.**Removing duplicate elements in list**
393
+
**Removing duplicate elements in list**
385
394
386
395
```python
387
396
l = [1,4,1,8,2,8,4,5]
@@ -390,16 +399,20 @@ print(l)
390
399
# [8, 1, 2, 4, 5]
391
400
```
392
401
393
-
31.**Multiple assignment per variables**
402
+
**Multiple assignment per variables**
394
403
395
404
```python
396
-
a, b =50, 60
397
-
# a = 50, b = 60
398
-
a, *b =50, 60, 70
399
-
#50, [60, 70]
400
-
a = b = c =50
401
-
# a = 50, b = 50, c = 50
405
+
a, b =10, 20
406
+
# a = 10, b = 10
407
+
a, *b =10, 20, 30
408
+
#10, [20, 30]
409
+
a = b = c =10
410
+
# a = 10, b = 10, c = 10
402
411
```
403
412
_________________
404
413
414
+
Of course, this is not a complete list of all existing Python's tricks, but it may be useful to know Python can do a lot of useful things in simple way. There is beauty in simplicity.
415
+
416
+
If you want to read more articles on Python, see the excellent [How to Hire a Great Python Developer](https://www.toptal.com/python#hiring-guide) from our friends at [Toptal](https://www.toptal.com/)!
0 commit comments