Changes

Jump to: navigation, search

OPS435 Python Lab 3

42 bytes added, 03:13, 30 May 2017
no edit summary
= LAB OBJECTIVES =
This lab shows investigates methods of for reusing codewith both functions and loops.
= INVESTIGATION 1: FUNCTIONS =
== PART 1 - Using Functions ==
'''Functions and Strings'''
 :'''Perform the following 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')
print('Inside a Function')
 
 
</source
 :#Now that our function is created we can use it over and over. To execute the code inside the function, run the function name with "'''()'''" '''brackets''' at the end of the function name.<source>
hello()
hello()
hello()
</source>
 :#Next, create a function that returns some data after the function runs. This function does not print out any text, it creates new variables and at the end returns the value.<source>
def return_text_value():
name = 'Terry'
return greeting
</source>
 :#The different between returning a value and printing a value, returned values can be caught and stored for later use. Once the returned value has been stored, it can be printed, manipulated, compared in if statements, and more. Below will cover how to store a returned value.<source>
text = return_text_value()
</source>
 :#Now the returned text from the function has been stored in the variable "'''text'''". It can be used like any string value now.<source>
print(text)
</source>
'''Functions and Integers'''
:'''Perform the Following steps:'''
:#In this next example the functions will be returning integer values instead of text. There is not a big difference, but when returning number values, care needs to be taken if you try combining it with a string.<source>
def return_number_value():
num1 = 10
198
edits

Navigation menu