10 Essential Python Tips And Tricks For Programmers - GeeksforGeeks

Top 4 Advanced Project Ideas to Enhance Your AI Skills
Top 10 Machine Learning Project Ideas That You Can Implement
5 Machine Learning Project Ideas for Beginners
7 Cool Python Project Ideas for Intermediate Developers
10 Interesting Python Cool Tricks
10 Essential Python Tips And Tricks For Programmers
Amazing hacks of Python
Python Input Methods for Competitive Programming
Vulnerability in input() function – Python 2.x
Python | Output using print() function
Important differences between Python 2.x and Python 3.x with examples
Python Keywords
Keywords in Python | Set 2
Namespaces and Scope in Python
Statement, Indentation and Comment in Python
How to assign values to variables in Python and other languages
How to print without newline in Python?
Python end parameter in print()
Python | sep parameter in print()
Python | Output Formatting
Python String format() Method
f-strings in Python
Enumerate() in Python
Iterate over a list in Python
Print lists in Python (4 Different Ways)
Adding new column to existing DataFrame in Pandas
Python map() function
Taking input in Python
How to get column names in Pandas dataframe
Read JSON file using Python
Top 4 Advanced Project Ideas to Enhance Your AI Skills
Top 10 Machine Learning Project Ideas That You Can Implement
5 Machine Learning Project Ideas for Beginners
7 Cool Python Project Ideas for Intermediate Developers
10 Interesting Python Cool Tricks
10 Essential Python Tips And Tricks For Programmers
Amazing hacks of Python
Python Input Methods for Competitive Programming
Vulnerability in input() function – Python 2.x
Python | Output using print() function
Important differences between Python 2.x and Python 3.x with examples
Python Keywords
Keywords in Python | Set 2
Namespaces and Scope in Python
Statement, Indentation and Comment in Python
How to assign values to variables in Python and other languages
How to print without newline in Python?
Python end parameter in print()
Python | sep parameter in print()
Python | Output Formatting
Python String format() Method
f-strings in Python
Enumerate() in Python
Iterate over a list in Python
Print lists in Python (4 Different Ways)
Adding new column to existing DataFrame in Pandas
Python map() function
Taking input in Python
How to get column names in Pandas dataframe
Read JSON file using Python
Difficulty Level :
Easy
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, xprint(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 osimport 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 sysx = 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 Counterdef 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 Know2.30 Essential Python Tips And Tricks For Programmers
RobertoPreste
python-basics
python-list
python-string
Python
Technical Scripter
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Create a Pandas DataFrame from Lists
Reading and Writing to text files in Python
*args and **kwargs in Python
sum() function in Python