Saturday, November 29, 2014

Awesome Python One-liners

Hi there! Today I thought I would show you a bunch of awesome python one-liners. Full disclosure: I did not write any of these myself. Some of them are from here and some of them are from here.


How much is a terabyte or a megabyte? Find out with this code:

>>> import pprint;pprint.pprint(zip(('Byte', 'KByte', 'MByte', 'GByte', 'TByte'), (1 << 10 * i for i in xrange(5))))
[('Byte', 1),
 ('KByte', 1024),
 ('MByte', 1048576),
 ('GByte', 1073741824),

 ('TByte', 1099511627776L)]
Note: Python 3 users, you will not have "L" at the end of TByte.

What is the largest number that can be represented by 1 Byte? How about 128 Bytes?
>>> print '\n'.join("%i Byte = %i Bit = largest number: %i" % (j, j*8, 256**j-1) for j in (1 << i for i in xrange(8)))
1 Byte = 8 Bit = largest number: 255
2 Byte = 16 Bit = largest number: 65535
4 Byte = 32 Bit = largest number: 4294967295
8 Byte = 64 Bit = largest number: 18446744073709551615
16 Byte = 128 Bit = largest number: 340282366920938463463374607431768211455
32 Byte = 256 Bit = largest number: 115792089237316195423570985008687907853269984665640564039457584007913129639935
64 Byte = 512 Bit = largest number: 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084095

128 Byte = 1024 Bit = largest number: 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137215

Here is a function that gives all the possible permutations of the numbers passed to it in a list:
>>> f = lambda l: reduce(lambda z, x: z + [y + [x] for y in z], l, [[]])
>>> f([1, 2, 3, 4])
[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3], [4], [1, 4], [2, 4], [1, 2, 4], [3, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4]]
Note: Python 3 users, this function must be rewritten:
>>> import functools;f = lambda l: functools.reduce(lambda z, x: z + [y + [x] for y in z], l, [[]])

Here is a function that gives all the prime numbers up to its parameter:
>>> f = lambda n: sorted(set(range(2,n+1)).difference(set((p * f) for p in range(2,int(n**0.5) + 2) for f in range(2,(n/p)+1))))
>>> f(50)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
Note: Python 3 users, if this does not work, change set(range(2,n+1)) to set(list(range(2,n+1))).

To finish off I'd like to show you a useful one-liner that can be applied to several applications. It is the one-line if statement. This is its structure:
expression1 if condition else expression2
The way it works is simple. First it evaluates condition. If it is True, expression1 is evaluated and returned. If it is False, expression2 is evaluated and returned. Sample usage:
>>> n = 10
>>> s = 'Hi!' if n == 12 else 'Bye!'
>>> s
'Bye!'
>>> n = 12
>>> s = 'Hi!' if n == 12 else 'Bye!'
>>> s
'Hi!'

Well, that's it for now! Hope you try out these one-liners and show them to your friends.