Skip to content

Instantly share code, notes, and snippets.

@nepsilon
Last active October 20, 2016 02:11
Show Gist options
  • Save nepsilon/908292e20c9574729cd2785e4584e1d0 to your computer and use it in GitHub Desktop.
Save nepsilon/908292e20c9574729cd2785e4584e1d0 to your computer and use it in GitHub Desktop.
3 hidden Python tips — First published in fullweb.io issue #60

3 hidden Python tips 🐍

Hidden from beginner tutorials and seldom found in books.

1. You can multiply a string to repeated it:

>>> "HOOLI-"*2
'HOOLI-HOOLI-'

2. Chaining comparison operators:

>>> f = 2
>>>  1 < f < 3
True
>>>  1 < f < 3 < (f+2)
True

3. Flatten lists with sum():

>>> a = [[1,2,3], [4,5,6], [7,8,9]]
>>>  sum(a, [])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment