Python, created by Guido van Rossum in 1991, is an interpreted, high level-general purpose programming language. Most of us, working in software engineering field must have used Python in our work life. In this post, I’m going to cover the advanced python constructs and how you can efficiently use them to improve your code readability and performance. This is the part 1 and to be followed by Part 2 (coming soon).
1. Comprehensions
You must have heard about list comprehensions (Python 2.0). Comprehensions in Python are the constructs that allows sequence built from other sequences and while the list comprehensions are quite popular/frequently used, Python 3.0 introduced Dictionary and set comprehensions.
#Example Codelist_one=[5,10,15, 20]
new_list=[]
for x in list_one:
new_list.append(x**3)
print(new_list)
Output —
[125, 1000, 3375, 8000]
With list comprehension —
list_two=[5,10,15,20]
new_list=[x**3 for x in list_two]
print(new_list)
Output —
[125, 1000, 3375, 8000]
With condition —
#With conditionlist_three=[13,20,25,45]
new_list=[x**3 for x in list_three if x%2==0]
print(new_list)
Output —
[8000]
Dictionary Comprehension
dict_one=[10,20,30,40]
new_dict={x: x**2 for x in dict_one if x%2==0}
print(new_dict)
Output —
{10: 100, 20: 400, 30: 900, 40: 1600}
2. Default Dict
Default Dict, a part of high performance container datatypes, is a sub class of dict that returns a dictionary object. It never raises a key error and is initialized with a default factory function.
from collections import defaultdicttuition_balance= defaultdict(lambda:200)tuition_deposits = {'Naina' : 100, 'Steve' : 300, 'Benedict' : 200}for name,deposits in tuition_deposits.items():
tuition_balance[name]+=deposits
print(dict(tuition_balance))
Output —
{'Naina': 300, 'Steve': 500, 'Benedict': 400}
3. NamedTuple
NamedTuples are light weight, memory efficient object types which belongs to collections module. These are dict like constructs where you can access the attribute values by index, key name or getattr() function.
from typing import NamedTuple
class Account(NamedTuple):
name:str
value: float
Account(name='Naina',value=100)
Output —
Account(name='Naina', value=100.1)from collections import defaultdict
tuition_balances= defaultdict(lambda:200)tuition_deposits = [Account(name='Naina', value=100),
Account(name='Steve', value= 300),
Account(name='Benedict',value=200)]for deposit in tuition_deposits:
tuition_balances[deposit.name]+=deposit.value
print(dict(tuition_balances))
Output —
{'Naina': 300, 'Steve': 500, 'Benedict': 400}
4. Type Hinting
If you are working with complicated data structures, then type hints are very helpful to sort out the complexity and to make your code more readable (especially when you want to refer the code later).
def calc(x:set, y:set) ->set:
var_one = x + y
var_two = x | y
var_three = x & y
return var_three
a: set = {5,6,20,30,10,4,22}
b: set = {20,30,45,6,7,3,22}
result : set=calc(a,b)
print(result)
Output —
{6, 20, 22, 30}
No comments:
Post a Comment