Python is the most popular language among developers as well as non-developers. Its simple syntax and one-liners take this programming to the next level. In this blog, we are going to cover the top 20 python tips and tricks that you must know.
These tricks will be covered :
- Swapping of 2 numbers
- Pattern Program using a single for loop
- Max of 3 in a single statement (Chaining)
- Walrus with the while loop
- List, Tuples and Dictionary comprehension
- Sorting a List of dictionary in a single statement
- The Prime number using for else
- Reverse of string
- Functions Annotations
- Open any website on the browser
- Print Calendar
- Is vs ==
- Packing Unpacking
- Permutation and Combination
- Check ASCII from Character or Character from ASCII
- Image Downloader
- Play music
Swapping of two numbers
In any of the programming like C or C++, you might have learned about swapping of two numbers with and without 3rd variable. Let’s see in python :
Pattern Program using a Single For loop
Python allows to multiply string with an integer, so let’s take benefit of that to print a right angle triangle using a single for loop :
Max of 3 numbers in a single statement
By default, Python does not have any ternary operator concept but we can create a chain of conditions in a single line. Let’s see :
Walrus operator with the while loop
Walrus operator is one of the awesome ways to print and initialize a variable inside a print statement. But it could be used with conditions of while loop as well :
List, Tuples and Dictionary Comprehension
Comprehension in python programming can convert your 100s of lines of codes into few lines. First, let’s see list comprehension examples :
Map function receives two arguments: function and iterable.
If you have a list or any other iterator and you want to fetch one by one values from that iterator then you need to write a loop and then if you want to pass those values to a function and in return, you are getting some results then again you have to write another function which receives the values and in return gives you a result based on some logic. For example :
What if we do this in a single line of code using lambda expression and map() ?
A Filter is almost similar to map function, the difference is that filter helps you where you are trying to return those values which meet your condition. If the condition is true then only those values will be returned :
Sorting a List of Dictionary in a single statement
Sorting a list is easy and there is a pre-defined function in python which we use to apply to sort on the list. But how we are going to sort a dictionary. Let’s see
If we use a sorted function for dictionary then :
Output: By default results will be in ascending order, to get results in descending order, pass reverse = True in sorted function.
The Prime number using for else
Whenever we learn any programming language then we also learn the prime number program with it. It is one of the most popular and common examples to learn the basic loop and if-else. But python gives a unique syntax of for…else to print these types of programs :
Reverse a String
String reverse is one of the most popular tricks in python. Reversing a string in python could be done very easily :
Python gives you a feature named ‘function annotation’ which helps you to understand the type of data function will receive in arguments and what type of data function will return.
Open any website on the browser
Python gives you a package named webbrowser which helps you to open any website. You just need to pass the website URL :
It will open any website that you enter in the name variable in your browser.
In python we can easily display a calendar of a year or a month by simply using a calendar package :
Output: Calendar of month June 2020
Zip and Longest Zip
Zip is a function in python which allows you to combine two or more list, tuples into a single list.
But what if one list contains 6 elements and another contains only 4…then we will use a package itertools to overcome this :
Is vs ==
In python when we want to compare two values then we know there is comparison operator == that we can use. But there is a keyword known as ‘is’ which also used to do the comparison but ‘is’ keyword compares the reference of two objects :
Packing Unpacking in Python
Packing unpacking is a popular technique in python, we can return multiple values from a function and receive them in separate variables as well. If we want to return multiple values from a function then we call it as packing and when we call that function then we can use the same number of variables to unpack them :
Permutation and Combination
Permutation and combination are used to get the possible combinations of an iterable. In Python, we can get the permutations and combinations using itertools package.
Permutations: return successive r-length permutations of elements in the iterable.
Combinations: return successive r-length combinations of elements in the iterable. We need to set a length always in combinations function.
Check ASCII from Character or Character from ASCII
It’s simple in python to get ASCII number of any character and any character or symbol if you know its ASCII number. Python has 2 pre-defined functions : ord() and chr().
You can download any image from a given URL in python. By using urllib.request package we can make HTTP request and in return, we get HTTP response. Let’s see :
Image will be downloaded in your machine
Playing Music files from your OS
It’s a very easy task to perform in python. You can play music or videos or any other applications by using the os package. Let’s see one example :
Start the file from your system.
Use the random package to start any random file from the directory.
So these are the top 20 python tips and tricks that can help you to improve your Python programming and create your more interest in it. In future, we will upload the latest tips and tricks.