Python has a lot of functionality (or, say, tricks) that make the language unique from other languages, such as
1. Zen Python
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
If you want to go deeper into unpacking this tuple, I suggest checking out this blog from Trey Hunner.
4. Create a web server using one line
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
This module has data structures to help you solve various real-world problems without writing a lot of code.
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.
To learn more about Itertools check this link
7. Indexed Loops: Enumeration
This is a cool feature that provides an index without defining a counter for the index
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
Check this link for more details
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 .
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:
vocabulary comprehension and given comprehension
| tr> |
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
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 "_".
“ _ "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
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
Black Hat Python, 2nd Edition: Python Programming for Hackers and Pentesters PDF, 2nd Edition. Fully updated for Python 3, the second edition of this worldwide bestseller (over 100,000 copies sold)...
The Pragmatic Programmer: Your Journey To Mastery, 20th Anniversary Edition (2nd Edition). The Pragmatic Programmer is one of those rare technical books that you will read, reread, and re-read over...
This is the first book on synthetic data for deep learning, and its extensive coverage could make this book the standard benchmark for synthetic data for years to come. The book can also serve as an i...
Pandas for Everyone: Python Data Analysis (Addison-Wesley Data & Analytics Series), 1st Edition. Pandas for Everyone brings together the practical knowledge and insights you need to solve real-worl...