Fundamentals 8 min read

13 Amazing Python Features You Might Not Know

This article introduces thirteen lesser‑known Python features—including list stepping, the find method, iterators, doctest, yield statements, dictionary get, for/else and while/else loops, named string formatting, recursion limits, conditional expressions, argument unpacking, the special __hello__ import, and multiline strings—each illustrated with clear code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
13 Amazing Python Features You Might Not Know

Python is a top programming language with many hidden features that many developers never use. This article shares thirteen such Python features.

1. List Stepping – Use the step slice parameter to skip elements or reverse a list.

<code># 列表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 – Locate the start index of a substring.

<code># 查找方法
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 – Create an iterator from an iterable without a loop.

<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 – Write tests inside docstrings and run them with doctest.testmod .

<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 – Similar to return but produces a generator that can be resumed.

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

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

6. Handling Missing Dictionary Keys – Use dict.get() instead of direct indexing to avoid KeyError .

<code># 处理字典中的缺失值
dict_1 = {1: "x", 2: "y"}
print(dict_1.get(3))  # None
</code>

7. For/Else and While/Else – else runs after a loop completes without break .

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

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

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

<code># 命名格式化字符串
a = "Python"
b = "Job"
string = "I looking for a {} Programming {}".format(a, b)
print(string)  # I looking for a Python Programming Job

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

9. Setting Recursion Limit – Adjust Python's recursion depth with sys.setrecursionlimit() .

<code># 设置递归限制
import sys
sys.setrecursionlimit(2000)
print(sys.getrecursionlimit())  # 2000
</code>

10. Conditional (Ternary) Expression – Assign values based on a condition in a single line.

<code># 条件参数
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 unpack iterables and dictionaries into function arguments.

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

12. Hello World Shortcut – Importing __hello__ prints a friendly message.

<code># Hello World!
import __hello__
</code>

13. Multiline Strings Without Triple Quotes – Concatenate strings across lines using backslashes.

<code># 多行字符串
str1 = "你是否正在寻找免费的Python学习材料" \
       "欢迎来到 " \
       "公众号 关于数据分析与可视化"
print(str1)
</code>

The article concludes with a call‑to‑action offering free Python learning resources via a QR code.

PythonProgrammingcode examplesTutorialAdvanced Features
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.