Changes

Jump to: navigation, search

OPS435 Python Lab 3

237 bytes added, 09:26, 21 January 2020
no edit summary
<font color='red'>
'''** DO NOT USE - TO BE UPDATED FOR CENTOS 8.0 **'''
</font>
= LAB OBJECTIVES =
:In previous labs, you learned some programming tools in order to make your Python scripts '''more functional''' and allowed your Python script to run differently based on different data or situations. These tools included '''objects/variables''', '''condition statements''' and '''loops'''. The utilization of these basic tools not only apply to Python scripts, but basically all programming languages including interpreted (including '''Perl scripts''', '''Bash Shell scripts''', '''JavaScript''', etc ) and compiled languages (including '''C''', '''C++''', '''Java''', etc).
:In this lab, you will learn '''functions''', '''lists''', and '''loops''', with the primary focus on creating reusable code.
= INVESTIGATION 1: CREATING THE SIMPLEST 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 OPS235 course: if a supporting library is missing, the program would not be able to run the called function.
:Usually, a '''function''' will '''contain programming code''' in some part of the python file (most likely near the top of the file, before the main program). We refer to that as a '''"function declaration"'''.
== PART 1 - How User-Defined Functions are Declared and Run ==
:Functions may be designed :* '''not to accept arguments or return a value''', designed * to '''not accept arguments but return a value''', designed * to '''accept arguments and not return a value''', * or designed to '''both accept arguments and return a value'''. In this investigation, will we will focus of on creating functions that either do NOT return a value, or return a value.
'''Functions and Strings'''
</source>The final '''return "ok"''' will only take place if a previous return has not taken place before it. Once return has been used in a function, the function immediately exits and returns the value.
:#Call describe_temperature like this to confirm the results:<source>
print(describe_temperature(50))
# Will return 'hot'
print(describe_temperature(20))
# Will return 'perfect'
print(describe_temperature(-50))
# Will return 'cold'
print(describe_temperature(25))
# Will return 'ok'
print(describe_temperature(10))
# Will return 'ok'
</source>
:'''Perform the Following Instructions:'''
:#Create the '''~/ops435/lab3/lab3c.py''' script. The purpose of the script is to have a single function that can perform addition, subtraction, or multiplication on a pair of numbers. But the function will allow us to choose exatly what operation we are performing on it when we call it. If the operate function does NOT understand the operator given, it should return an error message(e.g. calling the function to 'divide' two numbers).
:#Use this template to get started:<source lang="python">
#!/usr/bin/env python3
:#Create a new python file for testing.
:#Import the '''''os''''' module in your python file.
:#You can issue operating system commands by using the '''system()''' function. Try it:<source lang="python">
os.system('ls')
# Notice how the long line below is wrapped to fit on one screen:
print("List length is " + str(length_of_list ) + ", smallest element in the list is " + str(smallest_in_list ) + ", largest element in the list is " + str(largest_in_list))
</source>
print(list_of_numbers)
print(new_list_of_numbers)
</source>The above is just one example of a quick use of for loops mixed with lists. But be careful when passing lists into functions. When you give a function a list as an argument, it is the actual list reference and NOT a copy. This means a function can change the list without making a new list. While you do have to be careful , this can also be useful, a . A function can modify any given list, ''without '' have to return it.<br><br>
:#To demonstrate, run the following code:<source lang="python">
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
1,760
edits

Navigation menu