Fundamentals » Functions and Methods »


Functions and Methods

The Basics Table Of Contents User Interaction

Python is an object-oriented language, but it provides both a class definition structure for object-oriented programming and standalone functions for procedural programming. In addition, the standard features of Python have been extended with the use of modules. In this chapter, we introduce the use of functions and modules in Python.


A function in Python is similar to a static method in Java. That is, they are defined and used independent of an object. The main difference is that Python functions are not defined within a class.

Built-in Functions

Some functions are very common and are part of the Python language itself These built-in functions are considered to be global to the program. Thus, they are always available and can be invoked using only the function name.

# Compute the absolute value of the integer x
y = abs( x )

Python provides a number of built-in functions which will be introduced and described throughout the book.

Function Arguments

When passing parameters to a function, the reference to the object is passed and not the value itself. This is the expected behavior since Python represents all data as objects and all variables contain references.

# Create a complex number with real value r and imaginary i.
z = complex( r, i )

Multiple parameters are separated with commas as in Java. If a function requires three arguments, then three values must be supplied. Python supports optional arguments which will be explored in a later section.

Parameter Types

Function definitions do not include parameter types as is required in Java. The first argument in the function call is passed to the first parameter in the function definition, the second argument to the second parameter and so on.

The programmer must be responsible and pass the correct argument types expected by the function. Some functions have been defined to check the type of data passed as arguments and will raise an exception if an incorrect data values are provided.


Not having parameter types allows for the development of more powerful functions that can be used with many different data types.

Return Value

Some functions may return a single value. This value can be used or ignored depending on the purpose of the function. If a function returns a value, it will always be an object reference.


Python is a relatively small language but provides the necessary components for creating powerful programs. Additional functionality is available from various components in the Python Standard Library.

The Standard Library includes a number of functions and class definitions in which related components are organized into individual files called modules. A module is simply a Python source file containing various function and class definitions. These components can be used within a Python program via the import statement. To include the math module you would use

from math import *

at the top of your program file. While this appears to be similar to the Java statement

import java.util.*;

note that in Java, each class was stored in a separate file and a collection of classes comprised a package. The Java statement above makes all classes within the java.util package available for use in your Java program. The Python import statement, includes the named module and makes all definitions within a single module avaiable for use in your Python program.

To use a module function or class, you must import the module as described above and then simply specify the function or class name

from math import *
y = sqrt( x )

You can import individual components of a module and make them available within the current file using

from math import sqrt

This version includes the module, but only makes the sqrt() function available for use in your program. Python has a second variation to the import statement which works very much like the from/import version

import random

but in this case, the definition are in a different namespace. For now, we will use the first version until we discuss namespaces in a later chapter. The import statement will be discussed in more detail later when dealing with variable scopes and user-defined modules.

The Python Standard Library contains a number of modules including some with function definitions only and others with class definitions. One of the more commonly used function only modules is described in the following section. A complete list of the standard modules is provided in the appendix.

The Math Module

The standard math module provides a number of mathematical functions. This module is basically a wrapper around the Standard C math functions, though, some functions specific to Python are also included.

To use a function in the math module, you must first import the module. Consider the following sample program.

# Prompts for a real value and computes its square root.

from math import *

x = float( raw_input( "Enter a real value:" ) )
y = sqrt( x )
print "The square root of", x, "is", y

The following is a list of the more commonly used mathematical functions. All parameters must be numeric and each function returns a floating point value unless otherwise indicated.

ceil( x )
Computes and returns the ceiling of x: ⌈ x
floor( x )
Computes and returns the floor of x: ⌊ x
sqrt( x )
Computes the square root of x.
cos( x ), sin( x ), tan( x )
Computes the indicated trigonometric value of radians x.
degrees( x )
Converts the radians x to degrees.
radians( x )
Converts the angle x from degrees to radians.

Objects and Methods

A class is used to define a class of objects and objects are created or instantiated from a class. Once an object has been created, it can then be used. In Python, all literal values result in the creation of objects.

Creating Objects

To instantiate an object directly from the class, the class constructor is called as if it were a standalone function

today = date( 2006, 8, 25 )

In Java, the new operator was used to instantiate objects as illustrated below

// Java object creation
Date today = new Date( 2006, 8, 25 );

The Python date class is defined in the standard datetime module. In order to create objects from the class, it must first be imported. The from version of the import statement

from datetime import date

is commonly used to import individual classes as is done here for the date class. If you use the plain import statement, the class name would remain part of the datetime() module and the module name would have to be prepended to all references of the class

import datetime
tomorrow = 2006, 8, 26 )

Invoking Methods

Once an object has been created, it can then be used via one of its methods. The dot operator is used to select a particular method as is done in Java. In the following example,

whichDay = today.weekday()

returns the day of the week for the given date. The rules for passing arguments and using the return value, if any, are the same as for standalone functions.


Python built-in types have various operators defined that can be used with objects of those types. An example of which are the mathemaical operators defined for the numeric types. Not all operators can be used with every type. But these operators are considered methods of the various classes.

Numeric Type Constructors

The numeric primitive types each have a built-in constructor for creating the various objects. These constructors can also be used to convert between the various types.

complex( x, y )
Creates a complex object with a real part x and an imaginary part y. Both x and y must be numeric.
float( x )
Converts a string or numeric object to a float.
int( x )
Converts a string or numeric object to an integer.
long( x )
Converts a string or numeric object to a long integer.

The Basics Table Of Contents User Interaction

© 2006 - 2008: Rance Necaise - Page last modified on September 15, 2006, at 03:10 PM