Python is a powerful programming language used for a variety of applications. While many of its features are well-known, there are some hidden gems that are less commonly known. In this article, we will explore the top 10 less known features in Python.
Walrus Operator
The Walrus Operator, also known as the “assignment expression,” is a new feature introduced in Python 3.8. It allows you to assign a value to a variable as part of an expression. For example, you can use it to simplify code like this:
n = int(input())
if n > 10:
print(n)
to this:
if (n := int(input())) > 10:
print(n)
Type Hints
Type hints are a way of indicating the expected type of a variable or function argument in Python. They were introduced in Python 3.5 and can help improve the readability and maintainability of your code. For example:
def greet(name: str) -> str:
return "Hello, " + name
Context Managers
Context managers allow you to manage resources in Python, such as files or network connections, in a safe and efficient way. They are used with the with
statement and ensure that resources are properly closed when they are no longer needed. For example:
with open("file.txt") as f:
content = f.read()
Enumerations
Enumerations are a way of defining a set of named values in Python. They were introduced in Python 3.4 and can help improve the readability and maintainability of your code. For example:
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
Underscores in Numeric Literals
In Python 3.6 and later, you can use underscores in numeric literals to improve their readability. For example:
one_million = 1_000_000
Extended Iterable Unpacking
Extended iterable unpacking allows you to assign multiple values to multiple variables at once in Python. It was introduced in Python 3.0 and can help simplify your code. For example:
first, *middle, last = [1, 2, 3, 4, 5]
F-Strings
F-strings, also known as formatted string literals, are a way of creating strings that include variables in Python. They were introduced in Python 3.6 and can help improve the readability of your code. For example:
name = "John"
age = 30
print(f"My name is {name} and I am {age} years old.")
defaultdict
The defaultdict is a subclass of Python’s built-in dictionary that provides a default value for keys that do not exist. It can help simplify your code by eliminating the need for if statements. For example:py
from collections import defaultdict
d = defaultdict(int)
d["foo"] += 1
functools.lru_cache
The lru_cache
decorator in Python's functools
module provides a way to cache the results of a function for improved performance. It can help speed up your code by avoiding unnecessary calculations. For example:
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
sys.intern
The sys.intern
function in Python allows you to cache strings for improved memory efficiency.
pythonCopy code
import sys
# Define two strings with the same value
string1 = "hello world"
string2 = "hello world"
# Check if the two strings are the same object in memory
if string1 is string2:
print("string1 and string2 are the same object in memory")
else:
print("string1 and string2 are different objects in memory")
# Use sys.intern to cache the strings
string1 = sys.intern(string1)
string2 = sys.intern(string2)
# Check if the two strings are the same object in memory after interning
if string1 is string2:
print("string1 and string2 are the same object in memory after interning")
else:
print("string1 and string2 are different objects in memory after interning"
In this code, we define two strings with the same value ("hello world"
) and check if they are the same object in memory. Since strings are immutable in Python, the interpreter may optimize by storing both strings in the same location in memory. However, we can use sys.intern
to force the interpreter to cache the strings, ensuring that they are the same object in memory.
After calling sys.intern
on the two strings, we check if they are the same object in memory using the is
keyword. If they are, we print a message indicating that they are the same object after interning. If not, we print a message indicating that they are different objects after interning.
Buy some cool fashion products and electronic gadgets from future —
That being said, I’ll be making more such videos on YouTube. If you feel like contributing to me, feel free to do so at Patreon and follow my socials for more content. I’ll catch you asap.
You’re Awesome :)
FadinGeek