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.
x, y = 10, 20print(x, y) x, y = y, x print(x, y) |
10 20 20 10
2. Reversing a string in Python
a = "GeeksForGeeks"print("Reverse is", a[::-1]) |
Reverse is skeeGroFskeeG
3. Create a single string from all the elements in list
a = ["Geeks", "For", "Geeks"] print(" ".join(a)) |
Geeks For Geeks
4. Chaining Of Comparison Operators.
n = 10result = 1 < n < 20print(result) result = 1 > n <= 9print(result) |
True False
4. Print The File Path Of Imported Modules.
import os import socket print(os) print(socket) |
<module 'os' from '/usr/lib/python3.5/os.py'> <module 'socket' from '/usr/lib/python3.5/socket.py'>
5. Use Of Enums In Python.
class MyName: Geeks, For, Geeks = range(3) print(MyName.Geeks) print(MyName.For) print(MyName.Geeks) |
2 1 2
6. Return Multiple Values From Functions.
def x(): return 1, 2, 3, 4a, b, c, d = x() print(a, b, c, d) |
1 2 3 4
7. Find The Most Frequent Value In A List.
test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4] print(max(set(test), key = test.count)) |
4
8. Check The Memory Usage Of An Object.
import sys x = 1print(sys.getsizeof(x)) |
28
9. Print string N times.
n = 2a = "GeeksforGeeks"print(a * n) |
GeeksforGeeksGeeksforGeeks
10. Checking if two words are anagrams
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')) |
True False
References:
1.10 Neat Python Tricks Beginners Should Know
2.30 Essential Python Tips And Tricks For Programmers
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- Jupyter notebook Tips and Tricks
- 5 Tips and Tricks To Crack The Hackathon
- Python Tricks for Competitive Coding
- Selenium Python Tricks
- 10 Interesting Python Cool Tricks
- Mathematics Tricks For Competitive Programming In Python 3
- Tricks in Windows 10 | Set -1
- Optimization Tips for Python Code
- Beginner Tips for Learning Python
- Tips to reduce Python object size
- Some time-saving tips for Linux Users
- Tips for restarting career after a break
- Few Tips for Fast & Productive Work on a Linux Terminal
- Tips For an Indie Game Developer
- Tips for Writing a Research Paper
- Top 3 tips an interviewee must have in mind
- Important differences between Python 2.x and Python 3.x with examples
- Creating and updating PowerPoint Presentations in Python using python - pptx
- Loops and Control Statements (continue, break and pass) in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : RobertoPreste

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
