Python – Libraries / Modules / Functions, Built-in VS Imported, and a demo of importing libraries into a Py 3 program!

SysExit1

Above is a program I will walk through at the bottom of this shorter post, as this is not a robust review of all Libraries / Modules importing just yet.

With that I will get right into it!

What is a Library / Module in Python, and what is Built-in VS Imported?

The functions / expressions / etc that I have been using so far are subsets of “Modules” which are also subsets of what is called a Python Library, as shown in the below graphic of the Built-in Modules on Pythons website which can be found here.

PyLi

Modules refer to the function set being run in a program as will be shown here, that when importing a library (like “import math” / “import random” / “import sys”) will bring its functions into the program for use – Sometimes Python Programs (entire chunks of code) are referred to as Modules because all their underlying functions at work in the program are a further subset of that “Module” whether Built-in or imported.

I will use the “Import Random” here to add some chaos to the Range function

When the “import random” starts the program, you can then use its functions along the way, as seen below I actually “Debugged” this one rather than played it as I’ve not used this feature much, but it demonstrates the program working all the same:

ModPy

I was hoping to see a little more ‘debug’ output from the import of the new library, but it doesn’t appear so, however at the bottom you can see a random output of 5 integers from the Range entered in the print statement of 1, 10 or integer range 1-10.

Some different Libraries that can be imported include:

  • Random
  • Sys
  • Os
  • Math
  • Lets of others at the link above to Pythons official page!

These do not need to be imported prepending the chunk of program to use the imported library either, they can be imported in the first lines and is good practice to do so.

A more intricate example importing multiple Libraries / Modules demonstrated!

I have modified my “While” Loop program to include an option to exit the program, rather than continuing to go through Iterations of Name / Password input, and one huge shout out for Visual Studio Code as it auto-suggested Line 4 to fix the “response = input()” as that is apparently a function I had to import and didn’t know it!

(I bet I could have used a different word to avoid the import, but decided to use it)

This program edit took a bit to get the line order correct, so that the Name portion still immediately loops (because of ‘continue’), and it will still repeat the entire program if prompted, however it will now give an option to exit – And say Bye See Ya if you do!

Here is the new program:

SysExit1

Important Note – sys.exit() ends with a closed parenthesis to simply call the function to execute, no input needed, it just flags the function to be executed!

I’ve adjusted the end statement of the While Loop to include an option to either type exit to quit the program, or hit enter (or really anything) to start another “Iteration” of the program, so it will still function the same as before except now the user can choose to exit the program!

With that lets give it a demo to see how it responds to different input:

SysExit2

Beautiful 🙂

Had I typed “LoopedBack” for the password it would have taken that with the “Access Granted” output, though it doesn’t actually lead to anywhere so the program just ends as seen in a previous post.

To wrap up this discussion of Library / Module importing

Visual Studio Code is an amazing and powerful tool when learning how to code!

I just can’t suggest it enough to help learn Python or any programming language, there are endless resources online for help, but VSC proves to be great training wheels!

It takes a bit of work to get the lines correct for the code to exit at the correct time, Loop where it needs to, however this is a great way to either get the user to input exit or I could have made some kind of other criteria to call out the sys.exit to immediately stop the program from continuing.

Until next time!

Leave a comment