Changes

Jump to: navigation, search

OPS435 Python Lab 3

207 bytes added, 03:20, 30 May 2017
PART 1 - Using Functions
== PART 1 - Using Functions ==
'''Functions and Strings'''
:'''Perform the following stepsFollowing Steps:'''
:#To start, open ipython and try experimenting with functions.<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.<source>
def hello():
print('Hello World')
</source>
 :#Now try storing the return value of the above function.<source>
number = return_number_value()
print(number)
print(return_number_value() + 10)
</source>
 :#The warning again, because this is a number value, using it with strings can cause errors. <source>
number = return_number_value()
print('my number is ' + number)
TypeError: Can't convert 'int' object to str implicitly
</source>
 :#If a number needs to be combined with a string use the one of the following syntax below.<source>
number = return_number_value()
print('my number is ', number)
print('my number is ' + str(return_number_value()))
</source>
'''Practice Using Functions''':'''lab3aPerform the Following Instructions:''':#Create a new script '''~/ops435/lab3/lab3a.py''' and store the following content inside:<source>
#!/usr/bin/env python3
print(str(number))
</source>
 :#Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3a
</source>
:#Before proceeding, make certain that you identify any and all errors in lab3a.py. When the check script tells you everything is ok before proceeding to the next step.  :#Start up ipython3 shell again.<source>
ipython3
</source>
 :#The if statement line found in lab3a.py is special if statement. It allows scripts to be imported into other python environment while only defining the functions, this powerful feature allows for python scripts to run differently depending on if you run it or import it. Take a look at the code, there is NO reason to run this in ipython3. <source>
if __name__ == '__main__':
print('python code')
</source>
  :#To show examples of how the above if statement works, run the lab3a.py script. The output will show that all the code found under the if statement was run. <source>
run lab3a.py
</source>
 :#This next example will show when the code under the if statement does NOT run.<source>
import lab3a
</source>
 :#When you import a python script it will run all code found that is not indented, including defining all of the functions. But, when importing, python will not run any code under the special if statement. Now that the script has been imported, we can run functions previously written.<source>
text = lab3a.return_text_value()
text
Functions can take arguments that work similarly to Python scripts. In this section, the functions created will be given arguments, these functions will use the given arguments to provide different output.
:'''Perform the Following Steps:''':#Start the ipython3 shell<source>
ipython3
</source>
 :#To create a new function that accepts arguments, provide variable names in the functions brackets. But if a function is given arguments in the definition, it must always get the same number of arguments when you call the function.<source>
def square(number):
return number ** 2
</source>
 :#To square a number in math your multiply using '''number * number''' or use exponents '''number ** 2''' to the power of two. This function takes one argument '''number''', the function will use exponents to multiply the number given by itself.<source>
square(5)
square(10)
square(square(2))
</source>
 :#Providing more than one argument in a function requires the use of commas. Be careful NOT to provide strings or decimals, as you may cause errors.<source>
def sum_numbers(number1, number2):
return int(number1) + int(number2)
</source>
 :#Running functions with multiple arguments is the same. When you put a function as a argument of another function, the inner-most function will run first, and the return value will be used as the argument on the outer function. For example, in this case below, '''sum_numbers(5, 5)''' will return '''10''', and provide square with that value '''square( 10 )'''.<source>
sum_numbers(5, 10)
sum_numbers(50, 100)
</source>
'''lab3bPractice Creating Functions With Return Values''':'''Perform the Following Instructions:''':#The next script is NOT COMPLETE, your job is to make the script do what it askes. It contains 3 functions, one of adding, one for subtracting, and one for multiplying. Make sure each function performs the correct operation and returns a integer value. Create a new script '''~/ops435/lab3/lab3b.py''' with the following contents:<source>
#!/usr/bin/env python3
</source>
::*The script should have a '''Shebang line'''::*The script should have a function sum_numbers(number1, number2)::*The script should have a function subtract_numbers(number1, number2)::*The script should have a function multiply_numbers(number1, number2)::*All functions should accept two arguments::*All functions should return a integer::*The script should contain no errors
::#Sample Run 1:<source>
run lab3b.py
15
</source>
::#Sample Import 1:<source>
ipython3
20
</source>
  :#Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3b
</source>
:#Before proceeding, make certain that you identify any and all errors in lab3b.py. When the check script tells you everything is ok before proceeding to the next step. 
'''Multiple Arguments and IF Statements'''
198
edits

Navigation menu