Python Functions

"Functions in Python are reusable blocks of code that perform specific tasks. They enhance code organization, modularity, and readability. By defining functions, you can avoid code duplication, improve maintainability, and promote code reusability."- Gemini 2024

Built-in Functions
Function Definition Example
print() Prints values to the console print("Hello, world!")
len() Returns the length of a sequence (string, list, tuple) my_list = [1, 2, 3]
print(len(my_list)) # Output: 3
sorted() Sorts a sequence (list, tuple) in ascending order my_list = [3, 1, 2]
sorted_list = sorted(my_list)
print(sorted_list) # Output: [1, 2, 3]
max() Returns the largest item in a sequence my_list = [3, 1, 2]
max_value = max(my_list)
print(max_value) # Output: 3
min() Returns the smallest item in a sequence my_list = [3, 1, 2]
min_value = min(my_list)
print(min_value) # Output: 1
See them all in the Python Docs
Custom Functions
Function Signature

A function signature defines the function's name and parameters.

Parameters vs. Arguments

Parameters are placeholders for values that a function expects to receive, while arguments are the actual values passed to the function when it's called.


Keyword vs Positional Arguments

Keyword arguments are passed using the keyword name, while positional arguments are passed based on their position in the function call.

Default Values

Default values can be assigned to parameters, providing flexibility in function calls.


Variable Scope

The scope of a variable determines where it can be accessed within a program.

Parameter Passing

Python uses pass-by-reference for mutable objects and pass-by-value for immutable objects.


Anonymous Functions (Lambda Expressions)

Small, unnamed functions defined using the `lambda` keyword.


Args & Kwargs

In Python, the * and ** symbols have specific meanings when used in function parameters:

  1. *args:
    • Collects multiple positional arguments into a tuple.
    • Allows functions to accept a variable number of arguments.
  2. **kwargs:
    • Collects keyword arguments into a dictionary.
    • Allows functions to accept a variable number of keyword arguments.


Type Hints (Optional)

Providing type hints for function parameters and return values can improve code readability and maintainability.


# Custom Function

def greet(name, greeting='Hello'):
    return f'{greeting}, {name}!'

# Called with positional arguments
result1 = greet('Alice', 'Hey')
print(result1)

# Called with keyword arguments
result2 = greet(name='Bob', greeting='Hi')
print(result2) 

# Called using a default value
result3 = greet('Charlie')
print(result3)


# Lambda Function

square = lambda x: x**2
result = square(5)
print(result)


# Function with type hints and documentation

def greet(name: str) -> str:
    """Greets the given name.

    Args:
        name: The name to greet.

    Returns:
        A greeting message.
    """

    return f"Hello, {name}!"
# Describe data

def get_info(values):
    result = {
        'count': len(values),
        'avg': sum(values)/len(values),
        'max': max(values),
        'min': min(values)
    }
    return result

data = [28, 49, 16, 7, 88]
info = get_info(data)
print(info)

# Print nicely
import json
print(json.dumps(info, indent=4))

# Reverse a string

def reverse_word(word):
    last_index = len(word)-1
    result = ''
    for i in range(len(word)):
        result += word[last_index - i]
    return result

print(reverse_word('stressed'))

# Count the characters in a string

def count_chars(input_string, stripped=False):
    if stripped:
        count = len(input_string.strip())
    else:
        count = len(input_string)
    return count

result = count_chars('')
print(result)

result = count_chars('0123456789')
print(result)

result = count_chars('Hi you')
print(result)

result = count_chars('  Hi you  ')
print(result)

result = count_chars('  Hi you  ', True)
print(result)


# Named parameters

def grade(score, total=100, passing=60, pass_fail=True):
    grade = 'Pass'
    if pass_fail:
        if score < passing:
            grade = 'Fail'
    else:
        if score >= (total * .9):
            grade = 'A'
        elif score >= (total * .8):
            grade = 'B'
        elif score >= (total * .7):
            grade = 'C'
        else:
            grade = 'D'
    return grade

scores = [0, 100, 90, 80, 70, 91, 69]

for s in scores:
    letter = grade(s) # default values
    print(f'{s} = {letter}')

for s in scores:
    letter = grade(s, 110, 70, False) # positional arguments
    print(f'{s} = {letter}')

for s in scores:
    letter = grade(s, pass_fail=False) # keyword argument
    print(f'{s} = {letter}')


# Positional & Keyword Arguments

def my_function(*args, **kwargs):
    print("Positional arguments:", args)
    print("Keyword arguments:", kwargs)

my_function(1, 2, 3, key1="value1", key2="value2")


# Example Courtesy of Claude.AI

def train_ai_assistant(
    name: str,
    intelligence_level: int = 100,
    *knowledge_domains: str,
    personality: str = "friendly",
    sarcasm_level: float = 0.0,
    **special_abilities: bool
) -> "AI_Assistant":
    # Function implementation would go here
    pass

my_ai = train_ai_assistant(
    "Claude",
    150,
    "science", "philosophy", "humor",
    personality="witty",
    sarcasm_level=0.3,
    time_travel=True,
    telepathy=False
)