Python Concepts/Built-in Functions

Page Index

 * Lesson
 * Assignments
 * Further Reading or Review
 * References

Lesson
The Python interpreter has a number of functions and types built into it that are always available. This means that you don't have to import anything so that you can access them. Some of the built-in functions are so powerful, simple, common or useful, such as  that it's difficult to imagine a block of code without them. As we have advanced through this course, we've seen and used many built-in functions  perhaps without realizing that they have their own place and classification in the Python documentation.

class range(start,stop[,step])
In the python documentation the  type is included under "Built-in Functions." However,  is actually an immutable sequence type similar to lists and tuples. Conceptually  is similar to a tuple containing a well-defined sequence of values. Both the sequence and the values may be huge, but  occupies only a small amount of memory and it is very fast.

Of the three arguments  must be included. If is not included, it defaults to $$0.$$ If  is not included, it defaults to $$1.$$

supports indexing.

An operation on  may return   or

Try it with big numbers.

globals
This expression has the appearance of a function but it returns and behaves like a dictionary representing the current global symbol table.

Invoke python on Unix and display information about When you invoke python interactively, the above global variables are predefined. Unless you really know what you are doing, it is recommended that you do not attempt to change any of the predefined global variables.

Python source file

When python is invoked by executing python source file  on Unix, the following global variables are predefined: The values  have been added and value   has changed.

Within the listcomp above:


 * Variables  are local to the listcomp and do not appear in the list of global symbols. This is why a listcomp was used.


 * Each value has been converted to  for printing. (Actually so that method   will work properly.)

Accessing dictionary globals
To delete a global variable:

Creating global variables
During execution of python code global variables that did not exist in the code may be created.

Importing modules
Change syntax of  statement slightly and try again:

Printing globals
To view the contents of  simply enter: While the above information is 100% accurate, without formatting it's not very helpful.

Basic formatted output can be accomplished with: The above adds variable 'v' and it does not show which of the above values are strings.

Try again using a listcomp:

Global communication
may be used for communication between nested functions. In the code below,  is defined within and  is defined within

By means of    can access the local variables of both   and Notice that:


 * is local to  and


 * is local to

locals
This expression has the appearance of a function but it returns and behaves like a dictionary representing the current local symbol table. At module level, globals and locals are the same dictionary. This means that, outside the body of a function,  and   are the same. Within the body of a function assumes its individual identity.

Outside a function
Invoke python on Unix and display  and The above operations show that, outside a function, a change in  is immediately reflected in   and vice-versa.

Inside a function
The code below shows that, within a function,  is local.

Variable name duplicated
The following code shows that it's possible to have the same variable name in both  and with different values:

Inside a listcomp
Within a listcomp  is local to the listcomp:

Assignments



 * In your python code experiment with statements like,   and the same for   or   instead of   Use   so that you can see exactly what you have imported.

Further Reading or Review

 * Previous Lesson: Variable Scoping
 * Functions This Lesson: Built-in Functions
 * Next Lesson: Modules
 * Course Home Page