# Webinar: Functions + Methods in Python 8/14/21 # You'll start getting to know functions in Ch 7 # functions should be modular and reusable # we define a function once and call it as many times as needed # define your function ONCE def myFunction(parameter): # parameters aren't like "normal" variables x = 5 # don't do this with parameters return parameter print(parameter) # call my functions many times myFunction("Hi") print(myFunction("Hey")) print(myFunction("How you doing?")) # function definition, Gallant def squareThis(num): # square = num * num # return square # return num**2 return num * num print(squareThis(5)) # 25 x = squareThis(6) print(x) # 36 # defining a function like Goofus def squareSomething(num): num = 5 # dont' do this! return num * num print("Goofus calls:") print(squareSomething(5)) # 25 print(squareSomething(6)) print(squareSomething(8)) # You'll be getting to know various methods of common data types in Ch 8 and beyond # METHODS of a data type or "class" are just functions that belong to that type # dot syntax # variableOfSomeType.method() # myList.aListMethod() # myString.aStringMethod() # help(str) print(dir(str)) help(str.count) myString = "Just sit right back and you'll hear a tale." print("How many times is that string in there?", myString.count("sit")) # Ch 8 Task 10 # Complete the function to return the number # of upper case letters in the given string def countUpper(mystring): # mystring.isupper() count = 0 for char in mystring: if char.isupper(): count += 1 return count print(countUpper('Welcome to WGU'))# expected output: 4 print(countUpper('Hello Mary'))# expected output: 2 print(countUpper("WKRP in Cincinatti")) # Student Question: #... and not on functions! # starting with year 2000, create a list containing 5 leap years # when the list is complete, print the full list with a message # expected outcome: These are the leap years: [2000, 2004, 2008, 2012, 2016] # % mod! leapYears = [] for n in range(2000, 2021): # if n % 4 == 0: # if len(leapYears) <= 4: # leapYears.append(n) # or if n%4==0 and len(leapYears)<=4: leapYears.append(n) print("These are the leap years: {}".format(leapYears))