HomeToolsAbout

Pythonic Style

What is Pythonic Style

Tuple Unpacking

A tuple is similar to a list except that it’s immutable.

Unpacking contents of tuple can be done via index:

some_tuple = ('this', 'is', 'a', 'tuple') word_1 = some_tuple[0] word_2 = some_tuple[1] word_3 = some_tuple[2] word_4 = some_tuple[3]

Python uses multiple assignment.

  • you can assign all the variables in one-line like variable destructure in JS.
some_tuple = ('this', 'is', 'a', 'tuple') word_1, word_2, word_3, word_4 = some_tuple

List Comprehensions

Let's say we want standard name capitalization applied to a list of strings.

You can use a for-loop to accomplish this:

names = ['faKer', 'ZEus', 'keria', 'oNEr', 'Guma'] for i in range(len(names)): names[i] = names[i].title() names # ['Faker', 'Zeus', 'Keria', 'Oner', 'Guma']
names = ['faKer', 'ZEus', 'keria', 'oNEr', 'Guma'] names = [name.title() for name in names] names # ['Faker', 'Zeus', 'Keria', 'Oner', 'Guma']
AboutContact