Essential Python Tips And Tricks For Programmers Last Updated : 22 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Python is one of the most preferred languages out there. Its brevity and high readability makes it so popular among all programmers. So here are few of the tips and tricks you can use to bring up your Python programming game. 1. In-Place Swapping Of Two Numbers. Python3 x, y = 10, 20 print(x, y) x, y = y, x print(x, y) Output:10 20 20 10 2. Reversing a string in Python Python3 a = "GeeksForGeeks" print("Reverse is", a[::-1]) Output:Reverse is skeeGroFskeeG 3. Create a single string from all the elements in list Python3 a = ["Geeks", "For", "Geeks"] print(" ".join(a)) Output:Geeks For Geeks 4. Chaining Of Comparison Operators. Python3 n = 10 result = 1 < n < 20 print(result) result = 1 > n <= 9 print(result) Output:True False 4. Print The File Path Of Imported Modules. Python3 import os import socket print(os) print(socket) Output:<module 'os' from '/usr/lib/python3.5/os.py'> <module 'socket' from '/usr/lib/python3.5/socket.py'> 5. Use Of Enums In Python. Python3 class MyName: Geeks, For, Geeks = range(3) print(MyName.Geeks) print(MyName.For) print(MyName.Geeks) Output:2 1 2 6. Return Multiple Values From Functions. Python3 def x(): return 1, 2, 3, 4 a, b, c, d = x() print(a, b, c, d) Output:1 2 3 4 7. Find The Most Frequent Value In A List. Python3 test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4] print(max(set(test), key = test.count)) Output:4 8. Check The Memory Usage Of An Object. Python3 import sys x = 1 print(sys.getsizeof(x)) Output:28 9. Print string N times. Python3 n = 2 a = "GeeksforGeeks" print(a * n) Output:GeeksforGeeksGeeksforGeeks 10. Checking if two words are anagrams Python3 from collections import Counter def is_anagram(str1, str2): return Counter(str1) == Counter(str2) # or without having to import anything def is_anagram(str1, str2): return sorted(str1) == sorted(str2) print(is_anagram('geek', 'eegk')) print(is_anagram('geek', 'peek')) Output:True False 11. Sorting the dictionary according to key and value Python3 x={1:3,4:2,5:1,2:9} #sorting the dictionary according to value print(sorted(x.items(),key=lambda a:a[1])) #sorting the dictionary according to key print(sorted(x.items(),key=lambda a:a[0])) Comment More infoAdvertise with us Next Article Essential Python Tips And Tricks For Programmers R RahulBisht07 Follow Improve Article Tags : Misc Technical Scripter Python python-list python-string python-basics +2 More Practice Tags : Miscpythonpython-list Similar Reads 12 Tips to Become a Better Programmer Becoming a good programmer is something that every computer science student wants. Students try to learn coding from the start of their college in order to become good programmers. You just have to start from the basics and then learn slowly, as codes are the building blocks for any program; therefo 6 min read Geek Week â Celebrate The Biggest Programming Festival With GeeksforGeeks Every programmer has a dream they want to fulfill. Now that dream for you could be working at the top product-based companies in the world like Google, Facebook, Microsoft, etc. or it could even be something as big as transforming the world one invention at a time!!! And all those dreams (and more!) 12 min read 15 Tips to Improve Logic Building Skills in Programming âIn some ways, programming is like a painting. You start with a blank canvas and certain basic raw materials. You use a combination of science, art, and craft to determine what to do with them.â - Andrew Hunt Yes, programming in itself is a very beautiful art. Sometimes we may face some problems whi 8 min read A Step by Step Guide for Placement Preparation | Set 1 Campus placements season is beginning at almost all the colleges and each one of us wants to prepare to do our best. A lot of students have been asking on different forums "How to use GeeksforGeeks for placement preparation"? In this article, a step-by-step guide for placement preparation is discuss 4 min read Top 10 Python Developer Skills That You Must Know in 2024 The tech industry is a whirlwind of innovation, and Python stands tall as one of the most in-demand programming languages fueling this progress. According to a recent Stack Overflow survey, Python reigns supreme as the most preferred language among developers. This translates to a goldmine of opport 7 min read Like