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 numbersrnPattern Program using a single for looprnMax of 3 in a single statement (Chaining)rnWalrus with the while looprnList, Tuples and Dictionary comprehensionrnMaprnFilterrnSorting a List of dictionary in a single statementrnThe Prime number using for elsern Reverse of stringrn Functions Annotationsrn Open any website on the browser
Print Calendarrn Ziprn Is vs ==
Packing Unpackingrn Permutation and Combinationrn Check ASCII from Character or Character from ASCIIrn Image Downloaderrn Play music
Swapping of two numbersrnIn 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 :
rnOutput :
rnPattern Program using a Single For looprnPython 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 :
rnOutput :
rnMax of 3 numbers in a single statementrnBy default, Python does not have any ternary operator concept but we can create a chain of conditions in a single line. Let’s see :
rnOutput :
rnWalrus operator with the while looprnWalrus 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 :
rnOutput :
rnList, Tuples and Dictionary ComprehensionrnComprehension in python programming can convert your 100s of lines of codes into few lines. First, let’s see list comprehension examples :
rnOutput :
rnOutput :
rnOutput :
rnMaprnMap 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 :
rnOutput :
What if we do this in a single line of code using lambda expression and map() ?
Output :
rnFilterrnA 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 :
rnOutput :
rnSorting a List of Dictionary in a single statementrnSorting 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 :
rnOutput :
Solution :
Output: By default results will be in ascending order, to get results in descending order, pass reverse = True in sorted function.
rnThe Prime number using for elsernWhenever 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 :
rnOutput :
rnReverse a StringrnString reverse is one of the most popular tricks in python. Reversing a string in python could be done very easily :
rnOutput :
rnFunctions AnnotationsrnPython 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.
rnOutput :
rnOpen any website on the browserrnPython gives you a package named webbrowser which helps you to open any website. You just need to pass the website URL :
rnOutput:rnIt will open any website that you enter in the name variable in your browser.
rnPrint CalendarrnIn python we can easily display a calendar of a year or a month by simply using a calendar package :
rnOutput :
rnOutput: Calendar of month June 2020
rnZip and Longest ZiprnZip is a function in python which allows you to combine two or more list, tuples into a single list.
rnOutput :
But what if one list contains 6 elements and another contains only 4…then we will use a package itertools to overcome this :
rnOutput :
rnIs vs ==rnIn 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 :
rnOutput :
rnPacking Unpacking in PythonrnPacking 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 :
rnOutput :
rnPermutation and CombinationrnPermutation 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.
rnOutput :
Combinations: return successive r-length combinations of elements in the iterable. We need to set a length always in combinations function.
rnOutput :
rnCheck ASCII from Character or Character from ASCIIrnIt’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().
rnOutput :
rnImage DownloaderrnYou 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 :
rnOutput:rnImage will be downloaded in your machine
rnPlaying Music files from your OSrnIt’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 :
rnOutput:rnStart the file from your system.
rnUse the random package to start any random file from the directory.
rnConclusion :rnSo 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.