Getting Started

Functions

Functions are reusable blocks of code that accept inputs, perform tasks, and optionally return results, helping organize programs efficiently.

Usage

Declaring a function

A function in Python is defined using the def keyword, followed by a name, parentheses (), and a block of code indented below.

pythonCopyEditdef greet():
    print("Hello!")

To execute a function, it must be called using its name with parentheses:

pythonCopyEditgreet()

Parameters and Arguments

Functions can accept parameters (placeholders) and use them within the block.

pythonCopyEditdef greet(name):
    print(f"Hello, {name}!")

Arguments are actual values passed to the function when it is called:

pythonCopyEditgreet("Alice")

A function can accept multiple parameters:

pythonCopyEditdef add(a, b):
    print(a + b)

Return Statement

A function can return a value using the return keyword.

pythonCopyEditdef square(x):
    return x * x

Once return is executed, the function ends. Returned values can be stored in variables:

pythonCopyEditresult = square(5)

Lambda Functions

Lambda functions are Python’s shorthand for simple anonymous functions.

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

Best used for small, one-line functions.

Default Parameters

Functions can define default values for parameters, which are used if no argument is passed.

pythonCopyEditdef greet(name="Guest"):
    print(f"Hello, {name}!")

*args (Rest Parameters Equivalent)

Using *args, a function can accept a variable number of positional arguments as a tuple.

pythonCopyEditdef sum_all(*numbers):
    return sum(numbers)

print(sum_all(1, 2, 3))  # Outputs: 6

Useful when the number of arguments is unknown.

Callback Functions

A function can accept another function as a parameter and invoke it later.

pythonCopyEditdef process_user(callback):
    print("Processing user...")
    callback()

def done():
    print("Done!")

process_user(done)

Callbacks are often used in asynchronous programming, such as with threading or async I/O.

Immediately Executed Code

Python does not have a direct IIFE equivalent, but functions can be defined and called immediately:

pythonCopyEdit(lambda: print("This runs immediately"))()

Or using a traditional function:

pythonCopyEditdef run():
    print("Immediate run")

run()

This pattern can be useful for encapsulation or quick execution.

Conclusion

Functions are a fundamental part of Python, enabling code reuse, modularity, and clean structure. In the next section, we’ll explore objects and data structures like lists, dictionaries, and classes—essential tools for organizing and managing data.

Usage

Declaring a function

A function in Python is defined using the def keyword, followed by a name, parentheses (), and a block of code indented below.

pythonCopyEditdef greet():
    print("Hello!")

To execute a function, it must be called using its name with parentheses:

pythonCopyEditgreet()

Parameters and Arguments

Functions can accept parameters (placeholders) and use them within the block.

pythonCopyEditdef greet(name):
    print(f"Hello, {name}!")

Arguments are actual values passed to the function when it is called:

pythonCopyEditgreet("Alice")

A function can accept multiple parameters:

pythonCopyEditdef add(a, b):
    print(a + b)

Return Statement

A function can return a value using the return keyword.

pythonCopyEditdef square(x):
    return x * x

Once return is executed, the function ends. Returned values can be stored in variables:

pythonCopyEditresult = square(5)

Lambda Functions

Lambda functions are Python’s shorthand for simple anonymous functions.

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

Best used for small, one-line functions.

Default Parameters

Functions can define default values for parameters, which are used if no argument is passed.

pythonCopyEditdef greet(name="Guest"):
    print(f"Hello, {name}!")

*args (Rest Parameters Equivalent)

Using *args, a function can accept a variable number of positional arguments as a tuple.

pythonCopyEditdef sum_all(*numbers):
    return sum(numbers)

print(sum_all(1, 2, 3))  # Outputs: 6

Useful when the number of arguments is unknown.

Callback Functions

A function can accept another function as a parameter and invoke it later.

pythonCopyEditdef process_user(callback):
    print("Processing user...")
    callback()

def done():
    print("Done!")

process_user(done)

Callbacks are often used in asynchronous programming, such as with threading or async I/O.

Immediately Executed Code

Python does not have a direct IIFE equivalent, but functions can be defined and called immediately:

pythonCopyEdit(lambda: print("This runs immediately"))()

Or using a traditional function:

pythonCopyEditdef run():
    print("Immediate run")

run()

This pattern can be useful for encapsulation or quick execution.

Conclusion

Functions are a fundamental part of Python, enabling code reuse, modularity, and clean structure. In the next section, we’ll explore objects and data structures like lists, dictionaries, and classes—essential tools for organizing and managing data.

Usage

Declaring a function

A function in Python is defined using the def keyword, followed by a name, parentheses (), and a block of code indented below.

pythonCopyEditdef greet():
    print("Hello!")

To execute a function, it must be called using its name with parentheses:

pythonCopyEditgreet()

Parameters and Arguments

Functions can accept parameters (placeholders) and use them within the block.

pythonCopyEditdef greet(name):
    print(f"Hello, {name}!")

Arguments are actual values passed to the function when it is called:

pythonCopyEditgreet("Alice")

A function can accept multiple parameters:

pythonCopyEditdef add(a, b):
    print(a + b)

Return Statement

A function can return a value using the return keyword.

pythonCopyEditdef square(x):
    return x * x

Once return is executed, the function ends. Returned values can be stored in variables:

pythonCopyEditresult = square(5)

Lambda Functions

Lambda functions are Python’s shorthand for simple anonymous functions.

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

Best used for small, one-line functions.

Default Parameters

Functions can define default values for parameters, which are used if no argument is passed.

pythonCopyEditdef greet(name="Guest"):
    print(f"Hello, {name}!")

*args (Rest Parameters Equivalent)

Using *args, a function can accept a variable number of positional arguments as a tuple.

pythonCopyEditdef sum_all(*numbers):
    return sum(numbers)

print(sum_all(1, 2, 3))  # Outputs: 6

Useful when the number of arguments is unknown.

Callback Functions

A function can accept another function as a parameter and invoke it later.

pythonCopyEditdef process_user(callback):
    print("Processing user...")
    callback()

def done():
    print("Done!")

process_user(done)

Callbacks are often used in asynchronous programming, such as with threading or async I/O.

Immediately Executed Code

Python does not have a direct IIFE equivalent, but functions can be defined and called immediately:

pythonCopyEdit(lambda: print("This runs immediately"))()

Or using a traditional function:

pythonCopyEditdef run():
    print("Immediate run")

run()

This pattern can be useful for encapsulation or quick execution.

Conclusion

Functions are a fundamental part of Python, enabling code reuse, modularity, and clean structure. In the next section, we’ll explore objects and data structures like lists, dictionaries, and classes—essential tools for organizing and managing data.

© Layer Docs

Made by Alejandro · Powered by Framer

Create a free website with Framer, the website builder loved by startups, designers and agencies.