Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 3

No change in size, 07:48, 31 May 2017
PART 1 - Using Functions
return greeting
</source>
:#The different between returning a value and printing a value, returned values can be caught and stored for later use. Once the returned value has been stored, it can be printed, manipulated, compared in if IF statements, and more. Below will cover how to store a returned value.<source>
text = return_text_value()
</source>
return num3
# This IF Statement may seem cryptic and unclear. Any code written under this if IF statement will be run when you run your script.
if __name__ == '__main__':
text = return_text_value()
python3 ./CheckLab3.py -f -v lab3a
</source>
:#Before proceeding, make certain that you identify any and all errors in lab3a.py. When the checking script tells you everything is ok OK before proceeding to the next step.
:#Start up ipython3 shell again.<source>
ipython3
</source>
:#The if IF statement line found in lab3a.py is special if IF statement. It allows scripts to be imported into other python environment while only defining the functions, this powerful feature allows for python scripts 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 IF statement works, run the lab3a.py script. The output will show that all the code found under the if IF statement was run. <source>
run lab3a.py
</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 IF statement. Now that the script has been imported, we can run functions previously written.<source>
text = lab3a.return_text_value()
text
13,420
edits