Few killer concepts in Python

Nimesh Mohanakrishnan
3 min readJul 24, 2021

Python programming gets very interesting every day. I was exhausted ideating on the content for my recent design project and hence, decided to take time and clear my mind. And yes, I choose to write a code. It is funny to notice that I choose to program when I get exhausted because, during my internship, I do the other way round. :)

Anyway, It has been quite a long time I have written a python code. Hence I decided to kill the time to revisit certain concepts that excited me when I was learning python. I believe these are a few of the killer concepts in python that enable you to write faster and compact code.

1. Comprehensions

Comprehensions in python allow you to write less-space code making it more compact. It can be convenient to write optimized and faster code so that the python interpreter can spot a predictable pattern during loops. One more advantage is code readability. For experienced developers, it is easy to read the code whereas, it might be difficult for beginners until they grasp the concept.

In python, we can generate lists and dictionaries comprehensions.

List Comprehension:

List comprehension in python follows the following syntax.

Sytax(Without condition):

[<expression> for <iterable unit> in <iterable or pattern>]

Syntax(With Condition):

[<expression> for <iterable unit> in <iterable or pattern> if <condition>]

Output:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 27, 125, 343, 729]

The first comment shows the simple functionality of appending the square of the numbers from one list to another using a “for loop”. The second comment shows the list comprehension for the same functionality. The third comment is different and interesting. It shows the list comprehension with a condition.

Dictionary Comprehension:

Dictionary comprehension in python follows the following syntax.

Sytax(Without condition):

{ key: value for key, value in <iterable or sequence> }

Sytax(With condition):

{ key:value for key, value in <iterable or sequence> if <condition>}

Output:

{1: 4, 2: 3, 3: 2, 4: 1, 5: 2, 6: 1, 7: 2, 8: 1}
{1: 4, 2: 3, 3: 2, 4: 1, 5: 2, 6: 1, 7: 2, 8: 1}
{1: 4, 3: 2, 5: 2, 7: 2}

2. Enumerate Function

The enumerate function takes a collection and returns an enumerate object. Suppose there is a list and, we have to print the index and the corresponding element(iterable). To achieve this, we can use enumerate function.

Output:

0 1
1 2
2 3
3 4
4 5
{0: 1, 1: 2, 2: 3, 3: 4, 4: 5}

3. Zip Function

We use the zip function in python when we need to collate data items present in two or more different tuples, dictionaries, or lists. It will create an iterator that will aggregate elements from two or more iterable.

In the above function, we combine the element present in the list of fruits and vegetables into a “zipped” list.

Output:

[(‘orange’, ‘tomato’), (‘apple’, ‘cucumber’), (‘banana’, ‘onion’), (‘grapes’, ‘capsicum’)]

Python is vast and yet simple. There are a lot more concepts that make it an exceptional coding language. The importance of any concept depends on the given context and situation. But I believe the concepts mentioned above are a general collative of high significance.

--

--

Nimesh Mohanakrishnan

Aspiring UX Researcher & Designer | Design thinking Practitioner trained by University Innovation Fellows, Stanford d.school