Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Webinar: Functions + Methods in Python
- # You'll start getting to know functions in Ch 7
- # A function is essentially a block of code with a name
- # that can (or not) accepts inputs as arguments and perform
- # some actions accordingly.
- # Functions should be modular and reusable - a good function has ONE JOB
- # We define a function once and call it as many times as needed
- # def myFunction(parameter): # calls provide values into parameter variables
- # # so don't do this:
- # # parameter = 5 # No! We don't assign values to parameters this way
- # newValue = parameter/2 # whatever the function is doing
- # return newValue # return vs print, do one not both
- # print(newValue) # this would not execute, since we already returned
- def returnATuple(x, y, z): # x, y, z are parameters
- return z, y, x # same as return (z, y, x)
- x = returnATuple(1, 2, 3) # 1, 2, and 3 are ARGUMENTS going into each parameter
- print("{}: {}".format(x, type(x)))
- # function definition, Gallant
- def squareThis(num):
- return num * num
- print(squareThis(3)) # 9? yes
- print(squareThis(5)) # 25? yes
- # function definition, Goofus
- def squareSomething(num):
- num = 5 # no, Goofus! Don't override your parameter!
- return num * num
- print(squareSomething(3)) # Goofus hopes you like 25
- print(squareSomething(6)) # on every...
- print(squareSomething(9)) # single call :(
- # Examples and Student Questions
- # Ch 8 Task 1
- # Complete the function to print the first X
- # number of characters in the given string
- def printFirst(mystring, x):
- # pass
- print(mystring[0:x])
- # expected output: WGU
- printFirst('WGU College of IT', 3)
- # expected output: WGU College
- printFirst('WGU College of IT', 11)
- printFirst("WKRP in Cincinnati", 4) # add a test call of your own
- # if name = main, a structure you'll see first Lab 7.18
- # this structure helps us separate different testing of our code
- # define your function outside of the IF
- # just the function definition
- # the IF block below only executes when this very file is run
- # (and NOT if this file is imported into another py file)
- # if __name__ = "__main__":
- # grab data with input()
- # call your function
- # anything else to finish up
- # Seen in 7.18:
- # And another good example 7.22
- # Define your function here.
- def swap_values(user_val1, user_val2):
- return user_val2, user_val1 # that's a tuple, whether we wrapped in () or not
- if __name__ == '__main__':
- # Type your code here. Your code must call the function.
- int1 = int(input())
- int2 = int(input())
- myTuple = swap_values(int1, int2)
- print("{} {}".format(myTuple[0], myTuple[1]))
- # Student question (no writing your own function on this one, but..)
- # Lab 8.9
- # In Ch 8 you'll start really getting to know your string METHODS
- userInput = input()
- noSpaces = ""
- for char in userInput:
- if char.isalpha(): # class methods are functions too!
- noSpaces += char
- print(noSpaces) # just like the built-in functions!
- # Remember that the built-in functions and class methods like str.strip() and list.append()
- # are functions just like the functions you write. They all follow the same rules.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement