Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 3

25 bytes added, 09:56, 2 June 2017
no edit summary
<br><br>
= INVESTIGATION 1: USING CREATING SIMPLE FUNCTIONS = 
:A very simple definition of using '''functions''' is to create and reuse '''smaller programs within a larger program'''. In programming languages such as '''C''', '''C++''' and '''Java''', commonly used functions are pre-packaged in '''Libraries'''. This relates to dependency issues that were discussed when compiling C programming code in your OPS25 course: if a supporting library is missing, the program would be able to run the called function. We do not go into detail involving Libraries for this course since Python is an '''interpreted''' (i.e. not a compiled) language.
:Functions may be designed '''not to accept arguments or return a value''', designed to '''not accept arguments but not return a value''', designed to '''accept arguments and not return a value''', or designed to '''both accept arguments and return a value'''.
== PART 1 - Using Functions ==
'''Functions and Strings'''
<br><br>
= INVESTIGATION 1: CREATING ADVANCED FUNCTIONS = == PART 2 1 - Providing Functions With Arguments ==
:Functions can pass-up arguments to be used for processing. In this section, you will learn how to define functions that accept arguments and learn how to perform function calls that pass up arguments for processing (such as mathematical operations or testing conditions (eg. error-checking)).
lab3b.sum_numbers(10, 5)
15
 
lab3b.sum_numbers(25, 25)
50
 
lab3b.subtract_numbers(10, 5)
5
 
lab3b.subtract_numbers(5, 10)
-5
 
lab3b.multiply_numbers(10, 5)
50
 
lab3b.multiply_numbers(10, 2)
20
:::6. Before proceeding, make certain that you identify any and all errors in lab3c.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 32.
<br><br>
== PART 3 2 - Running System Commands with Subprocess ==
This last part of the investigation will give you access to system commands within your python scripts. While we are able to run some bash commands inside the ipython3 environment, these do not transfer over into the python code we write. It is not usually a good idea to run system commands in Python, this makes your Python code less portable and makes it require a speicifc operating system or a system that has those commands available. There is also the case of security, allowing python to execute commands on the system can be a security problem if care isn't taken. For these reason you should only use subprocess and the system commands as a last resort and stick to Python code only.
:::5. Before proceeding, make certain that you identify any and all errors in lab3d.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 INVESTIGATION 23.
= INVESTIGATION 2 3 - LISTS =
Lists are ones of the most powerful data-types in Python. A list is a series of comma separated values found between square brackets. Values in a list can be anything: strings, integers, objects, even other lists. In this section we will introduce lists and how to use them effectively, we will come back to lists again in later labs.
13,420
edits