Python

Python Advanced Functions

Python Advanced Functions

This page covers the advanced functions of Python programming.

For Python intermediate functions here.

It continues the previous intermediate tutorials in more details.

Advanced functions encompass:

  • Arbitrary arguments (*args)
  • Keyword arbitrary arguments (**kwargs)
  • Recursion

Please note that both the number and order of parameters inside the function must match the arguments when calling the function.

As previously seen, keyword arguments avoids the problem of parameter/argument order identity.

Arbitrary arguments (*args)

The method of arbitrary arguments deals with the issue of identical number of parameters. When the number of parameters/arguments is not defined, the special keyword “*args” can be used.

This in turn creates a tuple of arguments and matches the relevant argument to the parameter.

The keyword *args” is a common practice, referring to arbitrary arguments (but not mandatory to use it). What must apply is the single star/asterisk sign (*).

def calculation(*number):
    total = 0
    for n in number:
        total += n

    print(f"Total: {total}")


calculation(7, 2, 5)
calculation(7, 2, 5, 13, 15)

# Output: Total: 14
# Output: Total: 42

Another example of arbitrary arguments.

def fruits_name(*fruit):
    print(f"A {fruit[0]} apple.")
    print(f"A {fruit[1]} apple.")

fruits_name("red", "yellow", "green")

# Output: A red apple.
# Output: A yellow apple.

The number of arguments is 3, but the function prints only 2 (first and second). No issues with the number of parameters/arguments identity.

Keyword arbitrary arguments (**kwargs)

The method of keyword arbitrary arguments is similar to “*args”. The difference is that keyword arborary arguments deals with the problem of number of parament/argument identity with keyword arguments.

The keyword is “**kwargs“, but just like with “*args”, the parameter name can be anything. The important notation is the double star/asterisk sign (**).

The keyword arbitrary arguments create a tuple of arguments that matches the relevant arguments to the function’s parameter.

def fruits_name(**fruit):
    print(f"A {fruit['colour']} {fruit['variety']} apple.")

fruits_name(colour = "red", variety = "empire")
fruits_name(colour = "yellow", variety = "gala")

# Output: A red empire apple.
# Output: A yellow gala apple.

Recursion

In mathematics, a recursion is a process of solving a computational problem. The method refers to a process where a part of its operations involves calling the process.

In other words, a function, which call itself inside its block of code. And as such, often recursion methods implement repetitive iterations.

The following is an example of a recursive function.

def rec_func(n):
    print(n)
    if n <= 50:
        rec_func(n + 7)
# Output:
1
8
15
22
29
36
43
50
57

In the above instance, the function “rec_func()” calls itself inside the block of code. The reason for incrementing the result on each iteration by 7 is so the recursion does not continue forever.

Factorial function

A very common way of learning recursion is the factorial function.

The factorial functions finds the factorial number of an integer. A factorial number is the product of all positive numbers between 0 and the chosen number.

The calculation is simple: multiply all integers from the chosen number down to 0. Notation for factorial is “n!“.

For instance:

4! = 4 x 3 x 2 x 1 = 24

def factorial(n):
    if n == 1:
        return 1
    else:
        return (n * factorial(n - 1))

factorial(4)

# Output: 24

Next: Python Advanced Classes

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.