Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

Learn Top 20 Tricks of Python in just 20 minutes

Author: Brain Mentors
by Brain Mentors
Posted: Jun 13, 2020

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 :

Output :

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 :

Output :

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 :

Output :

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 :

Output :

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 :

Output :Output :Output :

Map

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 :

Output :What if we do this in a single line of code using lambda expression and map()?Output :

Filter

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 :

Output :

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 :Solution :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 :

Output :

Reverse a String

String reverse is one of the most popular tricks in python. Reversing a string in python could be done very easily :

Output :

Functions Annotations

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.

Output :

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 :

Output:

It will open any website that you enter in the name variable in your browser.

Print Calendar

In python we can easily display a calendar of a year or a month by simply using a calendar package :

Output :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.

Output :

But what if one list contains 6 elements and another contains only 4…then we will use a package itertools to overcome this :

Output :

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 :

Read Full Article Here - Learn Top 20 Tricks of Python in just 20 minutes

About the Author

Brain Mentors Pvt. Ltd. started with a mission to link the IT industry and educational institutions. We aim to transform our every student into an IT professional who is ready to be employed in the industry.

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Brain Mentors

Brain Mentors

Member since: May 12, 2020
Published articles: 5

Related Articles