I am not a teacher… yet - TTU CAE Network...
Transcript of I am not a teacher… yet - TTU CAE Network...
![Page 1: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/1.jpg)
Disclaimer
• This is not a class!
• It’s just a CyberEagles python seminar
• I am not a teacher… yet
![Page 2: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/2.jpg)
Functions and List Comprehensions
Vitaly Ford
11/18/15
![Page 3: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/3.jpg)
Function
• A module of code
• Accomplishes a specific task
• Takes input, processes, returns a result
• Good to reuse code
• Easier to comprehend what’s going on
• Good for testing small parts of code
![Page 4: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/4.jpg)
Defining a Function
def name (parameters):
here_comes_the_code
return expression
![Page 5: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/5.jpg)
Calling a Function
def division ( a, b ):
c = a / b
return c
result = division ( 64, 16 )
print ( result )
What’s the problem with this function?
![Page 6: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/6.jpg)
__main__
if __name__ == “__main__”:
print ( “This program is being run by itself” )
else:
print ( “I am being imported from another module” )
![Page 7: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/7.jpg)
Parameters
• Passed by reference
def add_elements ( inputList, headElement, tailElement ):
inputList.append ( tailElement )
inputList.insert ( 0, headElement )
return
if __name__ == “__main__”:
mylist = [5, 6, 7]
add_elements (mylist, 4, 8)
![Page 8: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/8.jpg)
Parameters (cont.)
• Required
• Keyword
• Default
• Variable-length
![Page 9: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/9.jpg)
Required Parameters
def add_elements ( inputList, headElement, tailElement ):
inputList.append ( tailElement )
inputList.insert ( 0, headElement )
return
if __name__ == “__main__”:
add_elements ()
Throws an error!
![Page 10: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/10.jpg)
Keyword Parameters
def calc_stuff ( len, width, height, radius, sigma ):
# do stuff with input params
result = len – width + height – radius / sigma
print ( result )
return
calc_stuff ( 5.6, 7, 8, 33, 0.3 )
calc_stuff ( height = 8, sigma = 0.3, len = 5.6, width = 7, radius = 33 )
![Page 11: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/11.jpg)
Default Parameters
def calc_weights ( a, b, c, precision = 0.1 ):
# do stuff
print ( “Precision: ”, precision )
return
calc_weights ( 1, 2, 3 )
calc_weights ( 1, 2, 3, 0.5 )
![Page 12: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/12.jpg)
Variable-length
def sum_them_up ( *allVars ):
sum = 0
for v in allVars:
sum += v
return sum
s = sum_them_up ( 5, 4 )
s = sum_them_up ( 3, -6, 7, 0, 10 )
![Page 13: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/13.jpg)
Global vs Local Variables
result = 100
def mult ( a, b ):
result = a * b
print ( “Inside result: “, result )
mult ( 8, 6 )
print ( “Outside result: “, result )
Want access a global variable inside your function? Do
global result
![Page 14: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/14.jpg)
List Comprehensions
V = [ 2**i for i in range ( 10 ) ][1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
E = [ i for i in range (20) if i % 2 == 0]
L = “I love Star Wars”.split()['I', 'love', 'Star', 'Wars']
K = [ [ i, len (i) ] for i in L ][['I', 1], ['love', 4], ['Star', 4], ['Wars', 4]]
![Page 15: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/15.jpg)
Lab – Easy
• Ask a user to enter usernames; insert them all into a list. Whenever the user enters a keyword “stop” (capitalized or not – should not matter), print everything that is in the list, line by line.
![Page 16: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/16.jpg)
Lab – Medium
• Find all palindromic primes up to 1000
![Page 17: I am not a teacher… yet - TTU CAE Network Blogsblogs.cae.tntech.edu/cybereagles/files/2015/11/11.18.2015.python.pdf · •I am not a teacher… yet. Functions and ... •Accomplishes](https://reader031.fdocuments.in/reader031/viewer/2022022512/5ae40f177f8b9a097a8ef064/html5/thumbnails/17.jpg)
Lab – Hard
• Write a Fibonacci sequence with the help of list comprehensions