1/10
Looks like no tags are added yet.
how do you define a function in python
def someFunction():
someCode
someFunction is the name of the function
someCode is the code to be executed inside the function
How do you get input and output arguments from functions
def someFunction(inputArgument1, inputArgument2, etc.):
someCode
return outputArgument, outputArgument2, etc.
where:
inputArgument1, inputArgument2, etc. are variables or data provided as input and are called input arguments
outputArgument1, outputArgument2, etc. are data returned as output and are called output arguments
It is not necessary to include output arguments after return
When calling the function it is important to use the correct order for input and output arguments
what is a keyword argument
a function in which the keyword is = to a value rather than be positional
how do you define a keyword argument
def someFunction(keywordArgument1=defaultValue1,keywordArgument2=defaultValue2,etc):
someCode
return outputArgument1, outputArgument2, etc.
how do you call a keyword argument
functionName(keywordArgumentName=keywordArgumentValue)
note:can change arguments by making them = to another value
e.g FunctionName(keywordArgument= ”stop”)
What is a tuple
When a python function returns multiple outputs
You can create a tuple like this:
aTuple=(item1, item2, etc.)
You can access tuple elements like this:
element=aTuple[index]
difference between a tuple and a list
tuples use round brackets not square brackets
tuples are immutable (can’t assign a new item)
How do you unpack a tuple
a= (5,8,9,'salt')
item1,item2,item3,item4=a
print(item1)
print(item2)
print(item3)
print(item4)
Are variables defined inside a function available outside this function
no, they are of local scope
Are variables outside a function available inside this function
They might be accessible and modifiable inside the function but will not effect the value outside the function
If input arguments of a function are modelled inside the function will they be modified outside?
not unless you redefine it after the function has been defined
e.g. would need to do outsideVariable=function(outsideVariable)