Open main menu

CDOT Wiki β

Changes

OPS435 Python3 Lab 3

911 bytes added, 18:14, 28 February 2020
m
INVESTIGATION 1: CREATING SIMPLE FUNCTIONS: fix typo
:Usually, a '''function''' contains '''programming code''' and most likely placed near the top of a python file, and before the main processing section.
: When a Python program is run, the '''function's code is read into the corresponding process memory''', waiting to be executed when the function is called. Until a Function is specifically called, its code will sit still in the memeorymemory.
:When creating programs that define and use functions, '''a large programming task can be broken-down into smaller elements''' (or '''modules'''). This is why creating programs that use functions is referred to as '''"modular programming"'''.
:#Create a new python file for testing code in this section.
:#Whenever you want to create a function, you must start with the keyword "'''def'''". The '''def''' keyword is used to start the definition of the function, and it should be followed first by the name of the function, and secondly arguments (if there is any) it expected, and finally a ":". When the python interpreter see the function declaration and its code, it does not run the code you write right wary. Functions, just like '''if''' statements, must have all code under them indented.<source lang="python">
def hello():
print('Hello World')
hello()
hello()
</source>You should notice that the function just does the same thing over-and-over no matter how many times your call the function by name. By the wayOn one hand, that is OK. On the other hand, you may want to create and use a function to do somethinguseful, like perform error checking or some other task that report something back to the '''caller''' for further processing. For example, return a '''true''' or '''false''' value if the by an error checking function that was called was detected no errors or detected to check where there is an error. But let's stick to some simple examples first, before tackling more complex use of functions. In Python, when a function does not return a value, it the Python interpreter will automatically return a special Python object called '''None'''. '''None''' is a Python 3.x keyword which is used to indicate that there is "nothing". 'None' is not the same as an empty object, like an empty string. Let update the above Python script to the following version:<source lang="python">
def hello():
print('Hello World')
print('Stuff return from hello():',my_stuff)
print('the object my_stuff is of type:',type(my_stuff))
</source>You can assume that there is a hidden '''return''' statement at the end of any function if it does not have one implicitly.The following python script should produce the same result as the one above:<source lang="python">
def hello():
print('Hello World')
== PART 2 - Function that does not take argument but returns a string ==
:#Let's create a function that '''returns''' some data to the caller after the function is called. This function does not print out any text: instead; it creates new objects and at the end , returns one of the object named '''greeting''', which is a string object containing 'Good Monring Terry'.<source lang="python">
def return_text_value():
name = 'Terry'
text = return_text_value()
</source>
:#Now the returned string from the function has been assigned to an object named "'''text'''". It can be used like any string object now.<source lang="python">
print(text)
</source>
print(text)
lab3a.return_number_value()
</source> You should notice that although your script runs without any error, however, it only prints out text returned from the return_text_vaule() function. It does not print out the value returned from the return_number_value() function. Modify the last line like this:<source lang="python">import lab3atext = lab3a.return_text_value()print(text)print(lab3a.return_number_value())</source> You should see the values retuned form all of the function calls should now work.
:# Download the checking script and check your work. Enter the following commands from the bash shell.<source lang="bash">
cd ~/ops435/lab3/
<br><br>
== PART 2 - Launching Linux command and controlling its process Commands with builtin functions in os and subprocess modules ==:The remainder of this investigation will allow you to explore the behaviour of some functions from the os and subprocess modules. All of these functions will take function argument(s), some will return value(s), and some will not. The functions we are going to explore are: os.system(), os.popen(), and subprocess.Popen(). All of them can be used to launching launch any Linux commands and can interact with the their processesvia the standard input, standard output, and standard error data channels.
'''Perform the Following Steps:'''
import os
whoami_return=os.popen('whoami')
whocmi_contents whoami_contents = whoami_return.read()
print('whoami_contents:',whoami_contents)
</source>What conclusion would you draw from the about python script?
:#Try another one:<source lang="python">
ipconfig_return = os.popen('ipconfig')
ipconfig_contents = ipconfig_return.read()print('The contents of ipconfig_return:',ipconfig_returnipconfig_contents)
</source>
<br><br>As you may recall from lab2, you issued '''import sys''' to import special object from that built-in module. You can also import the subprocess module to load a function called '''Popen()''' which will allow you to have better control of the output produce by Linux commands like 'ls', 'whoami', and 'ifconfig', etc. <br><br>
:# What is the purpose of using functions in a Python script?
:# Write Python code to define a function called '''greetings()''' that when called will greet the user by name and on the next line display the current date. [hint: use the os.system() function to display the current date.]
:# Why is it useful for functions to accept '''arguments''' passed-up upon function execution?
:# What is the purpose of the '''import''' command? What can be the consequence if the import command is not used prior to running a function by name?
:# Write Python code to define declare a function called '''join()''' that excepts accepts two arguments which will be be stored as the variables called '''word1''' and '''word2''' respectively during the execution of the function.
:# What is the command to return a value from a function?
:# What is the purpose of the '''system()''' functionin the os module?:# What is the purpose of a '''list'''object?
:# Assume that the following list has been defined: '''mylist = [ 'apple', 1, 'grape', 2, 'banana', 3, ]'''<br>Based on that, what will the following contain?<source lang="python">mylist[0]
mylist[3]