i am not a teacher… yet - ttu cae network...

17
Disclaimer This is not a class! It’s just a CyberEagles python seminar I am not a teacher… yet

Upload: phamdien

Post on 28-Apr-2018

222 views

Category:

Documents


2 download

TRANSCRIPT

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

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

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

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

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

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

__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

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

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

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

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

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

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

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

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

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

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

Lab – Hard

• Write a Fibonacci sequence with the help of list comprehensions