Python Comprehension

A guided tour and quick start.

Keno Leon

--

Photo by Sharon McCutcheon from Pexels

Why Read this ?

If you ( like me ) are new to python and/or comprehension I hope this short tutorial will get you up to speed. In a nutshell, comprehension is a way to write concise ( short and effective) python code.

Much of comprehension in python deals with how you speak the language and much like your real language, you can say something with a lot of words (verbose) or say a lot with a few words (succinct).

Even if you prefer verbose code ( I sometimes do ), concise code is widely used, so if you want to read others code it pays to learn about it, let’s start with a plain vanilla verbose example so we have something to work with.

""" Here we are simply populating a list """boxODonuts = []
FLAVORS = ['Glaze', 'Chocolate', 'Vanilla', 'Cinnamon']
for flavor in FLAVORS:
boxODonuts.append(flavor + ' donut')
print(boxODonuts)OUTPUT:
['Glaze donut', 'Chocolate donut', 'Vanilla donut', 'Cinnamon donut']

--

--

Responses (1)