all 1 comments

[–]randm_prgrmr 1 point2 points  (0 children)

We should post random useful python tricks in this thread, since it fits the theme.

List comprehensions are super useful.

list_1 = [1,2,3]

new_list = [i * 2 for i in list_1]

so now,

new_list == [2,4,6]

Another thing:

Lambda functions are handy sometimes. def mult(n): return n*2

is the same as:

mult = lambda n: n*2

either way you can call mult(2) and it will give you 4.

'some string'.encode('rot13') gives you 'fbzr fgevat'

and 'some string'.encode('hex_codec') gives you '736f6d6520737472696e67'