O'Reilly logo

Python Essentials by Steven F. Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Defining functions with positional parameters

The essential Python function definition is built with the def statement. We provide a name, the names of the parameters, and an indented suite of statements that is the body of the function. The return statement provides the range of values.

The syntax looks like this:

def prod(sequence):
    p= 1
    for item in sequence:p *= item
    return p

We've defined a name, prod, and provided a list of only one parameter, sequence. The body of the function includes three statements: assignment, for, and return. The expression in the return statement provides the resulting value.

This fits the mathematical idea of a function reasonably well. The domain of values is any numeric sequence, the range will be a value of the a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required