Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Webinar: Functions + Methods
- # DEFINE it once
- # CALL it as many times as needed
- # Know the difference PARAMETER and ARGUMENT
- # the value for a parameter variable is given by a particular argument on a specific call
- # function definition, Gallant
- def squareThis(num):
- return num * num # we don't need to give the parameter a value, just use it
- # RETURN means the function call itself evaluates to the value returned. This makes the function very useful!
- # PRINT() just shows us output so we can see it right now.
- # On the Pre + OA, do whichever the questions asks you to do.
- # I like to say "Return by default, unless the question specifically asks you to print."
- # call the function
- squareThis(5) # arg of 5, should get a result of 25
- print(squareThis(5)) # print() the call so I can see it
- print(squareThis(6)) # arg of 6, should get us 36
- # don't be Goofus
- def squareSomething(num):
- num = 5 # don't do this! the calls provide values
- print(num * num) # this will always be 25!
- print(squareThis(10)) # Gallant will be right, with 100
- print(squareSomething(10)) # Goofus hopes you like 25
- x = squareThis(3) # because the function RETURNS a value, it can be used in assignment...
- print(x)
- y = squareThis(4) + 5 # ... and as part of more complex expressions
- print(y)
- # Ch 8 Task 1
- #Task 1:
- #Complete the function to print the first X number of characters in the given string
- def printFirst(mystring, x):
- # x = 3 # don't do this!
- print(mystring[0:x])
- printFirst('WGU College of IT', 3) # expected output: WGU
- printFirst('WGU College of IT', 11) # expected output: WGU College
- # The class METHODS you'll be learning over the later chapters are themselves FUNCTIONS. They're functions that "belong" to that type of object
- myList = [2, 4, 7, 5, 9]
- print(dir(list)) # see the methods of the list class
- help(list.sort) # see help on this specific method
- myList.sort() # The list class sort() method orders the list. This method doesn't return a value, but other list methods do.
- print(myList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement