Advertisement
jspill

webinar-functions-2021-08-14.py

Aug 14th, 2021
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. # Webinar: Functions + Methods in Python 8/14/21
  2.  
  3. # You'll start getting to know functions in Ch 7
  4.  
  5. # functions should be modular and reusable
  6. # we define a function once and call it as many times as needed
  7.  
  8. # define your function ONCE
  9. def myFunction(parameter): # parameters aren't like "normal" variables
  10.     x = 5 # don't do this with parameters
  11.     return parameter
  12.     print(parameter)
  13.  
  14. # call my functions many times
  15. myFunction("Hi")
  16. print(myFunction("Hey"))
  17. print(myFunction("How you doing?"))
  18.  
  19. # function definition, Gallant
  20. def squareThis(num):
  21.     # square = num * num
  22.     # return square
  23.     # return num**2
  24.     return num * num
  25.  
  26. print(squareThis(5)) # 25
  27. x = squareThis(6)
  28. print(x) # 36
  29.  
  30. # defining a function like Goofus
  31. def squareSomething(num):
  32.     num = 5 # dont' do this!
  33.     return num * num
  34.  
  35. print("Goofus calls:")
  36. print(squareSomething(5)) # 25
  37. print(squareSomething(6))
  38. print(squareSomething(8))
  39.  
  40. # You'll be getting to know various methods of common data types in Ch 8 and beyond
  41. # METHODS of a data type or "class" are just functions that belong to that type
  42. # dot syntax
  43. # variableOfSomeType.method()
  44. # myList.aListMethod()
  45. # myString.aStringMethod()
  46.  
  47. # help(str)
  48. print(dir(str))
  49. help(str.count)
  50. myString = "Just sit right back and you'll hear a tale."
  51. print("How many times is that string in there?", myString.count("sit"))
  52.  
  53. # Ch 8 Task 10
  54. # Complete the function to return the number
  55. # of upper case letters in the given string
  56. def countUpper(mystring):
  57.     # mystring.isupper()
  58.     count = 0
  59.     for char in mystring:
  60.         if char.isupper():
  61.             count += 1
  62.     return count
  63.  
  64. print(countUpper('Welcome to WGU'))# expected output: 4
  65. print(countUpper('Hello Mary'))# expected output: 2
  66. print(countUpper("WKRP in Cincinatti"))
  67.  
  68. # Student Question:         #... and not on functions!
  69.  
  70. # starting with year 2000, create a list containing 5 leap years
  71. # when the list is complete, print the full list with a message
  72. # expected outcome: These are the leap years: [2000, 2004, 2008, 2012, 2016]
  73.  
  74. # % mod!
  75. leapYears = []
  76. for n in range(2000, 2021):
  77.     # if n % 4 == 0:
  78.     #     if len(leapYears) <= 4:
  79.     #         leapYears.append(n)
  80.     # or
  81.     if n%4==0 and len(leapYears)<=4:
  82.         leapYears.append(n)
  83. print("These are the leap years: {}".format(leapYears))
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement