Respond to discussions homework

profilesinister670
entd200_response.docx

Respond to minimum 150 Words

After making a few programs it was clear to me that any software that is built using python would be a huge amount of code.  I know a little bit about this because I have a friend that works as a code editor.  He basically finds problems and fixes them.  If you had a piece of code with a million lines in it and it didn’t run, you would want to break it down into functional groups in order to isolate the problem.  Likewise if you have developed a program that contains a few hundred lines of code that performs a particular task and you need to use it again, you would not want to rewrite that code.  Modules is the solution to this problem.  If you saved that hundred lines of code on your computer you can simply access that work by using the import statement.

If you want to start using modules you need might make some original code or use one of the files you already have on your computer.  Beginning Programming with Python for Dummies uses the example:

Def SayHello(Name):

                Print(“Hello “. Name)

                Return

Def SayGoodbye(Name):

                Print(“Goodbye “, Name)

                Print(“Goodbye “, Name)

                Return

 

Once that file is saved, lets call it HelloGoodbye.py, it can be accessed with the import statement.

Import HelloGoodbye

Dir(HelloGoodbye)

Now that you have built a module, your code can become more efficient and affective.  Having prebuilt code imported into a program will make your code easier to look at visually and understand what you are working on.  However, someone else might not understand all of the prebuilt modules.  This can become an issue when collaborating with multiple developers on a project.

-Richard

Respond to minimum 150 Words

Modules are one of the basic foundations to understanding how programming works. They can save you countless amounts of time in writing out repetitive code. But before I explain how to use a module, I'll give a basic description of one. Essentially, a module is a folder that you store pre-written code into for future use. For example, if you wanted to test out some function you saved into your BP4D folder you can do something like this:

import .os

os.chdir("C:/Users/Kevin/Desktop/BP4D")     #This is where you would put exactly where the module is on your                                                                                               #computer.

import MyLibrary      #This is where you would type the name of the file / module you want to import from the folder.

                                     #For this example, "MyLibrary" is my module I am importing. 

 

If you don't receive an error code then you imported your module correctly. It's as simple as that. Now you can see what functions or attributes are inside your module by doing something like this:

dir(MyLibrary)            #Don't forget, you need to type the name of your own module inside the parenthesis.

 

After you verify everything was imported properly, you can use them as you wish. 

But Kevin, you only showed us how to import, but what about creating them? Great question. Allow me to show you. 

Open up a new Python Shell window. After you open it up, you will need to open a new window to where you can type your code freely and save it. Click on file -> new file. This will open up an untitled Python window.

Now create any function you wish - I will show you a basic function that will just print "Hello World!" on the screen:

def HelloWorld ():

     print("Hello World!")

Now save this file inside your BP4D folder and title it whatever you want. For this example, I titled it : HelloWorld. 

You have just created and documented a function / module for future use!

 

If you wanted to use this "HelloWorld" module then you can just by simply importing it following the directions I gave above. Then by typing:

HelloWorld.HelloWorld()                          #The first part is the module name, the second part is the function name

It will now print "Hello World!" as expected. 

 -Kevin