Fundamentals 7 min read

11 Pythonic Tricks to Boost Your Python Skills

This article introduces eleven practical Pythonic techniques—including destructuring assignment, zip aggregation, lambda usage, underscore placeholders, list comprehensions, dummy functions, regex handling, map/reduce, whitespace trimming, shallow copying, and interpreter shortcuts—each illustrated with clear code examples to help developers write more elegant and efficient Python code.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
11 Pythonic Tricks to Boost Your Python Skills

Python is an elegant language, but mastering its idioms takes practice. This article presents eleven Pythonic techniques that make code more concise and readable.

1. Destructuring assignment Python allows unpacking iterables with a starred expression to capture the middle elements.

a, *mid, b = [1, 2, 3, 4, 5, 6]
print(a, mid, b)  # 1 [2, 3, 4, 5] 6

2. Using the zip function to aggregate items The built‑in zip combines elements from multiple iterables into tuples.

id = [1, 2, 3, 4]
leaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Yang Zhou']
sex = ['male', 'male', 'male', 'male']
record = zip(id, leaders, sex)
print(list(record))  # [(1, 'Elon Mask', 'male'), (2, 'Tim Cook', 'male'), (3, 'Bill Gates', 'male'), (4, 'Yang Zhou', 'male')]

3. Correct use of lambda functions A one‑liner lambda can filter a list, for example to print all odd numbers.

print(list(filter(lambda x: x % 2 == 1, numbers)))

4. Ignoring values with an underscore Unused variables can be assigned to _ to indicate they are irrelevant.

L = [1, 3, 5, 7]
a, _, b, _ = L
print(a, b)  # 1 5

5. Power of list comprehensions List comprehensions can embed conditional logic in a single expression.

Genius = ["Jerry", "Jack", "tom", "yang"]
L1 = [name if name.startswith('y') else 'Not Genius' for name in Genius]
print(L1)  # ['Not Genius', 'Not Genius', 'Not Genius', 'yang']

6. Placeholder for unfinished functions Use pass or an ellipsis ( ... ) as a temporary body.

def my_func():
pass

or

def my_func():
...

7. Text processing with regular expressions The re module provides full regex support.

import re
txt = "Yang is so handsome!!!"
x = re.search("Elon", txt)
print(x)  # None

8. map and reduce functions map applies a function to each element; reduce aggregates results.

names = ['yAnG', 'MASk', 'thoMas', 'LISA']
names = map(str.capitalize, names)
print(list(names))  # ['Yang', 'Mask', 'Thomas', 'Lisa']
from functools import reduce
city = ['L','o','n','d','o','n',2,0,2,0]
city_to_str = reduce(lambda x, y: str(x) + str(y), city)
print(city_to_str)  # London2020

9. Removing unnecessary whitespace Combine split() and join() to normalize spaces.

quote = "   Yang   is a full   stack hacker."
new_quote = ' '.join(quote.split())
print(new_quote)  # Yang is a full stack hacker.

10. Simple shallow copy of a list Slice notation creates a shallow copy.

a = [1, 2, 3, 4, 5, 6]
b = a[:]
b[0] = 100
print(b)  # [100, 2, 3, 4, 5, 6]
print(a)  # [1, 2, 3, 4, 5, 6]

11. Inspecting the last expression value in the interpreter The underscore variable holds the result of the most recent expression.

5 + 6
# 11
_
# 11

These eleven tips demonstrate how Python’s syntactic sugar and built‑in functions can be leveraged to write cleaner, more efficient code.

PythonProgrammingFundamentalsCodetipspythonic
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

login 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.