Changes

Jump to: navigation, search

OPS435 Python3 Lab 3

234 bytes added, 19: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"'''.
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>
text = lab3a.return_text_value()
print(text)
print(lab3a.return_text_valuereturn_number_value())
</source> You should see the values retuned form all of the function calls now.
:# Download the checking script and check your work. Enter the following commands from the bash shell.<source lang="bash">
:#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]

Navigation menu