In Python
In Python
In Python
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
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).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
Example:
python
Copy code
class MyClass:
@staticmethod
def static_method():
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:
Example:
python
Copy code
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):
Example:
python
Copy code
square = lambda x: x * x
print(square(5)) # Output: 25
4. Recursive Functions
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
for number in count_up_to(5):
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
from functools import partial
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
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
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
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
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
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
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
print(greeting("Ajay")) # Output: Hello, Ajay!
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
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).
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
Example:
python
Copy code
class MyClass:
@staticmethod
def static_method():
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:
Example:
python
Copy code
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):
Example:
python
Copy code
square = lambda x: x * x
print(square(5)) # Output: 25
4. Recursive Functions
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
for number in count_up_to(5):
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
from functools import partial
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
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
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
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
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
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
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
print(greeting("Ajay")) # Output: Hello, Ajay!
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
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).
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
Example:
python
Copy code
class MyClass:
@staticmethod
def static_method():
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:
Example:
python
Copy code
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):
Example:
python
Copy code
square = lambda x: x * x
print(square(5)) # Output: 25
4. Recursive Functions
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
for number in count_up_to(5):
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
from functools import partial
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
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
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
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
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
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
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
print(greeting("Ajay")) # Output: Hello, Ajay!
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
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).
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
Example:
python
Copy code
class MyClass:
@staticmethod
def static_method():
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:
Example:
python
Copy code
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):
Example:
python
Copy code
square = lambda x: x * x
print(square(5)) # Output: 25
4. Recursive Functions
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
for number in count_up_to(5):
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
5. Higher-Order Functions
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
from functools import partial
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
Example:
python
Copy code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
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
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
obj = MyClass()
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():
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:
Example:
python
Copy code
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):
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
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
Example:
python
Copy code
return func(func(value))
def double(x):
return x * 2
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
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
Example:
python
Copy code
def greet(name):
greeting = greet
9. Generator Functions
Example:
python
Copy code
def count_up_to(max):
count = 1
yield count
count += 1
print(number)
# Output: 1 2 3 4 5
These are functions that accept a variable number of arguments using *args
(non-keyword arguments) and **kwargs (keyword arguments).
Example:
python
Copy code
# Output:
Example:
python
Copy code
return a * b
double = partial(multiply, 2)
print(double(5)) # Output: 10
These are functions that are associated with a class and can be either
instance methods or class methods.
python
Copy code
class MyClass:
def instance_method(self):
python
Copy code
class MyClass:
@classmethod
def class_method(cls):
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():