13 Essential Python Tricks to Boost Your Development Efficiency
This article presents thirteen practical Python tips—including argument packing, list comprehensions, alias imports, on‑the‑fly library loading, multi‑input handling, variable scope, safe dictionary access, data trimming, generators, lambda one‑liners, division with remainder, and tuple swapping—to help developers write cleaner, faster code and improve productivity.
Pass arguments without declaring
This trick lets you pass an unlimited number of arguments to a function without explicitly declaring them.
def Test_func(*numbers):
mul = 1
for n in numbers:
mul = mul * n
print(mul)
Test_func(1, 3, 4) # 12Smart list iteration
Use a list comprehension to transform a list in a single line.
mylst = [11, 22, 33, 44, 55]
new = [x * 2 for x in mylst]
print(new) # [22, 44, 66, 88, 110]Shorter library names
Alias frequently used libraries to reduce typing.
import pandas as pd
import numpy as np
import tkinter as tk
import time as tPyforest – lazy imports
Install pyforest to use popular libraries without explicit imports.
pip install pyforest
import pyforest
a = np.array([[1, 2], [3, 5]])Take multiple input in one line
Read space‑separated values and split them automatically.
data = input("Enter num with Spaces: ").split()
print(data) # ['1', '2', '3']Local and global variables
Demonstrate the difference between local and global scope.
a = 5
b = 6
def func():
global a
a = 6 * 2
global a
a = 0Smart dictionary access
Use dict.get() to avoid KeyError when a key is missing.
mydict = {"a": 10, "b": 20, "c": 30}
mydict.get(mydict["d"]) # None
# mydict["d"] would raise KeyErrorTrim raw data
Remove unwanted spaces or characters from strings.
data = " Hello"
print(data.strip(" ")) # Hello
data = " Hello Pythoneer"
print(data.lstrip(" ")) # Hello Pythoneer
data = "Hello Coder$$$"
print(data.rstrip("$")) # Hello CoderYield – generator magic
Use yield to create a generator that retains state between calls.
def func():
yield 1
yield 2
yield 3
yield 4
for x in func():
print(x)One‑liner lambda functions
Create small anonymous functions in a single line.
mul = lambda x: x * 2
print(mul(3)) # 6
mul = lambda x, y: x * y * 2
print(mul(1, 2)) # 4Division with quotient and remainder
Use divmod() to obtain both quotient and remainder.
x = 5
y = 3
div = divmod(x, y)
print(div) # (1, 2)Swap values without a temporary variable
Python’s tuple unpacking makes swapping trivial.
d1 = 55
d2 = 66
d2, d1 = d1, d2
print(d1, d2) # 66 55Signed-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.
