Skip to main content


Top 11 Essential Python Tips and Tricks


Some python tips and tricks that I know might also help you with your coding skills. Its not that complicated all you need is practice.



2) In-Place Swapping Of Two Or More Numbers



1
2
3
4
5
6
7
>>> #_2 in-place swapping of two or more numbers
>>> x, y, z = 10, 20, 30
>>> print(x,y,z)
10 20 30
>>> x, y, z = z, x, y
>>> print (x, y, z)
30 10 20


3) Reversing String / Strings In Python



1
2
3
4
5
6
7
#_3 reversing a string in python
>>> a = "I love to code in Python"
>>> print("Reverse is:-",a[::-1])
Reverse is:- nohtyP ni edoc ot evol I
>>> b = "IMPythonProgrammerThePySnakeBlogger"
>>> print("Reverse is:-",b[::-1])
Reverse is:- reggolBekanSyPehTremmargorPnohtyPMI


4) Create A Single String From All The Elements In List



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> #_4 creating a single string from all the elements in a list
>>> c = ["Python", "Is", "Fun"]
>>> print(" ".join(c))
Python Is Fun
>>> print("-".join(c))
Python-Is-Fun
>>> print("\n".join(c))
Python
Is
Fun


5) Chaining Of Comparison Operators



1
2
3
4
5
6
7
8
>>> #_5 chaining of comparison operators
>>> num = 10
>>> output = 1 < num < 20
>>> print(output)
True
>>> output = 1 > num <= 7
>>> print (output)
False


6) Find The Most Frequent Value In A List



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
>>> #_6 find the most frequent value in a list
>>> mylist = [1,2,3,4,2,2,3,1,4,4,4]
>>> print(max(set(mylist),key = mylist.count))
4
>>> #lets break it up to understand
>>> mylist = [1,2,3,4,2,2,3,1,4,4,4]
>>> a = set(mylist)
>>> print(a)
{1, 2, 3, 4}
>>> key = mylist.count
>>> print(key)
<built-in method count of list object at 0x03A8FE40>
>>> print(max(a,key=mylist.count))
4


7) Check The Memory Usage Of An Object



1
2
3
4
5
>>> #_7 check the memory usage of an object
>>> import sys
>>> x = 1
>>> print(sys.getsizeof(x))
14



9) Return Multiple Values From Functions



1
2
3
4
5
6
7
>>> #_9 return multiple values from functions
>>> def x():
 return 1,2,3,4

>>> a,b,c,d = x()
>>> print (a,b,c,d)
1 2 3 4


10)Use Of Enums In Python


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
>>> #_10 use of enums in python
>>> class myName:
 hello, world, of, python = range(4)

 
>>> print(myName.hello)
0
>>> print(myName.world)
1
>>> print(myName.of)
2
>>> print(myName.python)
3
>>> print(myName.hello,myName.world,myName.of,myName.python)
0 1 2 3


11) Checking If Two Words Are Anagrams



1
2
3
4
5
6
7
8
9
>>> #_11 checking if two words are anagrams
>>> from collections import Counter
>>> def is_anagram(str1, str2):
 return Counter(str1) == Counter(str2)

>>> print(is_anagram("python","nohtyp"))
True
>>> print(is_anagram("python","jython"))
False

Hope you like the tutorial.

Python is simple and fun to play with and yet very powerful. 🔥


🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍

Popular Posts

Python underline string, Python underline text, Underline python print

Python pip - Installing modules from IDLE (Pyton GUI) for python 3.7

Top 40 Python - String Processing in Python | Working with String in Python

Python Program - When was I born? / Date of Birth / MY BIRTHDAY (using Python3+)





Subscribe to our Channel


Follow us on Facebook Page

Join our python facebook groups



Join us on Telegram