What Does Python Cool Do?

Counters | File handling | Loops | NumPy | Python Methods and Functions | Regular Expressions

Python has a lot of functionality (or, say, tricks) that make the language unique from other languages, such as

1. Zen Python

import this

If you enter this command in the terminal, you get "Python Zen by Tim Peters" to help you improve readability. usability and maintainability of Python code.

Check out this video for more details

3. Swapping two variables on one line

Python provides cool functionality for swapping two variables on one line using what is 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}" )

 

a, b = b, a

 

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

If you want to go deeper into unpacking this tuple, I suggest checking out this blog from Trey Hunner.

Unpacking tuples

4. Create a web server using one line

python - m http.server 8000

To create a simple file sharing application, go to the folder you want to share and enter the above command, then go to your browser and enter

 127.0. 0.1: 8000 

to open this folder in your browser, you can use it from other devices, even if you are on the same network.

Here link to read more about it

5 ... All data structure in 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))

This module has data structures to help you solve various real-world problems without writing a lot of code. 
Collections

6. Python Pearl: Itertools

Itertools — one of the most important standard libraries available in Python 3, which has many built-in functions. Itertools provides functionality to create fast, memory efficient and beautiful code.

There are many useful functions in the Itertools module, let`s take a look at one of the most popular.

import itertools

 

itertools.permutations ( `ab` )

To learn more about Itertools check this link

Itertools

7. Indexed Loops: Enumeration

This is a cool feature that provides an index without defining a counter for the index

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

 

for i, value in enumerate (mylist):

  print (i, `: `  , value)

8. Reverse a list

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

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

 

list ( reversed (lst))

Check this link for more details

Reverse the list

9. Adding two lists with Zip

Let`s say you have two lists and you want to add the elements of that list, then python has a Zip function that comes in handy and gives you the result without using a nested loop .

a = [ 1 , 2 , 3 ]

 

b = [ 4 , 5 , 6 ]

 

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

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

The Zip operation is popular in Data Science due to matrix multiplication, where Zip can be used to multiply rows and columns.

10. List / Set / Dict comprehension

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

let`s say , you want to square an even number from 1 to 20

If you use a regular if-else, the code looks like this:

If you are using list comprehension, you just need to type less code

square_list = []

 

for number in range ( 1 , 20 ):

if number % 2 = = 0 :

square_list.append (number * number)

 

print (square_list)

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

 

print (square_list)

vocabulary comprehension and given comprehension

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

 

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

 

print (my_dict)

  

print (my_set)

11. Modern vocabulary

The Python vocabulary is so powerful that if you dig deeper into python, everything revolves around the object and the vocabulary.

If you want to learn more about the vocabulary, then check out this video, you will learn many important vocabulary features

12. Pretty Print

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

import pprint

 

pp = pprint.PrettyPrinter (indent = 4 )

 
pp.pprint (my_dict)

This is great when working with a large dictionary or if you are working with a JSON file, then you can use pprint to print the JSON file.

13. Use the interactive operator "_".

2 + 2

 

print (_)

“ _ "Indicates 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

Numpy
Pandas
Scikit-Learn
Scrapy
Beautiful Soup
OpenCV
Queries
Matplotlib
Pygame
SQLAlchemy
SciPy
Twisted Python

After going through all the interesting functions, your feeling

This is all about Python from my side. If you have any doubts or would like to add something, please comment below.

If you liked my article, you can follow me

Quora: https://www.quora.com/profile/Shankar-Jha-20
Medium: https://medium.com/@shankarj67
Twitter: https://twitter.com/Skhk634





Get Solution for free from DataCamp guru