Whether we want some help for a module or want to quickly test a piece of code, Python interpreter is of great help. And in this article we are going to cover different ways where Python interpreter comes handy. In previous articles we have:

So, what are you waiting for? start your interpreter and follow along as we talk about:

  • getting help from the interpreter
  • import a module
  • listing all the available modules
  • executing some Python code

Let’s get started!

help()

The very first helpful function that I want to talk about is the builtin help() function. It comes from the __builtins__ module and is of great help. There are two ways to use help interactive help or help about object.

Interactive Help

To start up make sure that you have the interpreter ready. Now, type help() and press the return key. You should now see a screen similar to this one:

Interactive help from Python Interpreter

Interactive help from Python Interpreter

The help> shows that our interactive help is ready to help us. Now we can use it to get help for different modules and functions. Let’s give it a try, and search for math and math.pow as shown below:

We can exit the help document and the interactive help by pressing the q key and then pressing return key.

Help about Object

We often want quick help on some module rather than entering the interactive help,  and that’s where help about object comes handy. So, when our interpreter is ready we can get help about a module or function by using the help(<object>) function.

To get help for math.pow we first need to import the math module and ask for help on the pow function. Please refer the code below:

Please note that we have to import the math module first, otherwise we get an error as shown below:

 

There is one more way to get help on a specific module. We can find all the available modules using the help("modules") function call. And it would return a result something like this:

help("modules")

help(“modules”)

It is obvious that the list shown in the image above is not complete. Okay, now that we know about the available modules, we can now get help on them and this time we are not even going to import one.

Yes, we can get the details of a module without importing it, and to do so we use the help("module_name") function, as shown below:

 

import

The import command helps you to import a module that is not currently available. By default, commonly used modules like math, random, web, datetime etc. are not available and ready to use. To use these modules or any other module we first need to import that module and then use it. You can do so simply using the import <module> command, as we did earlier with our help function.

Think of a scenario where you have installed flask, however, while running your app you are getting ModuleNotFoundError. That’s where import command comes to rescue. If the module import is successful, nothing is returned from the command. But if the import fails, we get an error as shown below:

 

dir()

Another helpful function that I want to talk about is the builtin dir() function. Similar to the dir command on Windows/DOS, the dir function, when called without an argument, returns you the names of different modules in the current scope.

When used with an object, it returns an alphabetised list of names comprising (some of) the attributes of the given object, and of attributes reachable from it. The help(dir) defines it as:

If the object supplies a method named __dir__, it will be used; otherwise the default dir() logic is used and returns:
for a module object: the module’s attributes.
for a class object: its attributes, and recursively the attributes of its bases.
for any other object: its attributes, its class’s attributes, and recursively the attributes of its class’s base classes.

The image below shows the result of dir(__builtins__) function call:

dir(__builtins__)

dir(__builtins__)

When we import any module, say math module, then we can see those modules in the response or result of the dir() function. Also, if we have some objects created in that scope then we will see those objects as well.

 

Executing Code

Now that we do understand how to get help from the interpreter, it’s time to see how we can use that help and execute some code for quick testing. So, let’s create a list of integers and then print the square of each number using the math.pow function.

Let’s start the Python interpreter and create a list of integers and assign it to the variable numbers:

The type(numbers) function return the type of the variable or object reference. Let’s now import the math module, iterate over the list of numbers and print the square of each one, as shown in the code below:

If we mess something up, the interpreter also gives us detailed errors as shown below:

 

Summary

The article focuses on getting us familiar and comfortable with the Python interpreter, whether it’s about getting some help or quickly testing a piece of code. Undoubtedly the interpreter is where most beginners start to learn Python, and the more we use more comfortable and efficient we get with it. In the next article, we will be talking about pip and some of its most useful commands.

Categories: Python

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.