In Python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 228

In Python, functions can be categorized into several types based on their

structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().
Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))
def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1
8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5
10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b
double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods


Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).
 Static Methods: Utility methods in classes, without access to class or
instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions


Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python
Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1
increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1
for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial


def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."


print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).
 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"


print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.
Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():
global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count
count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code
from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod
def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.


 Variadic Functions: Accept a variable number of arguments (*args,
**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"


print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions
Functions that take other functions as arguments or return functions as
results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code
counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):
count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.
Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python
Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.


 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:
python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)
print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.
Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:
python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}


11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."


obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.


 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3


2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:
return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b
7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions
Generator functions use yield instead of return. They return a generator
object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)


# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:
def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.


Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:
python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python
Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python
Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet
print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)


display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python
Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."


print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.


 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.
Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).
Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"


greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):


print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.
Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod
def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.


 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions
A function that calls itself to solve a smaller instance of the problem.
Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions
A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python
Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:
python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.
 Instance Method: Operates on the instance of the class (the object
itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python
Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions
Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python
Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):
return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions
In Python, functions are treated as "first-class citizens," meaning you can
pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions


These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)
print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods


Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).
 Static Methods: Utility methods in classes, without access to class or
instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions


Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python
Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1
increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1
for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial


def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."


print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).
 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"


print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.
Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():
global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count
count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code
from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod
def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.


 Variadic Functions: Accept a variable number of arguments (*args,
**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"


print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions
Functions that take other functions as arguments or return functions as
results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code
counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):
count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.
Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python
Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.


 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:
python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)
print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.
Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:
python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}


11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."


obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.


 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3


2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:
return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b
7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions
Generator functions use yield instead of return. They return a generator
object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)


# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:
def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.


Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:
python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python
Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python
Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet
print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)


display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python
Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."


print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.


 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.
Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).
Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"


greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):


print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.
Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod
def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.


 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions
A function that calls itself to solve a smaller instance of the problem.
Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions
A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python
Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:
python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.
 Instance Method: Operates on the instance of the class (the object
itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python
Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions
Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python
Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):
return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions
In Python, functions are treated as "first-class citizens," meaning you can
pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions


These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)
print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods


Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).
 Static Methods: Utility methods in classes, without access to class or
instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions


Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python
Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1
increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1
for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial


def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."


print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).
 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"


print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.
Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():
global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count
count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code
from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod
def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.


 Variadic Functions: Accept a variable number of arguments (*args,
**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"


print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions
Functions that take other functions as arguments or return functions as
results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code
counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):
count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.
Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python
Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.


 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:
python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)
print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.
Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:
python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}


11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."


obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.


 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3


2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:
return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b
7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions
Generator functions use yield instead of return. They return a generator
object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)


# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:
def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.


Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:
python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python
Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python
Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet
print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)


display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python
Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."


print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.


 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.
Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).
Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"


greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):


print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.
Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod
def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.


 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions
A function that calls itself to solve a smaller instance of the problem.
Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions
A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python
Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:
python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.
 Instance Method: Operates on the instance of the class (the object
itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python
Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions
Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python
Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):
return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions
In Python, functions are treated as "first-class citizens," meaning you can
pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions


These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)
print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods


Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).
 Static Methods: Utility methods in classes, without access to class or
instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions


Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python
Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1
increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1
for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial


def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."


print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).
 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"


print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.
Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code

counter = 0

def increment():
global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count
count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code
from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod
def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.


 Variadic Functions: Accept a variable number of arguments (*args,
**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"


print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)

print(factorial(5)) # Output: 120

5. Higher-Order Functions
Functions that take other functions as arguments or return functions as
results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.

Example:

python

Copy code
counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:

python

Copy code

def count_up_to(max):
count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}

11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.
Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."

obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python
Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.

 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.


 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

In Python, functions can be categorized into several types based on their


structure, use case, and behavior. Below are the common types of functions:

1. Built-in Functions

Python comes with many built-in functions that are ready to use without any
need for definition. Some common built-in functions include:

 print(): Displays output to the console.

 len(): Returns the length of an object.

 type(): Returns the type of an object.

 input(): Takes input from the user.

 sum(): Sums the elements of an iterable.

Example:

python

Copy code

print(len([1, 2, 3])) # Output: 3

2. User-defined Functions

These are functions defined by the user to perform specific tasks. You create
them using the def keyword followed by the function name and body.

Example:
python

Copy code

def greet(name):

return f"Hello, {name}!"

print(greet("Ajay")) # Output: Hello, Ajay!

