Related Ads Link

5 Surprising Tricks About Python You Need to Implement Today

5 Surprising Tricks About Python You Need to Implement Today

One of my favorite part of programming is finding the easiest way to do a job. While working with python, I decided to do the same. After doing a Lot research have discovered 5 Really Surprising Tricks about python you Need to implement Today.

These Surprising Tricks are somehow very satisfying for many people and now I am sharing with you all.

Here my countdown starts from 1 to 5, Don’t forget to give your feedback on these tricks in a comment. and please comment any python trick or hack you know, which can be helpful for programmers.

Trick #1: List Comprehensions in Python

This trick is really helpful in many cases, consider you have a list

>>> Lio = [1, 2, 3, 4, 5]

Now, you need to add these numbers in a manner that, it will look like:

[2, 4, 6, 8, 10]

Probably you will do the same as many other programmers did:

>>> Lio = [1, 2, 3, 4, 5]
>>> for i in range(len(Lio)):
>>> Lio[i] = Lio[i] * 2

But hear me out:

There is a better way to do this, follow the instructions below

>>> Lio = [ elem * 2 for elem in Lio ]

Cleaner then one you thought right? This is called list Comparison in Python, Now let’s try another trick.

Trick #2: Swapping Numbers Smartly in Python

Swapping Numbers Smartly in Python

Swapping Numbers Smartly in Python

We all have tried swapping in python before, Well here is a smarter way to do this in single line

>>> a=8
>>> b=4
>>> b, a =a, b
>>> a
>>> b

Sounds Better right?

Let’s move to next trick fast:

Trick #3: Printing by Loops Smartly in Python

Yes, you can smartly print large things smartly in python, Suppose you have to Print this one,

“CodingCodingCodingCoding LioLioLioLioLioLioLioLio”

How will you print this? Let me tell you the smarter way to do this:

>>> print “coding”*4+’ ‘+”Lio”*8

Trick #4: Converting List in Python

Can you convert any list to a single list without using any Loop? consider you have a list as following;

a = [[1, 2], [3, 4], [5, 6]]

Output:- [1, 2, 3, 4, 5, 6]

How are you going to do this? Maybe you have this idea;

>>> import itertools
>>> list(itertools.chain.from_iterable(a))
[1, 2, 3, 4, 5, 6]

Trick #5: Reversing String in Python

>>> a = “ilovepython”
>>> print “Reverse is:”,a[::-1]

Reverse is: nohtypevoli


I have provided you 5 python tricks, if you find this somehow intresting, then show your support by sharing this post on facebook, twitter and on other Social media platforms. Also try other post in the blog about python and programming. We have forums also to discuss any programming problems that you must try once. If you have any other Trick that will be helpful for readers, please provide it in the comment box. Keep Programming Cheers.!!









A Programmer by Passion and a Passionate Blogger by Heart <3 . CodingLio was my Dream Idea, I wanted To Create a Medium where, Geeks can actually Learn Something.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: