Welcome back to our programming tutorial series! Now that you’ve learned the basics of functions, it’s time to dive deeper into more advanced function concepts: default arguments, lambda functions, and variable scope. These tools will help you write more flexible and concise code.


Default Arguments: Simplifying Function Calls #

Default arguments allow you to set a default value for a parameter. If no value is passed for that parameter when calling the function, the default value is used.

Here’s an example:

def greet(name="stranger"):
    print(f"Hello, {name}!")

If you call greet() without passing an argument, it will use the default value:

greet()  # Outputs: Hello, stranger!
greet("Alice")  # Outputs: Hello, Alice!

Default arguments make your functions more flexible, allowing you to handle different cases without writing extra code.


Lambda Functions: Writing Small, Anonymous Functions #

Lambda functions (also known as anonymous functions) are a concise way to write simple functions. They are often used when you need a short function for a specific task and don’t want to define a full def block.

Here’s how to write a lambda function:

add = lambda a, b: a + b
print(add(5, 3))  # Outputs: 8

Lambda functions are useful when working with functions that take other functions as arguments, like the map(), filter(), or sorted() functions.

Example with sorted(): #

fruits = ["banana", "apple", "cherry"]
sorted_fruits = sorted(fruits, key=lambda fruit: len(fruit))
print(sorted_fruits)  # Outputs: ['apple', 'banana', 'cherry']

Variable Scope: Understanding Local and Global Variables #

Scope refers to where a variable can be accessed in your program. There are two types of scope:

  1. Local Scope: Variables defined inside a function have local scope. They can only be accessed within that function.
  2. Global Scope: Variables defined outside of all functions have global scope and can be accessed anywhere in the program.

Local Scope Example: #

def my_function():
    x = 10  # Local variable
    print(x)

my_function()  # Outputs: 10
print(x)  # Error! x is not defined outside the function

Global Scope Example: #

x = 10  # Global variable

def my_function():
    print(x)  # Can access the global variable

my_function()  # Outputs: 10

If you need to modify a global variable inside a function, use the global keyword:

x = 10

def update_x():
    global x
    x = 20

update_x()
print(x)  # Outputs: 20

Practical Exercise: Create a Custom Sorting Function #

Now that you understand default arguments, lambda functions, and variable scope, let’s put it all together:

  1. Write a function that takes a list of numbers and an optional sorting order (ascending or descending).
  2. Use a lambda function as the sorting key.
  3. Use default arguments to set the default sorting order to ascending.

Here’s a starter example:

def sort_numbers(numbers, reverse=False):
    return sorted(numbers, reverse=reverse)

# Example usage
numbers = [5, 2, 9, 1, 5, 6]
print(sort_numbers(numbers))  # Ascending order
print(sort_numbers(numbers, reverse=True))  # Descending order

What’s Next? #

Great work! You’ve just learned about advanced function concepts like default arguments, lambda functions, and variable scope. In the next post, we’ll explore how to use modules to organize and reuse your code more efficiently.



Happy coding, and we’ll see you in the next lesson!