Subtitles section Play video Print subtitles The day will come when you are stranded in space and need to write a Python script. It’s unlikely you will have internet access, so if you need help, you’ll be on your own. To be independent, you need to learn about Python’s interactive help features. With these, you can quickly learn about classes, modules, functions… Remember, fortune favors the prepared mind. To begin, start the Python interpreter… The first function you need to know is the d-i-r function. This is short for “directory”. When you press enter, Python displays a list of available objects. When you first start the interpreter, there are 4 standard objects. Today we will focus on the first one: built-ins. This is a module that contains a collection of common objects which are always available to you. To see the list of built-in objects, view the directory for the built-ins module. To do this, call the directory function, and this time pass it the built-ins module name. This list contains dozens of functions and types ready for use. To learn about one of these objects, you call the help function with the name of the object. As an example, let’s learn about the pow function. This displays the help documentation for the pow function. The first line confirms it is a function in the built-in module. The following lines show how to use the function. There are 3 inputs listed: x, y and z. The third input is inside brackets, which means it is optional. The first two inputs are not inside brackets. That means they are required. The arrow points to the return type for this function. pow returns a number. Below the usage is a detailed description of the function. To test the pow function, let’s raise 2 to the 10th power. It works! From the description, we see an equivalent way to perform this calculation is to use a double asterisk. We get the same answer… Let’s see another example. First, display the directory of builtin objects. This time, we will learn about the object called hex. Like before, we will use the help function. The first line confirms that hex is a built-in function. You do not need to import anything to use this function… Next, we see that the input to the hex function is a number, while the output is a string. Let’s test this function. The hexadecimal representation of the number 10 is ‘a’. And that is what we get. Notice that this value is a string, as specified in the help text. You can tell it is a string because it is surrounded by quotes. Also note that hexadecimals in Python begin with 0x. If you ever need to convert a hexadecimal back to a regular decimal, simply type in the hex value. Be sure to not use quotation marks so Python interprets it as a number, and not a string. There are many more modules beyond built-ins. Think of a module as a folder that contains other Python objects. To see a list of available modules, call help(“modules”). To learn about a module and see what objects are available, you must first import it. We will illustrate this by importing the math module… Once you do this, you can check that it is available to you by calling the directory function. See how the math module now appears? It is ready for us to use. Let’s use the directory function to see what is inside the math module. Call the directory function with the name of the module. Be careful you do not use quotes. We want the objects for the module, not a string. We will investigate the radians function. Watch what happens if we try to display the help text: The reason we get a NameError is because the radians function lives inside the math module. To view the help, you must specify the path to the function: math.radians We see the function converts degrees to radians. Let’s test this. Recall that 180° equals π radians. Once again we got a NameError. The reason is the same as before. We need to specify the path to the function in order to use it. It works! Quick note - there is a way to import a function so you do not have to type the full path. This time-saving feature will be discussed in a separate video on importing modules. The next time you are lost in space, don’t panic. The interactive help features of Python will make sure you can stop worrying about your dwindling resources, and instead focus on writing more Python software.
B1 module directory hex pow string built Interactive Help || Python Tutorial || Learn Python Programming 10 0 林宜悉 posted on 2020/03/06 More Share Save Report Video vocabulary