What makes the Python
1,779 reads
1,779 reads

What makes the Python Cool.

by Shankar JhaJuly 23rd, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

As the topic says, we will look into some of the cool feature provided by <a href="" target="_blank">Python</a>.

Company Mentioned

Mention Thumbnail
featured image - What makes the Python Cool.
Shankar Jha HackerNoon profile picture

As the topic says, we will look into some of the cool feature provided by Python.

Python has a lot of functionality (or say tricks) which makes the language unique from another language such as

1. The Zen of Python

>> import this

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren't special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to d

Although that way may not be obvious at first unless you're Dutc

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it's a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea -- let's do more of those!

If you type this command on the terminal you will get “The Zen of Python, by Tim Peters” which will help you to improve the readability, usability and maintainability of Python code.

Check out this video for more detail

2. XKCD Comics

>> import antigravity

If you type this command on the terminal, you will get a cool comics in your browser like this

3. Swapping of two variable in one line

Python provides a cool functionality to swap two variables in one line using something called tuple unpacking which will make your code shorter and easier to read

>> a = 10

>> b = 20

>> print(f"Before swapping value of a = {a} and b = {b}")

Before swapping value of a = 10 and b = 20

>> a, b = b, a

>> print(f"After swapping value of a = {a} and b = {b}")

After swapping value of a = 20 and b = 10

If you want to dig deeper into this tuple unpacking, I will suggest to check out this blog by trey hunner

Multiple assignment and tuple unpacking improve Python code readability - Trey Hunner_Multiple assignment and tuple unpacking improve Python code readability Mar 7th, 2018 4:30 pm | Comments Whether I'm…

4. Create a web server using one line

>> python -m http.server 8000

To create a simple file sharing application go to your folder which you want to share and type the above command then go to your browser and type to open that folder in your browser, you can use this from other devices also if you are in the same network.

Here is a link to know more about this

5. All Data Structure at one place: Collections

>> from collections import Counter

>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]

>> print(Counter(myList))

Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})

This module has data structures which will help you to solve various real-life problems without writing too much code.

8.3. collections - Container datatypes - Python 3.7.0 documentation_Property returning a new containing all of the maps in the current instance except the first one. This is useful for…

6. Gem of python: Itertools

Itertools is one of the most important standard library available in Python 3 which has a lot of features inbuilt. Itertools provides the functionality to create fast, memory-efficient, and good-looking code.

You will find a lot of useful function in Itertools module, let us look into one the popular one

>> import itertools

>> itertools.permutations('ab')

[('a', 'b'), ('b', 'a')]

To learn more about Itertools check-out this link

Itertools in Python 3, By Example - Real Python_It has been called a "gem" and "pretty much the coolest thing ever," and if you have not heard of it, then you are…

7. Looping with Index: Enumerate

This is a cool feature which provides the index without having to define any counter for index

>> mylist = [1,13,16,15,80]

>> for i, value in enumerate(mylist):

print( i, ': ', value)

0 : 1

1 : 13

2 : 16

3 : 15

4 : 80

8. Reversing a list

The reverse is always the tedious task in any programming language but Python’s built-in reversed() function allows you to create a reverse of a list in one line

>> lst = [1, 2, 3, 4, 5]

>> list(reversed(lst))

[5, 4, 3, 2, 1]

For more details check-out this link

How to Reverse a List in Python - dbader.org_A step-by-step tutorial on the three main ways to reverse a Python list or array: in-place reversal, list slicing, and…

9. Adding two lists using Zip

let us say you have two lists and you want to add the elements of that list then python is having a Zip function which will come in handy and give you the result without using a nested loop

>> a = [1,2,3]

>> b = [4,5,6]

>> for i,j in zip(a,b):

>> print("Sum of a and b is", i+j)

Sum of a and b is 5

Sum of a and b is 7

Sum of a and b is 9

Zip operation is popular in Data Science because of the matrix multiplication where Zip can be used to do Row and Column multiplication.

10. List/Set/Dict comprehension

Comprehension provides the easiest way to define any complicated code in one line

let us say you want to square the even number from 1–20

If you use the normal if-else then the code will be like

>> square_list = []

>> for number in range(1,20):

>> if number % 2 == 0:

>> square_list.append(number*number)

>> print(square_list)

[4, 16, 36, 64, 100, 144, 196, 256, 324]

If using list comprehension, you just have to type a less code

>> square_list = [number*number for number in range(1,20) if number%2==0]

>> print(square_list)

[4, 16, 36, 64, 100, 144, 196, 256, 324]

In the same way dictionary comprehension and set comprehension can be used

>> my_dict = {i: i * i for i in range(10)}

>> my_set = {i * 10 for i in range(10)}

>> print(my_dict)

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

>> print(my_set)

{0, 70, 40, 10, 80, 50, 20, 90, 60, 30}

11. Modern Dictionary

Python dictionary is so powerful that if you go deeper into python then everything revolves around object and dictionary.

If you want to learn more about the dictionary then check this video, you will learn a lot of important feature of dictionary

12. Pretty Print

This is the easiest way to print the list and dictionary in a beautiful way by doing

>> import pprint

>> pp = pprint.PrettyPrinter(indent=4)

>> pp.pprint(my_dict)

This comes in handy when working with a large dictionary or if you are working with JSON file then you can use pprint to print the JSON file.

13. Use Interactive “_” Operator.

>>> 2 + 2


>>> _


>>> print(_)


The “_” references to the output of the last executed expression.

On the top of this Python also provides a lot of external libraries which has a better feature than any programming language, I am naming a few of the top library below

  1. Numpy
  2. Pandas
  3. Scikit-Learn
  4. Scrapy
  5. Beautiful Soup
  6. OpenCV
  7. Requests
  8. Matplotlib
  9. Pygame
  10. SQLAlchemy
  11. SciPy
  12. Python Twisted

After going through all of the cool features, your feeling is like

That’s all about Python from my side, If you have any doubt or you want to add something, please comment below.