Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 3

427 bytes removed, 08:46, 2 June 2017
PART 1 - Using Functions
print(str(number))
</source>
:::2 . Run your lab3a.py script in your ipython3 shell by issuing:<source>
run lab3a.py
</source>You should have seen three lines being displayed: the text "python code", a greeting, and a result of a math calculation.<br><br>'''NOTE:''' The IF statement in the displayed above is considered to be a '''special IF statement'''. It allows '''variables''' and '''functions''' from other python scripts to be '''used in your current Python script (or in the ipython shell) without having to re-define those functions'''. In order to use functions from other scripts, you are required to either issue the import command in your ipython3 shell environment, or use the import command within your Python script itself.<br><br>We can demonstrate the use of re-using functions from another script by simply '''issuing statements from the ipython shell by calling a function that is related to your lab3a.py Python script'''. But care MUST be taken to first use the import command to load in the function declarations from your Python script to your ipython shell environment first.
:::3 . Let's see what happens if we forget to import functions from your lab3a.py script prior to calling a function. Issue the following:<source>
text = lab3a.return_text_value()
</source>You should notice an error indicating xxx:::4 :#Start up ipython3 shell again.<source>ipython3</source>:#The IF statement line found in "name 'lab3a.py ' is special IF statementnot defined". It allows scripts This error occurs since you failed to be imported into other python environment while only defining instruct the functions, this powerful feature allows for python scripts ipython shell to run differently depending on if you run it or '''import it. Take a look at the code, there is NO reason to run this in ipython3.<source>if __name__ == '__main__': print('python code')</source>:#To show examples of how the above IF statement works, run the or "load existing defined functions from your lab3a.py script. The output will show that all the code found under " to the IF statement was runinternal memory. <sourcebr>run lab3a.py</sourcebr>:#This next example will show when ::4. Issue the code under the if statement does NOT run.following:<source>
import lab3a
</source>
:#When you import a python script it will run all code found that is not indented, including defining all of the functions. But, when importing, python will not run any code under the special IF statement. Now that the script has been imported, we can run functions previously written.<source>
text = lab3a.return_text_value()
text
lab3a.return_number_value()
</source> You should notice that all of the function calls should now work. NOTE: since you are in the ipython shell, the import command only will work as long as you are logged into that shell. You will need to use other methods (contained in other Python Scripts (or future ipython shell sessions) to make these function imports persistent, but you will learn about that at a later time.<br><br> :#::5. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3a
</source>
:#::6. Before proceeding, make certain that you identify any and all errors in lab3a.py. When the checking script tells you everything is OK before proceeding to the next step.
Make notes for all of your observations in your lab log book, and proceed to Part 2.
13,420
edits