Chapter 7: Functions and Modules Flashcards
What is the syntax for defining your own function?
def functionName(list of parameters): code detailing what the function should do return [expression]
Explain the syntax
def tells the program that indented code from next line onwards is part of the program. Return is the keyword used to return an answer from the function.
Can there be more than 1 return statement in a function?
Yes
What happens once the function executes a return statement?
It exits
What to do if the function need not return any value?
We can omit the return statement. Alternatively, we can write return or return none.
Write the script for defining a function.
What happens if the local variable shares the same name as the global variable?
Any code inside the function is accessing the local variable. Any code outside the function is accessing the global variable.
Write a script with the result showing the last flashcard
Result:
INSIDE THE FUNCTION Local variable(shares the same name as a global variable)
OUTSIDE THE FUNCTION Global variable(shares same name as a local variable)
What does Python allowing a default value mean?
This means that Python allows to assign default values to the parameters when defining a function
Give an example for defining the default parameters
def someFunction(a,b,c=1,d=2,e=3): print(a,b,c,d,e)
What are salient features to remember when assigning default parameters?
1) All parameters with default values must be placed at the end of the parameters list.
How does Python allow us to pass a variable number of arguments to a function?
By using the *(asterisk) before the parameter
Give an example of variable length argument
When we add an asterisk in front of a parameter name, we can pass in a variable number of arguments to the function. This is known as a ________variable length argument list.
non-keyworded
If we want to pass in a keyworded variable length argument list to the function, we can use _______
double asterisks
Write a script defining a function with double asterisks
If our function uses a normal argument (also known as a formal argument), a non-keyworded variable length argument list and a keyworded variable length argument list, we must define the function using the following order:
def someFunction2(farg,*args, **kwargs):
What is the first way to import the entire module?
by writing
importmoduleName
for example import random
How would you use the randrange() function in the random module?
We write random.range(1,10)
What is the third way to import modules?
To import specific functions
How can you import specific functions?
By writing
moduleName import name1 [,name2[,…nameN]]
What would you write to import the randrange() function from the random module?
from random import randrange
What if we want to import more than 1 function?
We separate them with a comma.
For example,
from random import randrange, randint
and then just write
randrange(1,10)
What if you find it troublesome to write random each time you use the function?
you simply write r.randrange(1,10)
Why should you create your own module?
If you want to reuse them in other programming projects in future
How would you use the checkIfPrime() function defined earlier in another script?
Save the script with this function in the same folder as the another script which you are using to call on this function. The other script is called usecheckifprime.py