Functions Flashcards
What important key word is used to create new function in Python?
def foo(params):
How is called the setion that defines the function. How is it look like?
it is called the header, it holds parameters: def hello_word(name):
What is a good practice to do with every function (and all pieces of code really)
def foo(name): #to include comments """this one prints your name if you have one. lol""" print name
How is called the biggest part of every function. (hopefully it is not a comment xD)
Body - be a body - ilg
How multiline comment is called inside the function
””” DOCSTRING “””
What have you to do with function to implement it?
After defining a function, it must be called to be implemented.
Power function
def power(base, exponent): # Add your parameters here! result = base**exponent print "%d to the power of %d is %d." % (base, exponent, result)
Can function call other functions
YES. THAT IS THE BASIC OF RECURSION FOR BRIAN SAKE
What is keyword ‘import’ used for
For importing MODULES
What a ‘module’ is
A module is a file that contains definitions—including variables and functions—that you can use once it is imported.
What ‘NameError’ means
means that Python doesn’t know what used thing is -> it was not defined yet
Where sqrt() function is? In which module
import math
How to properly call sqrt() function
math.sqrt()
How to import certain variables, functions or classes form module. What is a syntax of this.
from module import function from math import sqrt # now you don't have to type math.sqrt() - it's now sqrt()
How to import all things to be in yout scope
from module import *
How to print everything from given module. What kind of function you should use.
everything = dir(math) # Sets everything to a list of things from math
BIFS
Pythonyeaa
Can functions take variable number of arguments?
Yes, it is done using *args? def foo(*args):
Some cool functions
min(), max(), abs()
What is a funtion that returns the type of the data. How it is called? What it returns.
print type(1) # print type(2.0) # print type("Grimes") #
Some function
def shut_down(s): if s == "yes": return "Shutting down" elif s == "no": return "Shutdown aborted" else: return "Sorry"
How Pythoon compares strings. Type equal comparision.
string == “string value”
Another function
def distance_from_zero(number): if type(number) == int or type(number) == float: return abs(number) else: return "Nope"
distance_from_zero(“lol”)