Fundamentals 12 min read

Advanced Python Concepts: Exception Handling, Collections, itertools, Lambdas, Decorators, Generators, and More

This article presents a comprehensive guide to advanced Python topics—including exception handling, the collections and itertools modules, lambda functions, decorators, generators, process/thread basics, dunder methods, logging, and context managers—providing code examples and explanations to help developers deepen their expertise.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Advanced Python Concepts: Exception Handling, Collections, itertools, Lambdas, Decorators, Generators, and More

Advanced Python Concepts

This article shares advanced Python concepts such as exception handling, the collections module, itertools, lambda functions, decorators, generators, processes and threads, dunder methods, logging, and context managers.

1. Exception Handling

Exception handling helps resolve runtime errors. Common built‑in exceptions include ZeroDivisionError , ImportError , and IndexError . Use try and except blocks, optionally with else and finally :

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

2. collections Module

The collections module provides enhanced container data structures.

2.1 Counter

Counts elements of 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))
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.name, user.sex, user.age)
user = user._replace(age=22)
print(user)
print(user._asdict())
</code>

2.3 OrderedDict

Preserves insertion order (now also true for built‑in 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 default values for missing keys.

<code>from collections import defaultdict
dictt = defaultdict(int)
dictt['a'] = 2
print(dictt['a'])  # 2
print(dictt['b'])  # 0
</code>

2.5 deque

Double‑ended queue supporting fast appends/pops on both ends.

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

3. itertools Module

The itertools module offers combinatorial functions.

<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 functions defined with the lambda keyword.

<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 add functionality to functions without modifying their code.

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

<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; processes run in separate memory spaces. (Illustrative diagram 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, b = 5, 6
print(a+b)            # 11
print(a.__add__(b))   # 11
c, d = 'hello', 'world'
print(c+d)            # helloworld
print(c.__add__(d))   # helloworld
</code>

9. Logging

Python’s logging module supports five levels: Debug, Info, Warning, Error, Critical. The third‑party loguru library offers a simpler interface.

10. Context Managers

Context managers handle resource acquisition and release, commonly via the with statement.

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

Conclusion

The ten topics above provide advanced Python knowledge useful for work or interview preparation.

Exception HandlingCollectionsdecoratorsGeneratorsAdvanceditertools
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.