Python Functions Examples

Basic Function Definition:

Here is a simple function called "greet" that takes one parameter name and returns a greeting message.

Code:

``````def greet(name):
"""This function greets the person passed in as an argument."""
return f"Hello, {name}!"
# Example usage:
print(greet("Archana Minea"))
``````

Output:

```Hello, Archana Minea!
```

Function with Default Argument:

Here, the function greet has a default argument name='Guest', so if no argument is provided, it will greet 'Guest'.

Code:

``````def greet(name='Guest'):
"""This function greets the person passed in as an argument, defaulting to 'Guest' if no argument is provided."""
return f"Hello, {name}!"
# Example usage:
print(greet())
``````

Output:

```Hello, Guest!
```

Function with variable number of arguments:

Here in the *args parameter allows the function to accept any number of arguments, and sum(args) computes their sum.

Code:

``````def nums_sum(*args):
"""This function takes any number of arguments and returns their sum."""
return sum(args)
# Example usage:
print(nums_sum(1,2))
print(nums_sum(1,2,-3,4,5))
``````

Output:

```3
9
```

Function with keyword arguments:

The **kwargs parameter allows passing additional keyword arguments, which are then printed alongside the name and age.

Code:

``````def person_info(name, age, **kwargs):
"""This function prints out the person's name, age,
and additional information passed as keyword arguments."""
print(f"Name: {name}, Age: {age}")
for key, value in kwargs.items():
print(f"{key}: {value}")
# Example usage:
person_info("Eutychia Xenia", 28, City="New York", Occupation="Engineer")
``````

Output:

```Name: Eutychia Xenia, Age: 28
City: New York
Occupation: Engineer
```

Lambda Function:

This function defines a lambda function "add" which takes two arguments and returns their sum. Lambda functions are anonymous functions.

Code:

``````add = lambda x, y: x + y
# Example usage:
``````

Output:

```7
```

Function as a First-Class Citizen:

Functions can be passed as arguments to other functions, allowing for dynamic behavior.

Code:

``````def apply_operation(x, y, operation):
"""This function applies the given operation to the given numbers."""
return operation(x, y)
return a + b
def subtract(a, b):
return a - b
# Example usage:
print(apply_operation(5, 3, subtract))
``````

Output:

```8
2
```

Recursion:

Recursion is a technique where a function calls itself. Here, "factorial" calculates the factorial of a number recursively.

Code:

``````def factorial(n):
"""This function calculates the factorial of a number using recursion."""
if n == 0:
return 1
else:
return n * factorial(n-1)

# Example usage:
print(factorial(4))
``````

Output:

```24
```

Generator Function:

Generator functions use "yield" instead of "return" to return data incrementally, enabling efficient memory usage for large datasets.

Code:

``````def fibonacci(n):
"""This function generates Fibonacci sequence up to n."""
a, b = 0, 1
while a < n:
yield a
a, b = b, a + b

# Example usage:
fib_sequence = fibonacci(10)
print("Fibonacci sequence up to 10:")
for num in fib_sequence:
print(num, end=" ")
``````

Output:

```Fibonacci sequence up to 10:
0 1 1 2 3 5 8
```

Function Annotations:

Function annotations provide metadata about the types used by the function parameters and return value.

Code:

``````def add(x: int, y: int) -> int:
return x + y
# Example usage:
``````

Output:

```9
```

Note:

Function annotations in Python are primarily for documentation purposes and do not enforce the types at runtime. If you want the function to strictly enforce integer inputs and output, you can add runtime type checking using isinstance() and raise an error if the input types are not integers:

Code:

``````def add(x: int, y: int) -> int:
if not isinstance(x, int) or not isinstance(y, int):
raise TypeError("Both inputs must be integers.")
return x + y

# Example usage:
print(add(4.2, 5.7))  # This will raise a TypeError
``````

Output:

```9
TypeError: Both inputs must be integers.
```

Decorator:

Decorators allow modifying the behavior of functions or methods. Here, debug is a decorator that prints the name of the called function.

Code:

``````def debug(func):
def wrapper(*args, **kwargs):
print(f"Calling function: {func.__name__}")
return func(*args, **kwargs)
return wrapper

@debug
return a + b
# Example usage:
print("Result:", result)
``````

Output:

```Calling function: add
Result: 9
```

Function with Docstring:

Docstrings are used to provide documentation about the purpose and usage of a function. They are enclosed in triple quotes and are accessible via the __doc__ attribute of the function.

Code:

``````def square(x):
"""This function returns the square of a number."""
return x ** 2
``````

Nested Function:

Nested functions are functions defined within another function. They have access to variables from the enclosing scope.

Code:

``````def outer_function(x):
"""This is the outer function."""
def inner_function(y):
"""This is the inner function."""
return y * 2
return inner_function(x)

# Example usage:
result = outer_function(5)
print(result)
``````

Output:

```10
```

Function with Unpacking:

Unpacking allows passing elements of a collection as individual arguments to a function, making the code more concise.

Code:

``````def print_person_info(name, age):
"""This function prints person's name and age."""
print(f"Name: {name}, Age: {age}")
# Example usage:
person_info = ("Diindiisi", 28)
print_person_info(*person_info)
``````

Output:

```Name: Diindiisi, Age: 28
```

Function with Keyword-Only Arguments (PEP 3102):

Keyword-only arguments can only be passed using keyword syntax, improving the clarity of function calls.

Code:

``````def greet(*, name="Guest"):
"""This function greets the person with the provided name."""
return f"Hello, {name}!"

# Example usage:
greeting = greet(name="Emanuel")
print(greeting)
``````

Output:

```Hello, Emanuel!
```
﻿