Fundamentals 13 min read

Advanced Python Concepts: Exception Handling, Collections, itertools, Lambdas, Decorators, Generators, Threads, Dunder Methods, Logging, and Context Managers

This article introduces ten advanced Python topics—including exception handling, the collections and itertools modules, lambda functions, decorators, generators, threading, dunder methods, logging, and context managers—providing explanations, code examples, and practical tips to help developers deepen their programming skills and prepare for interviews.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Advanced Python Concepts: Exception Handling, Collections, itertools, Lambdas, Decorators, Generators, Threads, Dunder Methods, Logging, and Context Managers

In this tutorial we share several advanced Python concepts that can improve your coding proficiency and interview performance.

1. Exception Handling

Exception handling uses try and except blocks to catch errors such as ZeroDivisionError , ImportError , and IndexError . The typical syntax includes optional else and finally clauses.

<code>try:
    pass  # code that may raise an exception
except ValueError:
    pass
except ZeroDivisionError:
    pass
else:
    pass
finally:
    pass
</code>

2. collections Module

The collections module provides enhanced container data structures. Five commonly used types are demonstrated:

2.1 Counter

Counts occurrences of items in an iterable.

<code>from collections import Counter
data = [1,1,1,2,3,4,3,3,5,6,7,7,1]
count = Counter(data)
print(count)  # Counter({1: 4, 3: 4, 2: 1, 4: 1, 5: 1, 6: 1, 7: 2})
print(count.most_common(3))  # [('1', 4), ('3', 4), ('2', 1)]
for i in count.elements():
    print(i)
</code>

2.2 namedtuple

Creates tuple subclasses with named fields.

<code>from collections import namedtuple
User = namedtuple('User', ['name', 'sex', 'age'])
user = User(name='Runoob', sex='male', age=12)
print(user)
user = User._make(['RunoX', 'Male', 13])
print(user)
print(user.name, user.sex, user.age)
user = user._replace(age=22)
print(user)
print(user._asdict())
</code>

2.3 OrderedDict

Preserves insertion order (now also the default for dict ).

<code>from collections import OrderedDict
dictt = OrderedDict()
dictt['a'] = 5
dictt['d'] = 2
dictt['c'] = 1
dictt['b'] = 3
print(dictt)  # OrderedDict([('a', 5), ('d', 2), ('c', 1), ('b', 3)])
</code>

2.4 defaultdict

Provides a default value for missing keys.

<code>from collections import defaultdict
dictt = defaultdict(int)
dictt['a'] = 2
print(dictt['a'])  # 2
print(dictt['b'])  # 0
print({
    'a': 1,
    'b': 2
}.get('c', 0))  # 0
</code>

2.5 deque

A double‑ended queue allowing fast appends/pops from both ends.

<code>from collections import deque
queue = deque(['a', 'b', 'c'])
queue.append('d')
print(queue)  # deque(['a', 'b', 'c', 'd'])
queue.appendleft('e')
print(queue)  # deque(['e', 'a', 'b', 'c', 'd'])
queue.pop()
print(queue)  # deque(['e', 'a', 'b', 'c'])
queue.popleft()
print(queue)  # deque(['a', 'b', 'c'])
</code>

3. itertools Module

The itertools library offers combinatorial utilities such as product , permutations , combinations , combinations_with_replacement , accumulate , and groupby .

<code>from itertools import product, permutations, combinations, combinations_with_replacement, accumulate, groupby
a = [1, 2, 3]
print(list(product(a, a)))
print(list(permutations(a)))
print(list(combinations(a, 2)))
print(list(combinations_with_replacement(a, 2)))
print(list(accumulate(a)))
print(list(groupby(a)))
</code>

4. Lambda Functions

Anonymous one‑liner functions defined with the syntax lambda args: expression .

<code>even_or_odd = lambda a: a % 2 == 0
numbers = [1, 2, 3, 4, 5]
even = list(map(even_or_odd, numbers))
print(even)  # [False, True, False, True, False]
</code>

5. Decorators

Decorators wrap a function to extend its behavior without modifying its source.

<code>import functools
def decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        a, b = args
        print(a * b)
        result = func(*args, **kwargs)
        print(a / b)
        return result
    return wrapper
@decorator
def add(x, y):
    return x + y
result = add(5, 6)
print(result)
</code>

6. Generators and Iterators

Generators use yield to produce values lazily, offering memory‑efficient iteration.

<code>def fibonacci(n):
    a, b = 0, 1
    for i in range(n):
        a, b = b, a + b
        yield a
for i in fibonacci(5):
    print(i)  # 1 1 2 3 5
</code>

7. Processes and Threads

Threads share memory within a process, while processes run independently on separate CPUs. (Illustrative image omitted.)

8. Dunder (Double Underscore) Methods

Special methods like __add__ , __mul__ allow operator overloading.

<code>num = 5
print(num * 6)          # 30
print(num.__mul__(6))  # 30

a = 5
b = 6
print(a + b)           # 11
print(a.__add__(b))    # 11
c = 'hello'
d = 'world'
print(c + d)           # helloworld
print(c.__add__(d))    # helloworld
</code>

9. Logging

Python's logging module supports five severity levels: Debug, Info, Warning, Error, and Critical. The article also recommends the third‑party loguru library for simpler usage.

10. Context Managers

Context managers simplify resource handling, most commonly via the with statement for opening and closing files.

<code>with open('./test.txt', 'w') as f:
    f.write('Hello World!')
</code>

Conclusion

The ten advanced Python topics presented here aim to enrich your skill set for both daily development and technical interviews.

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