Skip to Content
Python: Essential Reference, Third Edition
book

Python: Essential Reference, Third Edition

by David Beazley
February 2006
Intermediate to advanced content levelIntermediate to advanced
648 pages
14h 53m
English
Sams
Content preview from Python: Essential Reference, Third Edition

Functions

You use the def statement to create a function, as shown in the following example:

def remainder(a,b):
        q = a // b       # // is truncating division.
        r = a - q*b
        return r

To invoke a function, simply use the name of the function followed by its arguments enclosed in parentheses, such as result = remainder(37,15). You can use a tuple to return multiple values from a function, as shown here:

def divide(a,b):
        q = a // b        # If a and b are integers, q is integer
        r = a - q*b
        return (q,r)

When returning multiple values in a tuple, it’s often useful to invoke the function as follows:

quotient, remainder = divide(1456,33)

To assign a default value to a parameter, use assignment:

def connect(hostname,port,timeout=300):
    # Function body

When default ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python: Essential Reference

Python: Essential Reference

David M. Beazley

Publisher Resources

ISBN: 0672328623Purchase book