Advertisement
jspill

webinar-functions-2020-05-15.py

May 15th, 2021 (edited)
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. # Webinar: Functions + Methods
  2.  
  3. # DEFINE it once
  4. # CALL it as many times as needed
  5.  
  6. # Know the difference PARAMETER and ARGUMENT
  7. # the value for a parameter variable is given by a particular argument on a specific call
  8.  
  9. # function definition, Gallant
  10. def squareThis(num):
  11.      return num * num # we don't need to give the parameter a value, just use it
  12.  
  13. # RETURN means the function call itself evaluates to the value returned. This makes the function very useful!
  14. # PRINT() just shows us output so we can see it right now.
  15.  
  16. # On the Pre + OA, do whichever the questions asks you to do.
  17. # I like to say "Return by default, unless the question specifically asks you to print."
  18.  
  19. # call the function
  20. squareThis(5) # arg of 5, should get a result of 25
  21. print(squareThis(5)) # print() the call so I can see it
  22. print(squareThis(6)) # arg of 6, should get us 36
  23.  
  24. # don't be Goofus
  25. def squareSomething(num):
  26.     num = 5 # don't do this! the calls provide values
  27.     print(num * num) # this will always be 25!
  28.  
  29. print(squareThis(10)) # Gallant will be right, with 100
  30. print(squareSomething(10)) # Goofus hopes you like 25
  31.  
  32.  
  33. x = squareThis(3) # because the function RETURNS a value, it can be used in assignment...
  34. print(x)
  35. y = squareThis(4) + 5 # ... and as part of more complex expressions
  36. print(y)
  37.  
  38. # Ch 8 Task 1
  39. #Task 1:
  40. #Complete the function to print the first X number of characters in the given string
  41. def printFirst(mystring, x):
  42.     # x = 3 # don't do this!
  43.     print(mystring[0:x])
  44.  
  45.  
  46. printFirst('WGU College of IT', 3)   # expected output: WGU
  47. printFirst('WGU College of IT', 11)  # expected output: WGU College
  48.  
  49. # The class METHODS you'll be learning over the later chapters are themselves FUNCTIONS. They're functions that "belong" to that type of object
  50. myList = [2, 4, 7, 5, 9]
  51. print(dir(list)) # see the methods of the list class
  52. help(list.sort) # see help on this specific method
  53. myList.sort() # The list class sort() method orders the list. This method doesn't return a value, but other list methods do.
  54. print(myList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement