Day 7: Functions – Part 2
- Scope of variables
- Recursion
- Lambda functions
Table of Contents
- Scope of Variables
- Global Variables
- Local Variables
- Naming Conflicts
- Recursion
- Base Case
- Examples of Recursion
- Lambda Functions
- Syntax
- Examples
Scope of Variables
In Python, the scope of a variable determines where in the code the variable can be accessed. There are two types of variable scopes: global and local.
Global Variables
A global variable is one that is defined outside of any function and can be accessed from any part of the code. Here is an example:
Local Variables
A local variable is one that is defined inside a function and can only be accessed within that function. Here is an example:
Naming Conflicts
It’s important to understand the scope of variables to avoid naming conflicts and unintended changes to the value of a variable. For example, if a global variable has the same name as a local variable, the local variable takes precedence and will be used instead of the global variable within that function. Here is an example:
Recursion
Recursion is a programming technique where a function calls itself repeatedly until it reaches a base case. The base case is a condition where the function stops calling itself and returns a value. Recursion is commonly used to solve problems that can be divided into smaller sub-problems that are easier to solve.
Base Case
The base case is a condition where the function stops calling itself and returns a value. Here is an example of a recursive function to calculate the factorial of a number:
Examples of Recursion
Here are some more examples of recursive functions:
Fibonacci sequence
Binary Search
Lambda Functions
A lambda function is an anonymous function that can have any number of arguments, but can only have one expression. Lambda functions are useful when you need a small function for a short period of time.
Syntax
Examples
Here are some more examples of lambda functions:
Sorting a list of tuples by the second element
Filtering even numbers from a list
Lambda functions are often used in conjunction with other functions, such as map()
, filter()
, and reduce()
.
In conclusion, in this lesson, we explored the scope of variables, recursion, and lambda functions. Understanding these concepts will allow you to write more efficient and flexible code.
Scope of Variables
- Global variables can be accessed from any part of the code, but should be used sparingly to avoid naming conflicts and unintended changes to the value of a variable.
- Local variables are defined inside a function and can only be accessed within that function.
- Naming conflicts can occur when a global variable has the same name as a local variable. In such cases, the local variable takes precedence within that function.
Recursion
- Recursion is a powerful technique that allows you to solve complex problems by breaking them down into smaller sub-problems.
- A recursive function calls itself repeatedly until it reaches a base case, which is a condition where the function stops calling itself and returns a value.
- Recursive functions can be difficult to understand and debug, so it’s important to use them judiciously and test them thoroughly.
Lambda Functions
- Lambda functions are anonymous functions that can be used to create short, one-time functions.
- Lambda functions are useful when you need to pass a small function as an argument to another function, such as
map()
,filter()
, orreduce()
. - Lambda functions are limited to a single expression, so they cannot be used for complex operations.
In conclusion, understanding the scope of variables, recursion, and lambda functions is essential for writing efficient and flexible code in Python. These concepts can be used to solve a wide range of problems and are important tools in any Python programmer’s toolkit.
- Python Programming: – Day 5: Control Flow Statements
- Python Programming: – Day 4: Data Types and Variables
- Python Programming: – Day 3: Basic Concepts and Operations
- Python Programming: – Day 2: Installing and Setting up Python
- Python Programming: – Day 1: Introduction to Python
- Python Programming: – Day 6: Functions Part -1