DSA: The 8-Week Grind — Day 3


Advanced Python Concepts — Code Smart, Code Pythonic
“Today, I’m writing code that doesn’t just work — it makes sense.”
🔹 1. Lambda Functions — Mini Functions On-theFly
Explanation:
A lambda is a small anonymous function that you define where you need it. It’s useful for quick calculations or one-time use cases.
add = lambda a, b: a + b
print(add(5, 3)) # Output: 8
🔹 2. Map, Filter, Reduce — Mass Processing Tools
Explanation:
map() applies a function to every item in a list.
filter() keeps only items that meet a condition.
reduce() rolls up a list into a single value.
nums = [1, 2, 3, 4]
# map: Squaring each number
squares = list(map(lambda x: x * x, nums))
print(squares) # Output: [1, 4, 9, 16]
# filter: Only keep even numbers
even = list(filter(lambda x: x % 2 == 0, nums))
print(even) # Output: [2, 4]
# reduce: Multiply all numbers together
from functools import reduce
product = reduce(lambda x, y: x * y, nums)
print(product) # Output: 24
🔹 3. List & Dict Comprehensions — One-Liner Loops
Explanation:
You can create new lists or dictionaries from an existing iterable in a single readable line.
squares = [x * x for x in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]
squares_dict = {x: x * x for x in range(5)}
print(squares_dict) # Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
🔹 4. Enumerate & Zip — Looping Made Pythonic
Explanation:
enumerate() gives you the index + value when looping.
zip() allows you to loop through multiple lists together.
langs = ['Python', 'Java']
for i, lang in enumerate(langs):
print(i, lang)
# Output:
# 0 Python
# 1 Java
names = ['Goutham', 'Bob']
scores = [95, 90]
for name, score in zip(names, scores):
print(f"{name} scored {score}")
# Output:
# Goutham scored 95
# Bob scored 90
🔹 5. Constructor (__init__
) — Birthplace of an Object
Explanation:
A constructor method runs every time you create an object, initializing its data.
class Student:
def __init__(self, name):
self.name = name
s = Student("Goutham")
print(s.name) # Output: Goutham
🔹 6. Instance, Class, Static Methods — Levels of Control
Explanation:
Instance Methods work on object data.
Class Methods work on class-level data.
Static Methods are utility functions inside classes.
class Example:
def instance_method(self):
print("Instance Method")
@classmethod
def class_method(cls):
print("Class Method")
@staticmethod
def static_method():
print("Static Method")
obj = Example()
obj.instance_method() # Output: Instance Method
Example.class_method() # Output: Class Method
Example.static_method()# Output: Static Method
🔹 7. Property Decorator — Clean Getters and Setters
Explanation:
Using @property
, you can make methods behave like attributes, making code cleaner.
class Circle:
def __init__(self, radius):
self._radius = radius
@property
def radius(self):
return self._radius
c = Circle(5)
print(c.radius) # Output: 5
🔹 8. Decorators — Wrapping Functions for Extra Power
Explanation:
Decorators allow you to "wrap" extra functionality around existing functions without modifying them.
def decorator(func):
def wrapper():
print("Before")
func()
print("After")
return wrapper
@decorator
def greet():
print("Hello")
greet()
# Output:
# Before
# Hello
# After
🔹 9. Wrapper Class — Supercharging Existing Objects
Explanation:
A wrapper class surrounds another object to modify or control its behavior.
class ListWrapper:
def __init__(self, lst):
self.lst = lst
def append(self, item):
print(f"Adding {item}")
self.lst.append(item)
def __str__(self):
return str(self.lst)
my_list = ListWrapper([1, 2])
my_list.append(3) # Output: Adding 3
print(my_list) # Output: [1, 2, 3]
🔹 10. NamedTuple & Dataclass — Lightweight Data Holders
Explanation:
These are shortcuts to define simple classes for storing data.
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(1, 2)
print(p.x, p.y) # Output: 1 2
from dataclasses import dataclass
@dataclass
class Employee:
name: str
salary: int
e = Employee("John", 5000)
print(e.name, e.salary) # Output: John 5000
🔹 11. Generators — Lazy Iterators (Efficient Memory Usage)
Explanation:
Generators yield one value at a time, which is memory-efficient for large datasets.
def countdown(n):
while n > 0:
yield n
n -= 1
for num in countdown(3):
print(num)
# Output:
# 3
# 2
# 1
🔹 12. Context Managers — Automated Setup & Cleanup
Explanation:
The with
statement handles opening and closing resources cleanly.
with open('file.txt', 'w') as f:
f.write("Hello World")
# File is automatically closed after this block.
🟢 Day 3 Takeaways:
✅ You now understand what these advanced Python concepts actually do
✅ You saw exactly how they work in code with real outputs
✅ From here on, your DSA solutions will be cleaner, smarter, and interview-grade
#DSA #8WeekGrind #Day3 #PythonAdvancedExplained #CodeWithOutputs #LearningInPublic #PythonDSA
Subscribe to my newsletter
Read articles from SAI GOUTHAM directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

SAI GOUTHAM
SAI GOUTHAM
💻 Experienced Computer Science graduate with 3+ years in software engineering, specializing in full-stack web development and cloud solutions. 🥇 Proficient in Python, JavaScript, and SQL, with expertise in React.js, Node.js, Django, and Flask. 🎖️ Skilled in optimizing system performance and deploying scalable applications using AWS. Strong background in agile methodologies and DevOps practices. 🥅 Committed to delivering high-quality, efficient, and scalable software solutions.