Advertisement
jspill

webinar-functions-methods-2023-04-08

Apr 8th, 2023
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. # Webinar: Functions + Methods in Python
  2.  
  3. # You'll start getting to know functions in Ch 7
  4.  
  5. # A function is essentially a block of code with a name
  6. # that can (or not) accepts inputs as arguments and perform
  7. # some actions accordingly.
  8.  
  9. # Functions should be modular and reusable - a good function has ONE JOB
  10. # We define a function once and call it as many times as needed
  11.  
  12. # We'll write a function to take in a number as square it. The function will RETURN that square.
  13.  
  14. # function definition, Gallant
  15. def squareThis(num): # num is the parameter
  16.     return num * num
  17. # print(squareThis(3))
  18. # print(squareThis(7))
  19.  
  20. # function definition, Goofus
  21. def squareSomething(num):
  22.     num = 5 # no, Goofus! Don't override the parameter!
  23.     return num * num
  24.  
  25. # print(squareSomething(3)) # Goofus hopes you like 25
  26. # print(squareSomething(6)) # on every...
  27. # print(squareSomething(9)) # single call :(
  28.  
  29. # keyword args of print()
  30. # print() --> print(end="\n", sep=" ")
  31.  
  32.  
  33. def makeMore(num): # only function definitions go outside of the if __name__ block
  34.     return num * num * num + num
  35.  
  36. if __name__ == "__main__":
  37.     # solve this Lab, this problem
  38.     myNum = int(input())
  39.     print(f"I bought {myNum} snails and now I have {makeMore(myNum)}!")
  40.  
  41. # print(__name__) # it's "__main__"
  42.  
  43. # print(dir(__builtins__))
  44. print(dir(str))
  45. print(dir(list))
  46.  
  47. # class methods are functions...
  48.  
  49. myList = ["Gilligan", "Skipper"]
  50. myList.append("Mary Ann") # class methods are called "on" an object of the type
  51. myList.append("Professor")
  52. print(myList)
  53.  
  54. # that object of its type is called the "self" and appears as if it were the first argument in the help docstring:
  55. help(list.append) # append(self, object, /) Append object to the end of the list.
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement