Lambda
A lambda function is a small, anonymous function that is defined without a name and contained entirely within a single line of code.
A lambda function can take any number of arguments, but it can only contain one expression. The result of this expression is automatically returned without using a return keyword.
Map, Filter, Reduce
map(func, *iterables) applies a function to every element of an iterable and returns a map object. Eg: squared = list(map(lambda x: x*x, numbers))
filter(func, *iterables) selects only those elements that satisfy a condition.
Eg: even = list(filter(lambda x: x % 2 == 0, numbers)
reduce() repeatedly applies a function to combine all elements into a single value.
Eg:
from functools impo*t reduce
numbers = [1, 2, 3, 4]
result = reduce(lambda x, y: x + y,*numbers)
print(result) # 10
List Comprehension
They offers short syntax when we have to create new list based on the existing list.
Eg: newlist = [x for x in fruits if x != "apple"]
Iterators
An Iterator is an object that allows you to traverse through elements one at a time, using __iter__() or __next__()
Instead of loading all values at once, iterators produce elements one at a time.
Generators
A Generator is a special type of iterator that generates values lazily using the yield keyword.It generates values lazily and is more memory efficient than storing all values in a list.
def generate_numbers():
yield 1
yield 2
yield 3
gen = generate_numbers()
print(next(gen))
print(next(gen))
print(next(gen))
Object Oriented Programming
Object-Oriented Programming (OOP) is a programming paradigm that organizes software around objects, which contain both data (attributes) and behavior (methods).
There are 4 pillars of OOPs - Abstraction, Encapsulation, Inheritance and Polymorphism
Abstraction means hiding implementation details and exposing only the necessary functionality to the user.
Encapsulation is the process of bundling data and methods together within a class and restricting direct access to internal data.
Inheritance allows a child class to acquire the properties and methods of a parent class.
Polymorphism means "many forms". The same method can behave differently depending on the object.
Class vs Object vs Instance
A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete thing what you made using a specific class. So, object and instance are the same thing, but the word instance indicates the relationship of an object to it's class
Composition vs Inheritance
Inheritance is a "is-a" relationship and composition is "has-a" relationship.
Composition is done by having a instance of another class C as a field of your class, instead of extending C.