Advertisement
jspill

webinar-python-functions-methods-2022-04-16

Apr 16th, 2022
1,320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 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. # def myFunction(parameter): # calls provide values into parameter variables
  13. #     # so don't do this:
  14. #     # parameter = 5 # No! We don't assign values to parameters this way
  15. #     newValue = parameter/2 # whatever the function is doing
  16. #     return newValue # return vs print, do one not both
  17. #     print(newValue) # this would not execute, since we already returned
  18.  
  19. def returnATuple(x, y, z): # x, y, z are parameters
  20.     return z, y, x # same as return (z, y, x)
  21. x = returnATuple(1, 2, 3) # 1, 2, and 3 are ARGUMENTS going into each parameter
  22. print("{}: {}".format(x, type(x)))
  23.  
  24. # function definition, Gallant
  25. def squareThis(num):
  26.     return num * num
  27. print(squareThis(3)) # 9? yes
  28. print(squareThis(5)) # 25? yes
  29.  
  30. # function definition, Goofus
  31. def squareSomething(num):
  32.     num = 5 # no, Goofus! Don't override your parameter!
  33.     return num * num
  34. print(squareSomething(3)) # Goofus hopes you like 25
  35. print(squareSomething(6)) # on every...
  36. print(squareSomething(9)) # single call :(
  37.  
  38. # Examples and Student Questions
  39.  
  40. # Ch 8 Task 1
  41. # Complete the function to print the first X
  42. # number of characters in the given string
  43. def printFirst(mystring, x):
  44.     # pass
  45.     print(mystring[0:x])
  46.  
  47. # expected output: WGU
  48. printFirst('WGU College of IT', 3)
  49. # expected output: WGU College
  50. printFirst('WGU College of IT', 11)
  51. printFirst("WKRP in Cincinnati", 4) # add a test call of your own
  52.  
  53.  
  54. # if name = main, a structure you'll see first Lab 7.18
  55. # this structure helps us separate different testing of our code
  56.  
  57.  
  58. # define your function outside of the IF
  59. # just the function definition
  60.  
  61. # the IF block below only executes when this very file is run
  62. # (and NOT if this file is imported into another py file)
  63.  
  64. # if __name__ = "__main__":
  65.     # grab data with input()
  66.     # call your function
  67.     # anything else to finish up
  68.  
  69. # Seen in 7.18:
  70.  
  71. # And another good example 7.22
  72. # Define your function here.
  73. def swap_values(user_val1, user_val2):
  74.     return user_val2, user_val1 # that's a tuple, whether we wrapped in () or not
  75.  
  76. if __name__ == '__main__':
  77.     # Type your code here. Your code must call the function.
  78.     int1 = int(input())
  79.     int2 = int(input())
  80.     myTuple = swap_values(int1, int2)
  81.     print("{} {}".format(myTuple[0], myTuple[1]))
  82.  
  83. # Student question (no writing your own function on this one, but..)
  84. # Lab 8.9
  85. # In Ch 8 you'll start really getting to know your string METHODS
  86. userInput = input()
  87. noSpaces = ""
  88.  
  89. for char in userInput:
  90.     if char.isalpha(): # class methods are functions too!
  91.         noSpaces += char
  92. print(noSpaces) # just like the built-in functions!
  93.  
  94. # Remember that the built-in functions and class methods like str.strip() and list.append()
  95. # are functions just like the functions you write. They all follow the same rules.
  96.  
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement