17 Must‑Know Python Tricks to Boost Your Coding Efficiency
This article presents 17 practical Python tips—from printing strings multiple times and returning multiple values to fast string reversal, duplicate removal, advanced unpacking, and modifying recursion limits—each illustrated with concise code examples for immediate use.
"Python is a truly wonderful language. When someone proposes a good idea, it takes about one minute and five lines of code to write something that almost does what you want. Then you can expand the script to 300 lines in an hour, and it still almost meets your needs." – Jack Jensen
Below are 17 useful Python development tricks.
1. Print a string N times
Use multiplication to repeat a string instead of a loop.
string = "Python "
ntimes = string * 3
print(ntimes) # Python Python Python2. Return multiple values from a function
def Mulvalues():
return 1, 2, 3
a, b, c = Mulvalues()
print(a, b, c) # 1 2 33. Get the file path of an imported module
import os
import json
import tkinter
print(os)
print(tkinter)
print(json)4. Fast method to reverse a string
string1 = "Coder"
string2 = "Algorithm"
print(string1[::-1]) # redoC
print(string2[::-1]) # mhtiroglA5. Multiple assignment
a, b, c = 1, 2, 3
print(a) # 1
print(b) # 2
print(c) # 36. Quickly remove duplicate items
lst1 = [1, 3, 3, 4, 5, 1]
lst2 = ["A", "A", "B", "C", "D", "D"]
newlst1 = list(set(lst1))
newlst2 = list(set(lst2))
print(newlst1) # [1, 3, 4, 5]
print(newlst2) # ['C', 'D', 'A', 'B']7. String formatting
name = "Haider"
skill = "Python"
# method 1
text = "My name is {n} and I'm a {s} Expert".format(**{"n": name, "s": skill})
print(text)
# method 2
text = "My name is {} and I'm a {} Expert".format(name, skill)
print(text)
# f‑string
text = f"My name is {name} and I'm a {skill} Expert"
print(text)8. Check an object's memory usage
import sys
val = 500
print(sys.getsizeof(val)) # 289. Initialize empty containers
my_list = list()
my_dict = dict()
my_tuple = tuple()
my_set = set()
print(my_list) # []
print(my_dict) # {}10. Reverse a list
my_list1 = [1, 2, 3, 4, 5, 100]
my_list2 = ["A", "B", "C"]
print(my_list1[::-1]) # [100, 5, 4, 3, 2, 1]
print(my_list2[::-1]) # ['C', 'B', 'A']11. Reverse a dictionary
dict = {'x': 1, 'y': 2, 'z': 3}
new_dict = {value: key for key, value in dict.items()}
print(new_dict) # {1: 'x', 2: 'y', 3: 'z'}12. Advanced multiple assignment
a, *b, c, d = 3, 4, 5, 6, 7
print(a, b, c, d) # 3 [4, 5] 6 713. Fast way to join strings
lst = ["I'm", "a", "Programmer"]
text = " ".join(lst)
print(text) # I'm a Programmer14. Merge two dictionaries
a = {"a": 1, "b": 2}
b = {"c": 3, "d": 4}
c = {**a, **b}
print(c) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}15. Change recursion limit
import sys
current_limit = sys.getrecursionlimit()
print(current_limit) # 1000
sys.setrecursionlimit(5000)
print(sys.getrecursionlimit()) # 500016. Multi‑prefix search
string1 = "www.medium.com"
if string1.startswith(("www", "http")):
print("True")
if string1.endswith(("com", "co.uk")):
print("True")17. IF statement tip
a = [1, 2, 3]
# slower way
if a[0] == 1 or a[1] == 1 or a[2] == 1:
print("Number is present in the list")
# faster way
if 1 in a:
print("Number is present in the list")Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
