Skip to main content

Posts

Showing posts with the label functions

Function Caching in Python

  Learning Sections          show Function Caching in Python Function caching is a technique used to store the results of expensive function calls and reuse those results when the same inputs occur again. Python's functools module provides a built-in way to cache function results using the lru_cache decorator. Using lru_cache from functools The lru_cache decorator caches the results of a function based on its inputs. It uses a Least Recently Used (LRU) caching strategy to manage the cache size: from functools import lru_cache # Apply the lru_cache decorator @lru_cache (maxsize= 128 ) def fib ( n ): if n == 0 : return 0 elif n == 1 : return 1 else : return fib ( n - 1 ) + fib ( n - 2 ) # Call the cached function print ( fib ( 10 )) In this example, the Fibonacci function results are cached, so repeated calls with the same input are faster. ...

Lambda Functions in Python

  Learning Sections          show Lambda Functions in Python Lambda functions, also known as anonymous functions, are small, unnamed functions defined with the lambda keyword. They can have any number of arguments but only one expression. Lambda functions are often used for short, throwaway functions that are not needed elsewhere in the code. 1. Basic Syntax The syntax for a lambda function is: lambda arguments : expression Example: add = lambda x , y : x + y print ( add ( 2 , 3 )) # Output: 5 2. Using Lambda with Built-in Functions Lambda functions are commonly used with built-in functions like map() , filter() , and sorted() . Example with map() : numbers = [ 1 , 2 , 3 , 4 , 5 ] squared = map ( lambda x : x ** 2 , numbers ) print ( list ( squared )) # Output: [1, 4, 9, 16, 25] Example with filter() : numbers = [ 1 , 2 , 3 , 4 , 5 ] even = filter ( lambd...

Enumerate Function in Python

  Learning Sections          show Enumerate Function in Python The enumerate function in Python adds a counter to an iterable and returns it as an enumerate object. This is particularly useful when you need to have an index while iterating over a list or any other iterable. 1. Basic Usage The basic syntax for enumerate is: enumerate ( iterable , start = 0 ) Here, iterable is the iterable to be enumerated, and start is the index value from which the counter is to be started (default is 0). 2. Example Usage Consider the following example where we enumerate over a list of fruits: # Example of using enumerate fruits = [ 'apple' , 'banana' , 'cherry' ] for index , fruit in enumerate ( fruits ): print ( f"{index}: {fruit}" ) # Output: # 0: apple # 1: banana # 2: cherry 3. Using start parameter The start parameter allows you to specify the starting in...

Functions in Python

  Learning Sections      show Functions in Python Functions in Python are reusable blocks of code that perform a specific task. They help in organizing code into manageable sections and avoid repetition. Functions are defined using the def keyword followed by the function name and parentheses () . Defining a Function A function is defined using the def keyword. Here's a simple example of a function that takes a parameter and prints a greeting message: # Defining a function def greet ( name ): # Function body print ( "Hello, " + name + "!" ) # Calling the function greet ( "Alice" ) # Output: Hello, Alice! Return Statement The return statement is used to return a value from a function. If no return statement is used, the function returns None by default. # Function with return statement def add ( a , b ): return a + b # Calling the function result = add ( 5 , 3 ...