Advance Python Concept I
The Memory Overhead Challenge
In conventional Python programming, data structures like lists, tuples, and dictionaries offer excellent versatility. However, they come with a significant performance trade-off: memory footprint. When you instantiate a list with, say, 1 million integers, the interpreter allocates a contiguous block of memory large enough to hold references to all those objects simultaneously. This eager evaluation strategy is often suboptimal — or entirely infeasible — for applications dealing with large-scale data streams, log files, or IoT telemetry. It can lead to high RAM consumption, garbage collector thrashing, and ultimately, MemoryError exceptions.
- Generators
The Generator Paradigm
Generators solve this architectural bottleneck through lazy evaluation — a hallmark of functional programming. A generator does not compute all values at once. Instead, it pauses its state after yielding a value and resumes exactly where it left off when requested. This means they operate with O(1) memory complexity regardless of the total sequence length, as they only store the current state of the frame, not the entire dataset.
Under the Hood: The yield Keyword
Unlike standard functions that use return to terminate execution and discard the local scope, a generator function uses the yield keyword. This transforms the function into a generator object that implements the iterator protocol—specifically the __next__() method.
When you call next(gen), the function executes until it hits a yield, emits the value, and suspends its stack frame (including local variables, loop counters, and instruction pointer). Upon the subsequent next() call, the frame is reactivated, and execution continues seamlessly.
def generators:
for i in range(400):
yield i
gen = generators()
print(next(gen)) #prints 0
print(next(gen)) #prints 12. List Comprehension
It creates a new list based on the another list, in a single and readable form.
It helps you write multiple lines of code in a single one.
In the below example:
word_len2 = [len(word) for word in words if word != ‘are’]
len(word) the return value
for word in words the for loop
if word != ‘are’ its condition
3. Lambda Function
Lambda function is an inline function which is defined at the same place we want to call it. Other def function is defined at one place and called at another. Lambda function is easy to track and find what is being done in this function. It is also known as anonymous function.
syntax:
my_lambda_function = lambda x, y : x + y
my_lambda_function function name
lambda keyword
x, y inputs
: colon to separate input and output
x + y outputs
which_num = lambda x : 'even' if x % 2 == 0 else 'odd'
print(which_num(2))
numbers = [2, 3, 4, 5, 7]
# lambda way
ans = lambda i : 'even' if i % 2 == 0 else 'odd'
results = list(map(ans, numbers))
print(results)
# list comprehension
results = ['even' if num % 2 == 0 else 'odd' for num in numbers ]
print(results)4. Multiple Function Argument
Get Leekhitame’s stories in your inbox
Join Medium for free to get updates from this writer.
Functions in python gets predefined number of argument.
def func (a, b, c):
if a != 0:
return a
print(func(3, 4, 5)If we pass another argument *options, it can pass multiple argument. We can pass multiple argument using *options where arguments are passed as a tuple. If we use **options then it wants argument to be passed as dictionary.
- options when you don’t know how many positional argument you may pass
commonly known as *agrs - **options when you don’t know how many keywords argument you may pass
commonly known as **kwargs
def func (a, b, c, *options):
if a != 0:
if options[0] == 'add':
return a + b + c
print(func(1, 2, 5, 'add'))
def func1 (a, b, c, **options):
if options.get('actions') == "subs":
return b - c
print(func1(7, 9, 1, actions = 'subs', attri = 'mul'))5. Regular Expression
It is use to match the pattern of text. For an example:
import re
text = "I have 2 dogs and 15 cats."
numbers = re.findall(r"\d+", text)
print(numbers)here, we are using re module then with re.findall we are finding all 0-9 digits from text. \d is the key to find 0–9 digits.
6. Exception Handling
Sometimes, our code might blast due to various reason. In order to handle error and perform anything when the certain exception is raised then we can do exception error handling try/except block.
try:
a = 10
b = 0
print(a / b)
except ZeroDivisionError:
print("You cannot divide by zero.")7. SETS
Set do not let any duplicate value to enter in its list.
print(set("my name is likhita and likhita is my duplicate name".split()))functions in sets are:
system_difference: to find difference in both set
difference: to find who is in only one set
intersection: to find which is in both set
union: to find who are in both sets
a = set(["ann", "billy", "cabel"])
b = set(["billy", "rino"])
print(a.symmetric_difference(b))
print(b.symmetric_difference(a))
print(a.difference(b))
print(b.difference(a))
print(a.intersection(b))
print(b.intersection(a))
print(a.union(b))Thank you for reading. Take a break, get a sip of water. And, wait for part two of this article with more advance concept on python.
Enjoy Your Life.
LA







