Python
programming
hidden-features
tips-and-tricks
software-development

Hidden features of Python

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Python is a versatile and powerful programming language, known for its simplicity and readability. However, there are many lesser-known features that even experienced developers might not be familiar with. In this article, we will explore some of these hidden Python features, providing technical explanations and examples to aid understanding.

List Comprehensions

List comprehensions offer a more compact and often more readable way to create lists. While many developers know basic list comprehensions, fewer might be aware of their full potential.

python
1# Basic list comprehension
2squares = [x**2 for x in range(10)]
3
4# List comprehension with condition
5even_squares = [x**2 for x in range(10) if x % 2 == 0]

Advanced: List comprehensions can be nested, and you can even use them for creating dictionaries and sets.

python
1# Nested list comprehension
2matrix = [[j for j in range(3)] for i in range(3)]
3
4# Dictionary comprehension
5square_dict = {x: x**2 for x in range(10)}
6
7# Set comprehension
8unique_squares = {x**2 for x in range(-9, 10) if x % 2 == 0}

Generators

Generators allow for iterating through potentially large datasets without using a lot of memory. They are particularly useful when dealing with streams of data or in scenarios where you require lazy evaluation.

python
1# Example of a generator function
2def fibonacci_sequence(n):
3    a, b = 0, 1
4    while a < n:
5        yield a
6        a, b = b, a + b
7
8# Using the generator
9for num in fibonacci_sequence(100):
10    print(num)

The yield keyword is what makes a function a generator. It pauses the function, returning a value until the next call.

collections Module

Python's collections module provides alternatives to the standard data types. Here are a few of them:

  • Counter for counting hashable objects.
  • defaultdict to simplify handling of missing dictionary keys.
  • deque for fast appends and pops from both ends (double-ended queue).
  • OrderedDict for maintaining the order of keys.
python
1from collections import Counter, defaultdict, deque, OrderedDict
2
3# Counter
4counter = Counter("success")
5print(counter)  # Outputs: Counter({'s': 3, 'c': 2, 'u': 1, 'e': 1})
6
7# defaultdict
8dd = defaultdict(int)
9dd['key'] += 1  # Automatically initializes 'key' to 0 and then increments.
10
11# deque
12dq = deque([1, 2, 3])
13dq.appendleft(0)    # Fast operation
14
15# OrderedDict
16od = OrderedDict.fromkeys('abcde', 0)
17print(od)  # Maintains the order: {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0}

Context Managers and with Statement

Python's with statement is used to wrap the execution of a block of code within methods defined by a context manager, which ensures proper resource management like file streams or locks.

python
1# File handling with context manager
2with open('example.txt', 'r') as file:
3    content = file.read()
4
5# Custom context managers using 'contextlib'
6import contextlib
7
8@contextlib.contextmanager
9def open_file(filename, mode):
10    file = open(filename, mode)
11    try:
12        yield file
13    finally:
14        file.close()
15
16# Usage
17with open_file('example.txt', 'r') as f:
18    print(f.read())

Decorators

Decorators allow you to modify the behavior of a function or class. They are often used for logging, timing functions, access control, and more. The @ symbol is used to apply a decorator to a function.

python
1# Simple decorator example
2def my_decorator(func):
3    def wrapper():
4        print("Something is happening before the function is called.")
5        func()
6        print("Something is happening after the function is called.")
7    return wrapper
8
9@my_decorator
10def say_hello():
11    print("Hello!")
12
13say_hello()
14
15# Output:
16# Something is happening before the function is called.
17# Hello!
18# Something is happening after the function is called.

The itertools Module

The itertools module is an impressive library for creating complex iterators. It supports a variety of operations like infinite loops, permutation, combination, and Cartesian product.

python
1import itertools
2
3# Infinite counting
4for i in itertools.count(10):
5    if i > 15:
6        break
7    print(i)
8
9# Permutations
10for perm in itertools.permutations('AB'):
11    print(perm)
12
13# Cartesian product
14for product in itertools.product('AB', 'CD'):
15    print(product)

Summary Table

Below is a summary table of the hidden Python features we have discussed:

Feature NameUsage ExampleDescription (Benefits or Use Cases)
List Comprehensions[x**2 for x in range(10)]Creates lists efficiently and can include conditionals.
Generatorsyield keyword and for-loopAllows lazy evaluation and efficient memory usage with large data sets.
collections ModuleCounter, defaultdict, dequeProvides alternative data structures with enhanced functionalities.
Context Managerswith statementHandles resource management automatically, ensuring proper closure.
Decorators@decoratorModifies or extends the behavior of functions or methods in a reusable way.
itertools Moduleitertools.permutations, itertools.countOffers sophisticated iteration constructs, including combinatorial generators.

These hidden features are just a peek into the capabilities Python offers. Using these effectively can lead to more efficient, readable, and concise code. While the features discussed have their own specific use cases, their mastery can significantly enhance one's programming capabilities in Python.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.