Fundamentals 5 min read

7 Little‑Known Python Tricks That Can Supercharge Your Code

Discover seven practical Python tricks—from function attributes and pass placeholders to eval, web server shortcuts, unlimited arguments, zip merging, and list rotation—each illustrated with clear code examples that make everyday coding faster and more elegant.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
7 Little‑Known Python Tricks That Can Supercharge Your Code

Python is beloved for its readable syntax, but many handy tricks are often overlooked; this article shares seven useful techniques.

1. Function attributes – Functions can hold custom attributes just like objects. Example:

# FunctionAttributes
def func():
    pass
func.name = "HaiderImtiaz"
func.age = 22
func.Profession = "Pythondeveloper"
func()
print("Name:", func.name)
print("Age:", func.age)
print("Profession:", func.Profession)

2. Incomplete code placeholders – Use the pass statement as a placeholder in functions, classes, loops, or conditionals.

# Place Holders
def func():
    pass
class data:
    pass
for x in range(5):
    pass
if 2 == 4:
    pass
else:
    pass

3. eval() function – Evaluates a string as a Python expression.

# Eval example 1
x = 5
y = eval('x + 2')
print(y)  # 7
# Eval example 2
x = 2
y = eval('x ** 3')
print(y)  # 8

4. Start a simple HTTP server – Launch a file‑hosting server directly from the interpreter:

# Starting a Web server
python -m http.server 5000

Running this command displays the address http://0.0.0.0:5000/ where the server is listening.

5. Unlimited arguments – Use *args to accept any number of positional arguments.

# Unlimited Arguments
def func(*arg):
    print("Argument Passed:", len(arg))
func(1, 2, 4, 5, 6, 7)  # Argument Passed: 6

6. zip() method – Combine two iterables into tuples, useful for parallel iteration.

# Zip method
list1 = ["Python", "JavaScript", "C#", "Dart"]
list2 = ["Machine Learning", "Web Developer", "Software", "App Dev"]
for x, y in zip(list1, list2):
    print(x, y)
# Output:
# Python Machine Learning
# JavaScript Web Developer
# C# Software
# Dart App Dev

7. Rotating a list – Quickly rotate elements left or right.

# Rotating list
lst = [10, 20, 30, 40, 50]
# rotate left
lst = lst[1:] + [lst[0]]
print(lst)  # [20, 30, 40, 50, 10]
# rotate right
lst = [lst[-1]] + lst[:-1]
print(lst)  # [50, 10, 20, 30, 40]

These tricks aim to make your Python workflow smoother and more enjoyable; feel free to share them with fellow developers.

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.

functionsevalzipcode trickslist manipulation
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.