3. Anonymous (Lambda) Functions

Lambda functions are small anonymous functions defined using the lambda
keyword. They can have any number of parameters but only one expression.
Lambda functions are often used as arguments to higher-order functions like
map(), filter(), and sorted().

Example:

python

Copy code

square = lambda x: x * x

print(square(5)) # Output: 25

4. Recursive Functions

A function that calls itself to solve a smaller instance of the problem.


Recursive functions are useful for tasks like factorials, Fibonacci series, or
tree traversals.

Example:

python

Copy code

def factorial(n):

if n == 1:

return 1

else:

return n * factorial(n - 1)
print(factorial(5)) # Output: 120

5. Higher-Order Functions

Functions that take other functions as arguments or return functions as


results are called higher-order functions. These are commonly used in
functional programming.

Example:

python

Copy code

def apply_twice(func, value):

return func(func(value))

def double(x):

return x * 2

print(apply_twice(double, 3)) # Output: 12

6. Pure Functions

A pure function is a function that, given the same input, will always return
the same output without causing any side effects (e.g., modifying global
variables or input arguments).

Example:

python

Copy code

def add(a, b):

return a + b

7. Impure Functions

Impure functions can have side effects like modifying global variables,
printing to the console, or altering mutable data structures.
Example:

python

Copy code

counter = 0

def increment():

global counter

counter += 1

increment()

print(counter) # Output: 1

8. First-Class Functions

In Python, functions are treated as "first-class citizens," meaning you can


pass them as arguments, return them from other functions, and assign them
to variables.

Example:

python

Copy code

def greet(name):

return f"Hello, {name}!"

greeting = greet

print(greeting("Ajay")) # Output: Hello, Ajay!

9. Generator Functions

Generator functions use yield instead of return. They return a generator


object that can be iterated over, producing values one at a time. This is
useful for memory efficiency when working with large data sets.

Example:
python

Copy code

def count_up_to(max):

count = 1

while count <= max:

yield count

count += 1

for number in count_up_to(5):

print(number)

# Output: 1 2 3 4 5

10. Variadic Functions

These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).

 *args: Allows passing a variable number of positional arguments.

 **kwargs: Allows passing a variable number of keyword arguments.

Example:

python

Copy code

def display_info(*args, **kwargs):

print("Positional arguments:", args)

print("Keyword arguments:", kwargs)

display_info(1, 2, 3, name="Ajay", age=30)

# Output:

# Positional arguments: (1, 2, 3)

# Keyword arguments: {'name': 'Ajay', 'age': 30}


11. Partial Functions

Partial functions allow you to fix a certain number of arguments of a function


and generate a new function. They are created using the functools.partial()
method from the functools module.

Example:

python

Copy code

from functools import partial

def multiply(a, b):

return a * b

double = partial(multiply, 2)

print(double(5)) # Output: 10

12. Method Functions

These are functions that are associated with a class and can be either
instance methods or class methods.

 Instance Method: Operates on the instance of the class (the object


itself).

 Class Method: Operates on the class as a whole rather than the


instance. It’s defined using @classmethod.

Example of an instance method:

python

Copy code

class MyClass:

def instance_method(self):

return "This is an instance method."


obj = MyClass()

print(obj.instance_method()) # Output: This is an instance method.

Example of a class method:

python

Copy code

class MyClass:

@classmethod

def class_method(cls):

return "This is a class method."

print(MyClass.class_method()) # Output: This is a class method.

13. Static Methods

Static methods are methods that belong to the class but do not have access
to the instance (self) or class (cls) variables. They are used for utility
functions.

Example:

python

Copy code

class MyClass:

@staticmethod

def static_method():

return "This is a static method."

print(MyClass.static_method()) # Output: This is a static method.

Summary of Python Function Types:

 Built-in Functions: Predefined by Python.

 User-defined Functions: Created by users for specific tasks.


 Lambda (Anonymous) Functions: Small anonymous functions.

 Recursive Functions: Functions that call themselves.

 Higher-Order Functions: Accept or return other functions.

 Pure Functions: No side effects.

 Impure Functions: Have side effects (like modifying global variables).

 First-Class Functions: Treated like any other variable (can be passed


as arguments or returned).

 Generator Functions: Use yield for generating values lazily.

 Variadic Functions: Accept a variable number of arguments (*args,


**kwargs).

 Partial Functions: Fix some arguments using functools.partial.

 Method Functions: Functions within a class (instance methods, class


methods).

 Static Methods: Utility methods in classes, without access to class or


instance variables.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy