2. CS151 Spring 2019
Goals
● Show how to SSH using NoMachine
● Show how to startup Spyder3
● Explain functions in python3
● Explain range(start, stop, step) function
● Explain __name__ == “__main__”
3. CS151 Spring 2019
Functions in Python3
Functions , like math, takes an argument(s) and returns zero or more values.
Structure
def funcName(arg1, arg2):
def : keyword to specify define a function
funcName: you can name your function anything but making it self describing
helps. (i.e. isPalindrome)
arg1,arg2: Arguments you pass to the function can be zero or more
4. CS151 Spring 2019
Functions cont ...
Structure cont…
def funcName(arg1, arg2):
Statements
return( value )
Your function can contain any number of statements to do what ever the function
is designed to do.
return(): This is what you return to the calling program.
5. CS151 Spring 2019
Example function
def add2numbers(myInt1, myInt2):
# Trivial example
tempInt = myInt1 + myInt2
return(tempInt)
This is trivial and pedantic to show an example, it could easily be written with one
line:
return(myInt1 + myInt2)
However, your programs will most likely not be so simple.
Also there are no error checking, this function assumes you will use it correctly.
This can be a bad assumption and you should check that myInt is actually an
integer.
6. CS151 Spring 2019
The range function in Python
You can always go right to the source documentation and lookup what a python
function does: https://docs.python.org/3/
Top right hand corner give a search box: range [enter]
class range(start, stop[, step])
start integer, remember we have 0 based indexing.
stop integer, can be an integer, boolean or function (i.e. len(myString))
step what increment or decrement to use, usually 1 or -1 but could be anything.
7. CS151 Spring 2019
Range example
def printString(myString):
for myIndex in range( 0, len(myString)-1, 1):
print(myString[myIndex])
This function will use a for loop to print from 0 to string length minus 1,
incrementing by +1 on each iteration.
Why -1 on string length?
Let myString = ‘Palindrome’ len(myString) returns 10 not 9
len(myString)-1 returns 9 which is the last character.
Index 0 1 2 3 4 5 6 7 8 9
string P a l i n d r o m e
8. CS151 Spring 2019
A real example program
A Palindrome is a sentence or word that is the same forward and backward.
String = 123454321
Bad algorithm:
Algorithm for Palindrome:
Reverse the given string and compare to original string
9. CS151 Spring 2019
A real example program, continued
A Palindrome is a sentence or word that is the same forward and backward.
String = 123454321
Good algorithm:
Algorithm for Palindrome:
Copy original string
Reverse string
Compare each string
If they are equal then return True, else return False.
10. CS151 Spring 2019
A real example program, continued
String = 123454321
Best algorithm:
Algorithm for Palindrome:
Reverse string
Loop from end of original string and append to a new string
Compare temp string to original string
If they are equal then return True, else return False.
11. CS151 Spring 2019
Convert to Python
def reverse(myString):
tempString = ‘’ #create an empty string
for myIndex in range(len(myString)-1, -1, -1): # more on this next
tempString += myString[myindex] #copy to new string
return(tempString)
def isPalindrome(myString):
revString = reverse(myString)
if myString == revString:
return(True)
else:
return(False)
#Call isPalindrome
print(isPalindrome(‘123454321’)
12. CS151 Spring 2019
Explanation
The for loop in reverse may look weird.
for myIndex in range(len(myString)-1, -1, -1):
Remember:
So len(myString) returns 10, but our index is 9 so we subtract one to accomodate
0 based indexing.
We stop at -1, since the for loop stops at 0 not -1, the operator is < not <=
Finally our step is decrementing by 1, so -1
Index 0 1 2 3 4 5 6 7 8 9
String P a l i n d r o m e
13. CS151 Spring 2019
More Explanation
We do not need to write the reverse function, however, we will see how we can
call reverse from another python program using an import statement.
How can we make this program better?
Error checking?
Whitespace, punctuation removal?