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
- # We'll write a function to take in a number as square it. The function will RETURN that square.
- # function definition, Gallant
- def squareThis(num): # num is the parameter
- return num * num
- # print(squareThis(3))
- # print(squareThis(7))
- # function definition, Goofus
- def squareSomething(num):
- num = 5 # no, Goofus! Don't override the parameter!
- return num * num
- # print(squareSomething(3)) # Goofus hopes you like 25
- # print(squareSomething(6)) # on every...
- # print(squareSomething(9)) # single call :(
- # keyword args of print()
- # print() --> print(end="\n", sep=" ")
- def makeMore(num): # only function definitions go outside of the if __name__ block
- return num * num * num + num
- if __name__ == "__main__":
- # solve this Lab, this problem
- myNum = int(input())
- print(f"I bought {myNum} snails and now I have {makeMore(myNum)}!")
- # print(__name__) # it's "__main__"
- # print(dir(__builtins__))
- print(dir(str))
- print(dir(list))
- # class methods are functions...
- myList = ["Gilligan", "Skipper"]
- myList.append("Mary Ann") # class methods are called "on" an object of the type
- myList.append("Professor")
- print(myList)
- # that object of its type is called the "self" and appears as if it were the first argument in the help docstring:
- help(list.append) # append(self, object, /) Append object to the end of the list.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement