What are the Cool Tricks in Python?

Python tricks or tips as you may want to call it can include a number of ways in which you can make your code efficient, reusable and even innovative. There are a number of ways in which you can apply these python tricks:

Merging Two Dictionaries

a = {'x' : 10, 'y': 20}
b = {'y':30, 'z':40}
c = {a**, b**}


{'x': 10, 'y': 30, 'z': 40}

Creating A Lambda Function

Another cool trick in python is using the lambda operator or lambda function which is used to create a function that you may want to be remained anonymous; means you only create this function when you need it.

temp = lambda x, y: x + y
print(temp(9, 3))



Creating a String Out Of The Elements Inside a List

You can create a string out of all the elements inside a list by using the join() method:

list = ["I", "am", "cool"]
print(" ".join(list))


I am cool

Swapping Values

This one is a tricky one! But you can easily swap values of two variables and assign it to some other variables, later, you can call out your variables which you declared initially.

a, b = 1, 2
print(a, b) 
a, b = b, a 
print(a, b)


1 2
2 1

Reverse String in Python

You can reverse a string by slicing the index negatively and then printing it on the screen:

string ="Python"



Printing the File Location of The Modules

import numpy; 
import pandas; 


Returning a Value Even If It’s Not Present in The Dictionary

You can add/return a value even it’s not present in the dictionary by using the get() method:

dict = {'a': 20, 'b': 90}
print(dict.get('c', 38))



Emoji in Python

Yes, it’s true that you can import a library of emojis and use them in python; In order to install an emoji, go to this link here:

Emoji in Python

and once you are done installing emoji on your system, you can use the following code:

import emoji
print(emoji.emojize('Python is :thumbs_up:'))


Python is 👍


This module lets you retrieve info, author’s details, images and meta information about the article that you want to read online.


Documentation of this article is pretty simple, all you need to do is install it by following a few steps here:


Wikipedia in Python

This module in python allows you to retreive all the information present on wikipedia depending on your need, so if you want a quick update on who is who and what is what through python, maybe you can try importing this module here:

Quickstart – wikipedia 0.9 documentation