cs ppt class 11
cs ppt class 11
A simple function is a function that performs a basic task, takes input (arguments), processes it, and
returns an output. It doesn't involve complex logic, just a straightforward operation.
EXAMPLE in Python:
return a + b
Key Points:
In short, a simple function encapsulates a single, clear task in a reusable block of code.
(ARGUMENT) ONLY PARIMETER
In a function, parameters are variables listed in the function definition, while arguments are
the actual values passed to the function when it is called.
In this case:
Key Point:
#Here ‘a’, ’b’, ’c’ are multiple parameters and 2 , 3, and 4 are the arguments passed to the
parameters.
#Output: 9
Example Breakdown:
Key Points:
Multiple Parameters: You can define multiple parameters in the function signature
to handle different inputs.
Arguments: You provide the corresponding arguments when calling the function, and
they are assigned to the respective parameters.
Multiple parameters allow the function to handle more complex logic with multiple inputs.
DEFAULT ARGUMENTS
Default arguments allow you to define a function with default values for one or more parameters. If
the caller does not provide a value for those parameters, the default value is used.
Output:
Example Breakdown:
In the function greet, name defaults to "Guest", and age defaults to 25.
If you don’t provide a value when calling greet(), the function uses the default
values.
Key Points:
This makes your functions more flexible and reduces the need for the caller to specify every value.
(ARGS) ARBITRATY NO. OF
ARGUMENTS
Output:
Example Breakdown:
Key Points:
*args: Collects additional positional arguments passed to the function into a tuple.
Flexible: The function can accept any number of arguments.
Accessing Arguments: Inside the function, args behaves like a tuple
It gives you the flexibility to pass any number of arguments to the function without
needing to specify each one.
It's particularly useful when you're not sure how many arguments a function might
need to handle.
(KWARGS)ARBITARY NO. OF
KEYWORD ARGUMENTS
Output:
Example Breakdown:
The function print_info uses **kwargs to collect the arguments name, age, and
city.
These arguments are stored as key-value pairs in the info dictionary.
Key Points:
It allows you to pass a flexible number of named arguments to a function, making the
function more versatile.
Useful when the function needs to handle multiple named arguments that can vary
each time the function is called.
Syntax:
Use the return keyword followed by the value or expression you want to return.
Example:
Output: #8
Example Breakdown:
The function add performs an addition of two numbers and then returns the result.
The returned value is captured in the variable sum_result and printed.
Key Points:
return: The keyword used to exit the function and send back a value to the caller.
A function can return a value of any type (integer, string, list, etc.).
Optional: If no return is specified, the function returns None by default
Returning a value makes the function reusable and allows the output to be passed
along for further computation.
Without a return statement, a function cannot provide any outcome, limiting its use.
(ANONYMOUS FUNCTIONS) LAMBDA
FUNCTION
Anonymous functions (also known as lambda functions) are small, one-liner functions in
Python that are defined without a name. They are often used for short tasks where you don’t
want to define a full function.
Syntax:
Output: #8
Example Breakdown:
Short tasks: For operations that can be written in a single line and used only once.
In functions like map(), filter(), and sorted(): Lambda functions are often used
when passing a short function as an argument.
Limitations:
Syntax:
Example Breakdown:
# Creating a closure:
Output: # 15
Key Points:
Key Takeaway:
In this closure example, the inner_function remembers the x value from its
surrounding environment (the outer function) even after the outer function has
finished executing.
(USING ARGS & RWARGS TOGETHER)
FUNCTION WITH VARIABLE NO. OF
ARGUMENT
*args: Collects additional positional arguments (tuple).
Function Syntax
Order of Parameters
Output:
#12
(3, 4, 5)
Changed
Explanation of the Example
Key Points
Conclusion
Combining *args and **kwargs provides a flexible way to handle functions with
variable arguments.
This feature is especially useful in scenarios like logging, event handling, etc.
This layout covers key points with an example and explanation, making it easy for an audience to
understand the use of *args and **kwargs together in Python functions.
RECURSIVE FUNCTION
A function that calls itself in order to solve smaller instances of the same problem.
Recursive Formula:
Example usage
result = factorial(5)
Output: #120
Recursion can sometimes lead to higher memory usage and slower performance for large
problems.
FUNCTION WITH TYPE PRINTING
Printing or checking the data type of an argument inside a function.
Useful for debugging, validation, and ensuring functions handle the correct types.
Syntax:
# Example usage
Explanation of Example
This demonstrates how type() shows the data type of the argument inside the function.
Key Points
Conclusion
Printing types inside functions can aid in debugging, validation, and understanding
the behavior of code.
Use type() to ensure that the correct data types are being passed and processed.
HIGHER ORDER FUNCTION
A higher-order function is a function that:
# Example functions
Explanation of Example
multiply_by_2 = multiplier(2)
multiply_by_3 = multiplier(3)
Explanation of Example 2
The function multiplier() returns a function that multiplies its input by a given
factor.
multiply_by_2 and multiply_by_3 are created by passing 2 and 3 to
multiplier().
These returned functions are then used to multiply numbers.
Key Points
Steps:
1. A function accepts another function as an argument.
2. The callback function is executed at an appropriate point inside the main
function.
3. The main function can use the result of the callback to continue processing.
Explanation of Example
Key Points
Syntax of Closures
Output: #15
Explanation:
counter() returns increment(), which remembers the count variable across calls.
Each time count_up() is called, the count value is updated and returned, maintaining
its state.
State Preservation: Closures allow inner functions to "remember" and maintain state
from the outer function.
Encapsulation: Closures hide state from the outside world while still providing
access to it.
Memory Efficiency: No need for global variables to preserve state between function
calls.
FUNCTION WITH YIELD (GENERATOR
FUNCTION)
A generator function is a function that returns an iterator using the yield keyword.
Unlike regular functions that return a single value and exit, generator functions yield
values one at a time and pause execution between each yield.
This allows for lazy evaluation (only generating values when needed), making them
memory efficient.
def generator_function():
yield value
The yield statement pauses the function and sends a value to the caller.
The function's state is saved and can be resumed when next() is called again.
This continues until the function exits, or a StopIteration exception is raised.
Output:
5
Explanation of Example
Explanation of Example 2
Memory Efficiency: Generators yield items one at a time instead of storing all values
in memory.
Lazy Evaluation: Values are computed only when requested (i.e., on-demand).
FUNCTION WITH FOR POSITIONAL
ONLY PAREMETER(PYTHON 3.8+)
Positional-only parameters are function arguments that must be passed by position, not
by keyword.
Introduced in Python 3.8 using the / syntax in function definitions.
The / character in a function's parameter list indicates that the parameters before it are
positional-only.
Explanation:
Clarifies intent: Some parameters are intended only to be used in a positional manner (e.g.,
for API consistency).
Prevents misuse: Disallows calling parameters as keywords when they shouldn’t be.
Improves performance: Avoids ambiguity in performance-critical or low-level functions.
Key Points