Concepts / Understanding Functions in Python

Understanding Functions in Python

Python provides built-in help functionality for getting quick information about functions and statements.

  • Programming

A Question at the Interpreter

When you encounter a Python function or statement that you do not yet understand, you do not always need to leave the interpreter prompt to find basic information. Python provides built-in help functionality for getting quick information about functions and statements. This makes the interpreter prompt a useful place to investigate while learning.

Definition

A function is a reusable piece of a program. Python includes built-in functions, such as len and range, that perform useful operations.

Following a Help Request

Consider the source example help('len'). The request begins with help, which is the built-in help functionality. The text len identifies the function about which you want information. Python then displays help for the len function. The important sequence is: request help, identify the function, and read the information Python displays.

Worked example

Requesting information about len

You want quick information about the len function while working at the Python interpreter prompt.

  1. 1
    Make the request

    Use the source example help('len') at the interpreter prompt.

  2. 2
    Identify the subject

    The text len tells Python which function you want to investigate.

  3. 3
    Read the result

    Python displays help for the len function.

  4. 4
    Connect the information to the purpose

    The len function is used to count the number of items.

Result

help('len') gives quick information about len, including that it is used to count the number of items.

Reusable Work in Python

Functions matter because they package work that can be used again. The source describes functions as reusable pieces of programs. Instead of treating every operation as unrelated, you can recognize a function as a named piece of program behavior that can be used when that operation is needed.

Python's built-in functions illustrate this idea. The len function is used to count the number of items. The range function is used to generate a sequence of numbers. Each function supports a particular task, so knowing a function's purpose helps you understand what kind of work it performs.

Help and Action

ExpressionPurpose
help('len')Displays help for the len function
lenNames the function being investigated
len applied to a Python objectUses len for its counting purpose

The help request is about learning what len does; len itself is the function used to count the number of items.

This distinction prevents a common confusion. help('len') is a request for information about len. It does not represent the counting purpose described for len. The function being described is len, while help is the tool used to learn about it.

In the real world

Suppose you are working at the interpreter prompt and remember the name len but not its purpose. Asking for help lets you check the function's role immediately. After reading that len counts the number of items, you have a clearer basis for deciding whether it is relevant to the task you are working on.

Mistakes with Built-In Help

  • Common mistake

    Treating help('len') as the counting operation

    What it looks like

    The help request is confused with the purpose of len.

    Why it fails

    The source example says that help('len') displays help for len, while len is used to count the number of items.

    Do this instead

    Separate the two roles: use help to obtain information, and recognize len as the function used for counting.

  • Common mistake

    Forgetting that functions are reusable pieces of programs

    Why it fails

    Thinking of a function as a one-time instruction hides why functions are useful.

    Do this instead

    Remember that functions are reusable pieces of programs and that built-in functions provide ready-made operations.

  • Common mistake

    Assuming every built-in function has the same purpose

    What it looks like

    Confusing len with range.

    Why it fails

    The source identifies different purposes: len counts the number of items, while range generates a sequence of numbers.

    Do this instead

    Use help when you need to check what a particular function does.

Best practice

When a function name is familiar but its purpose is unclear, use Python's built-in help functionality at the interpreter prompt instead of relying on a guess. Then connect the displayed information to the task the function performs.

Practice the Investigation

EASY

At the Python interpreter prompt, you need to find out what len is for. What built-in help request should you use, what information should you expect it to provide, and how is that different from the purpose of len itself?

Hints
  • Use the source example involving the text len.
  • Separate the act of requesting information from the operation performed by the function.
  • Recall the stated purpose of len and compare it with the stated purpose of range.
Worked example

Checking your reasoning

Explain the roles of help, len, and range without treating them as interchangeable.

  1. 1
    Identify help

    help provides quick information about a function or statement, especially at the interpreter prompt.

  2. 2
    Identify len

    len is the function described in the source as being used to count the number of items.

  3. 3
    Identify range

    range is the built-in function described as generating a sequence of numbers.

  4. 4
    Compare the roles

    The functions have different purposes, and help is the information tool used to investigate them.

Result

help provides information, len counts the number of items, and range generates a sequence of numbers.

Key Takeaways

  1. 1
    Python's built-in help functionality provides quick information about functions and statements.
  2. 2
    The interpreter prompt is a useful place to request this information.
  3. 3
    The source example help('len') displays help for the len function.
  4. 4
    Functions are reusable pieces of programs.
  5. 5
    len counts the number of items, while range generates a sequence of numbers.

At a glance

  • Use Python's built-in help functionality when you need quick information about a function or statement.
  • At the interpreter prompt, help('len') displays help for the len function.
  • Functions are reusable pieces of programs, and Python includes built-in functions for specific tasks.
  • The len function is used to count the number of items.
  • The help request and the function's operation are different: help explains len, while len performs its counting task.

Related concepts

More concepts starting with U