Fundamentals 8 min read

13 Lesser‑Known Python Features You Should Know

This article introduces thirteen hidden or underused Python features—including list stepping, the find method, iterators, doctest, yield, dictionary get, for/else loops, named string formatting, recursion limits, conditional expressions, argument unpacking, the __hello__ module, and multi‑line strings—each illustrated with code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
13 Lesser‑Known Python Features You Should Know

Python is a top programming language with many hidden capabilities. This article shares thirteen lesser‑known features that can make your code more concise and powerful.

1. List Stepping The step parameter can slice a list and also reverse it.

<code># List Stepping
data = [10, 20, 30, 40, 50]
print(data[::2])   # [10, 30, 50]
print(data[::3])   # [10, 40]
print(data[::-1])  # [50, 40, 30, 20, 10]
</code>

2. find() Method Returns the index of a substring within a string.

<code># Find method
x = "Python"
y = "Hello From Python"
print(x.find("Python"))   # 0
print(y.find("From"))     # 6
print(y.find("From Python")) # 6
</code>

3. iter() Function Creates an iterator from an iterable, allowing manual traversal with next() .

<code># iter()
values = [1, 3, 4, 6]
values = iter(values)
print(next(values))  # 1
print(next(values))  # 3
print(next(values))  # 4
print(next(values))  # 6
</code>

4. Doctest Enables embedded tests in docstrings and reports results.

<code># Doctest
from doctest import testmod

def Mul(x, y) -> int:
    """
    >>> Mul(4, 5)
    20
    >>> Mul(19, 20)
    380
    """
    return x * y

testmod(name='Mul', verbose=True)
</code>

5. yield Statement Works like return but pauses function execution, producing a generator.

<code># Yield statement
def func():
    print(1)
    yield 1
    print(2)
    yield 2
    print(3)
    yield 3

for _ in func():
    pass  # outputs 1, 2, 3
</code>

6. Handling Missing Dictionary Keys Use dict.get() to avoid KeyError .

<code># Handling missing keys
dict_1 = {1: "x", 2: "y"}
# print(dict_1[3])  # raises KeyError
print(dict_1.get(3))  # None
</code>

7. for/else and while/else The else block runs only when the loop finishes without break .

<code># for/else example
for x in range(5):
    print(x)
else:
    print("Loop Completed")

# while/else with break (else not executed)
i = 0
while i < 5:
    break
else:
    print("Loop Completed")
</code>

8. Named String Formatting Use str.format() or f‑strings to inject values.

<code># Named formatting
a = "Python"
b = "Job"
string = "I looking for a {} Programming {}".format(a, b)
print(string)  # I looking for a Python Programming Job

# f‑string
string = f"I looking for a {a} Programming {b}"
print(string)
</code>

9. Setting Recursion Limit Adjust Python's maximum recursion depth.

<code># Set recursion limit
import sys
sys.setrecursionlimit(2000)
print(sys.getrecursionlimit())  # 2000
</code>

10. Conditional Expression Ternary operator for inline conditional assignment.

<code># Conditional expression
x = 5 if 2 > 4 else 2  # 2
print(x)
y = 10 if 32 > 41 else 24  # 24
print(y)
</code>

11. Argument Unpacking Use * and ** to expand iterables and dictionaries into function arguments.

<code># Argument unpacking
def func(x, y):
    print(x, y)
list_1 = [100, 200]
dict_1 = {'x': 300, 'y': 400}
func(*list_1)   # 100 200
func(**dict_1) # 300 400
</code>

12. Hello World Shortcut Importing the special __hello__ module prints a greeting.

<code>import __hello__
</code>

13. Multi‑Line Strings without Triple Quotes Use backslashes to continue a string across lines.

<code># Multi‑line string
str1 = "你是否正在寻找免费的Python学习材料" \
       "欢迎来到 " \
       "公众号 快学Python"
print(str1)
</code>

These examples demonstrate how Python’s less‑frequently used features can simplify common tasks and enhance code readability.

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