Python is an easy to learn programming language, but do not underestimate it by the simplicity of the syntax. Python is used for complex mathematics, game designing, server side development and machine learning.
Here are some really cool and easy python tricks, that every python programmer must know. Let’s go through these interesting python tricks one by one.
- Reverse of a string:
This is a one liner code which uses the slicing concept and returns reverse of the string.
2. Transpose a Matrix:
A matrix stricture can be created using a two dimensional list. and to transpose it you can use the zip function which will exchange the rows and columns.
3. Swap Two Variables:
In python you do not have to specify the data type while assigning the value to the variable, thus you can assign the value to multiple variables at a time like:
name, age= “Iron Man”, 48
so using this concept you can swap the value of variables in just one line.
4. Zen of Python:
Zen of python was written by Tim Peters, it is a 19 line poem which are actually guiding priciples for python, you can find the poem by just writing in the IDLE:
5. Altering Tuple:
Tuples in python are said to be unchangeable, but what if we really want to change the values then here is a simple trick where you can convert the tuple into list, change the values and convert back into tuple.
6. Printing Emoji:
Don’t lie you will really want to try this and have a little fun in between your serious coding.
You can print emojis by passing the CLDR value or the short name or the emoji. you can find the list of CLDR names here:
just make sure that you separate the names by underscore.
7. Location Details:
Using geocoder module you can get the location details like latitude and longitude in python for any given location. it uses osm function which stands for “Open Street Map”.
First you will have to install the geocoder by writing:
!pip install geocoder and then the following code will work.
List comprehension in python is a simple and interesting way to minimize the length of your code. In just one line you can create variety of lists using for loop. it eliminates the creation of blank list, code indentation and the append function.
General Method of list creation:
for i in range(1,21):
list comprehension works as following:
It is read as : append i in the even_numbers list for 1 to 21 only if i is divisible by 2.
9. Ask Questions to your code:
Yes you read it right! Your python code can actually help you to get programming solutions by using howdoi module.
Actually it just takes answers from the top answers from stack overflow, so you might not get reliable information every time.
10. Zip Function:
Last but not the least, this one could be very useful as the zip function can take variety of data structures like tuple, list, dictionary, numpy array and zip then together.
Just priting the zip() will just return you the memory address so you can again convert the final result into any of the data structures.
That’s all Folks! I hope these tips will be useful for you. You can just show off your coding skills by using one of these Python tricks.
Here is the link to my colab notebook, where you can experiment more with the code. just make a copy of the notebook and you are ready to start.