Python is a simple to use, high-level, object-oriented programming language. There are some advanced features that are usually discovered through extensive experience and make easy solutions to any problem and represent yourself as an experienced developer.
- Map is a built-in Python function used to apply a function to a sequence of elements like a list or dictionary.
- A map object is the result obtained by applying the specified function to every item present in the iterable.
- It uses to process all the elements present in an iterable without explicitly using a looping construct
def square(num): return num * num result = list(map(square, [5, 10, 15, 25])) print(result) # Output: [25, 100, 225, 625] # ----------------------------- def multiplier(num_a, num_b): return num_a * num_b result = list(map(multiplier, [2, 8, 9], [6, 10, 12])) print(result) # Output: [12, 80, 108]
- The filter() function extracts elements from an iterable (list, tuple, dictionary) for which a function returns True.
- The filter() function will simply make an object which will not be a copy of the list but will be a reference to the original list, along with the function that has been provided to filter, and the indices which have to be traversed in the original list.
The Syntax For Filter
The filter() method in Python will return an iterator that will contain all the elements of the original iterable that has passed the function check
def is_even_number(number): if number % 2 == 0: return True return False # Extract elements from the numbers list result_iterator = filter(is_even_number, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # Convert to list even_numbers = list(result_iterator) print(even_numbers) # Output: [2, 4, 6, 8, 10]
- A Lambda Function is a small, anonymous function that does not contain any name and is contained in a single line of code.
- It can take any number of arguments, but the number of expressions can only be one.
- It makes a clean, simplistic programming language to use and easy to read for simple logical operations.
The Syntax For Lambda function
lambda arguments: expression
# Program to show the use of lambda functions double = lambda value: value * 2 print(double(5)) #Output: 10
Example use with map()
# Program to double each item in a list using map() data = [1, 5, 4, 6, 8, 11, 3, 12] update_data = list(map(lambda value: value * 2 , data )) print(update_data) #Output: [2, 10, 8, 12, 16, 22, 6, 24]
- A decorator is a feature in python that adds some new functionality to existing code without altering the original structure.
- There are two types of decorators:
- function decorators
- class decorators
- A decorators function has an @ before the function name.
- It takes in a function, modifies it by adding functionality, and then returns it.
- It is more like a regular function in Python that can be called and returns a callable.
def decorator(func): def inner(a, b): if b == 0: print("Can't divide by 0") return return func(a, b) return inner def division(a, b): print(a/b) division(8,10) Output: 0.8 division(10,0) Output: Can't divide by 0
- An exception is a condition that occurs during the execution of the program and interrupts the execution.
- Python has many built-in exceptions that are raised when your program encounters an error.
- When these exceptions occur, the Python interpreter stops the current process and passes it to the calling process until it is handled. If not handled, the program will crash.
- We use try and except blocks to handle exceptions in python.
- We can use multiple except blocks to handle multiple exceptions at a time.
try: # Some Code.... except: # Handling of exception
try: value = 5/0 # raises divide by zero exception. print(value) except ZeroDivisionError: # Handles ZeroDivision exception print("Can't divide by zero")