Fundamentals 8 min read

13 Must‑Know Python Tricks to Boost Your Coding Efficiency

This article presents thirteen practical Python techniques—from passing unlimited arguments and smart list iteration to using lambda functions and the divmod operator—each illustrated with concise code snippets that can streamline development, improve readability, and enhance overall programming efficiency.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
13 Must‑Know Python Tricks to Boost Your Coding Efficiency

We all know Python is one of the most popular programming languages and a favorite among developers. In this article, I share thirteen practical Python tips that can help you in daily development, increase work efficiency, and save development time.

1. Pass Arguments

This technique lets you pass unlimited arguments to a function without declaring them.

# Pass Arguments
def Test_func(*numbers):
    mul = 1
    for n in numbers:
        mul = mul * n
    print(mul)
Test_func(1, 3, 4)  # 12

2. Smart List Iteration

Instead of a manual loop, you can use a list comprehension to apply a function to each element.

# Smart way to iterate
mylst = [11, 22, 33, 44, 55]
new = [x * 2 for x in mylst]
print(new)  # [22, 44, 66, 88, 110]

3. Shorter Library Names

Use aliasing to shorten long library names.

import pandas as pd
import numpy as np
import tkinter as tk
import time as t

4. Pyforest

Pyforest allows you to use popular libraries without explicit imports.

# pip install pyforest
import pyforest
a = np.array([[1, 2], [3, 5]])

5. Multiple Input

Read several values from a single line.

# Take Multiple Input
data = input("Enter num with Spaces: ").split()
print(data)
# Input: 123
# Output: ['1', '2', '3']

6. Trim Data

Remove unwanted whitespace or characters from strings.

# Trim Data
data = "    Hello"
print(data.strip(" "))  # Hello
data = "    Hello Pythoneer"
print(data.lstrip(" "))  # Hello Pythoneer
data = "Hello Coder$$$"
print(data.rstrip("$"))  # Hello Coder

7. Runtime Error Handling

Use try/except to catch runtime errors.

# Handling Runtime Error
x = 6
try:
    if 5 > 3:
        x = x * y
    else:
        x = x + y
except:
    print("Y is not defined")
# Output: Y is not defined

8. Lambda Functions

Write small anonymous functions in one line.

# One liner functions
# example 1
mul = lambda x: x * 2
print(mul(3))  # 6
# example 2
mul = lambda x, y: x * y * 2
print(mul(1, 2))  # 4

9. Yield

Yield returns values from a generator without losing its state.

# Yield
def func():
    yield 1
    yield 2
    yield 3
    yield 4
for x in func():
    print(x)
# Output:
# 1
# 2
# 3
# 4

10. Local and Global Variables

Demonstrates declaring local and global variables inside functions.

# Local and Global Variables
# Local variables
a = 5
b = 6
# Global
def func():
    global a
    a = 6 * 2
    global a
    a = 0

11. Smart Dictionary Access

Use dict.get to avoid KeyError when accessing missing keys.

# Dictionary in Smart way
mydict = {"a": 10, "b": 20, "c": 30}
# Best way
mydict.get("d")  # None
# default way
mydict["d"]  # KeyError

12. Smart Data Swap

Swap two values without a temporary variable.

d1 = 55
d2 = 66
d2, d1 = d1, d2
print(d1, d2)  # 66 55

13. Division 2.0

Use divmod to obtain quotient and remainder simultaneously.

# Division
x = 5
y = 3
div = divmod(x, y)
print(div)  # (1, 2) -> (Quotient, Remainder)

These thirteen Python tricks can help improve your workflow and save development time.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

productivityScripting
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.