Python functions

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/10

encourage image

There's no tags or description

Looks like no tags are added yet.

11 Terms

1
New cards

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


2
New cards

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


3
New cards

what is a keyword argument

a function in which the keyword is = to a value rather than be positional

4
New cards

how do you define a keyword argument

def someFunction(keywordArgument1=defaultValue1,keywordArgument2=defaultValue2,etc):

someCode

return outputArgument1, outputArgument2, etc.

5
New cards

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”)

6
New cards

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]

7
New cards

difference between a tuple and a list

tuples use round brackets not square brackets

tuples are immutable (can’t assign a new item)

8
New cards

How do you unpack a tuple

a= (5,8,9,'salt')

item1,item2,item3,item4=a

print(item1)

print(item2)

print(item3)

print(item4)

9
New cards

Are variables defined inside a function available outside this function

no, they are of local scope

10
New cards

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

11
New cards

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)