Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 3

521 bytes removed, 21:58, 27 January 2018
PART 1 - How User-Defined Functions are Declared and Run
:'''Perform the Following Steps:'''
:#Launch your Centos VM, open a shell terminal (as a regular user) and start Create a new ipython3 sessionpython file for testing code in this section.:<source>ipython3</source>#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, it does not run the code you write. Functions, just like '''if''' statements, must have all code under them indented.<br><br>:# Enter the following code in your ipython3 shell:<source lang="python">
def hello():
print('Hello World')
print('Inside a Function')
</source>Remember to press '''ENTER''' a second time to return to the ipython prompt. You may :#Executing your file you should have noticed that nothing happened. Well actually, something did happen... the function called '''hello(''') has been defined and stored in internal memory in order for it to run when called by its function name. Now that our function was created, we can use it over and over. <br><br>:#To execute the code inside the function, run the function name with "'''()'''" '''brackets''' at the end of the function name.<br>Try running the '''hello()''' function by name three times by issuing the following in the ipython3 shelllike this:<source lang="python">
hello()
hello()
return greeting
</source>
:# Call the function in your ipython3 shell by issuing the followinglike this:<source lang="python">
return_text_value()
</source>One major difference between a function '''returning a value''' and simply '''printing a value''' is that '''returned''' values can be caught and stored in variables used in the program (that called the function) for later use. Once the returned value has been stored, it can be printed, manipulated, compared in IF statements, etc. Below will cover how to store a returned value.<br><br>
:#Enter Notice that this syntax looks just the following call to the input() function which you've used in the ipython3 shell to see returning a variable's value workslast lab:<source lang="python">
text = return_text_value()
</source>
:'''Perform the Following steps:'''
:#Issue Define the following in your ipython3 shellreturn_number_value() function:<source lang="python">
def return_number_value():
num1 = 10
</source>
:#Now, issue the following in the ipython3 shell to And call the '''return_number_value()''' functionit:<source lang="python">
number = return_number_value()
print(number)
print(return_number_value() + 10)
</source> What do you notice?<br><br>
:#Now, issue the following which use the print() statement to display both strings and numbers:<source lang="python">
number = return_number_value()
print('my number is ' + number)
</source> What do you notice? You should notice a warning message. This occurs because the returning value is a '''number''' and NOT a '''string'''! Combining numbers and strings in a statement (such as '''print()''') can cause errors. The error message should appear similar to the one displayed below: <source>
---------------------------------------------------------------------------TypeError Traceback (most recent call last):<ipython-input-24-d80d5924146a> File "test.py", line 2, in <module>()----> 1 print('my numbr number is ' + number) TypeError: Cancannot concatenate 'str't convert and 'int' object to str implicitlyobjects
</source>
:#If a number needs to be combined with a string, use the '''str()''' predefined function that was discussed in a previous lab in order to convert the returned number into a string:<source lang="python">