Ad Code

Responsive Advertisement

Outsmart your Friend with 7 Clever Python Tricks in 2020

Clever Python Tips and Tricks 😈

Clever Python Tips and Tricks to Outsmart and make your program easy in 2020. Learn how to use them with our Free Tutorials.


Top tricks that you don't know in Python Programming which you can show your friends to Show off.
What you need here is only to perform these tricks on your System side-by-side.
Without wasting time let's get started with the tricks in Python.

1. A Funny Text  😃
This is a simple two word text which will print the poem written by Tim Peters.
Let's check-:
Type -: import this 
import this
And see the Magic-:


The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!


2. A web Comic [xkcd] 👨‍💻

This is also a simple two word text which will open the xkcd webcomic of Romance, Math and Language.

The best part I like of it was that, that it work offline also.

Let's check-:

Type -: import antigravity

import antigravity

Output-:

xkcd Webcomic, python, import antigravity, progcode


3. Time Saver Code ⏳

To swap any the value of the two variables you write a code like this-
A = 10
B = 20
Swap = A
A = B
B = Swap
print (" A = " , A , " B = " ,B)

Output-:
A = 20, B = 10
But instead of writing this type of time consuming code you can use it as-
A = 10
B = 20
A, B = B, A
print (" A = " , A , " B = " ,B)
Output-:
A = 20, B = 10
In both the case ( a ) and ( b ) the Output was same.

4. Get file Path 


You can get the File path for the imported module using the following statement. 

For example if we want to know the file path for the module os so we use it as.

import os 
print (os ) 

when you run this program you will get the output as .

<module 'os' from 'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\os.py'>

5. Returning Multiple values to Function

Here program will return multiple values to the Function .

For Example -: 

def fun():
   return 1,2,3,4
a,b,c,d= fun()
print(a)
print(b)
print(c)
print(d)


6. Get the Size of A Value

To get the Memory usage of any value you have to use the following statement.

  • First you have to import a module named sys
  • Then you have to use a function name - getsizeof()


For example-:

n = 10
print(sys.getsizeof(n))

Output  = 14

Same as -: 

s = " Hello " 
print(sys.getsizeof(s))

Output = 30

it means that size of n and are 14 and 30 respectively.

7. Reverse the List


To reverse the list you have to use a simple statement.

For example-:

If you want to reverse the list x = [ 1,2,3,4,5,6,7]

you have to write-:

x = [ 1,2,3,4,5,6,7 ]
print( x [ : : -] )

Output =  [7, 6, 5, 4, 3, 2, 1]


Hope you like these tricks.


::: Comment Below For the Query and Feedback  :::

Post a Comment

0 Comments