Python Functions Flashcards

1
Q

Whats the syntax for Functions in python

A
def Funcname(parameter1, parameter2):
     code1, code 2
     return
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Whats the order of function manipulation to acquire variables

A

LEGB

Local, enclosed, global, built in

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Whats the difference between argument and parameter

A

argument is something u call externally of the function which is then placed into the parameters to be placed into the function.

sub the value of tha argument into the paraemter

myarg=25

def myfunc(parameter):
     print parameter

myfunc(arg) –> #parameter = myarg =25
print(myarg)

output: 25
25

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
charList = ['a', 'e', 'i', 'o', 'u']
myStr = "This is a string!"
def funcA(content, target):
    num = 0
    for char in content:
        if char in target:
            num += 1
    return num
result = funcA(myStr, charList)
print(result)
A

output = 5.

Similar kind of loop:

for char in mystr:
if char in charlist:
num+=1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does global variable do

A

Brings the reference ID from global into the function and link them together

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
def enclosing():
    myVariable = 'defined by enclosing'
    def enclosed():
        print('scope: ' + myVariable)
    enclosed()
enclosing()
A

By following the rules of LEGB,
“myVariable” acquires its value from the enclosed function.

If happens that “myVariable” with a value exists in the global, then the function would acquire it from there.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
def myFun (param):
     param.append(4)
     return param
myList = [1,2,3]
newList = myFun(myList)
print(myList,newList)
A

output=[1,2,3,4],[1,2,3,4]

Both param and myList shares the same reference index, hence changing the value of one will change the other

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
def myFun (param):
    param=[1,2,3]
    param.append(4)
    return param
myList = [1,2,3]
newList = myFun(myList)
print(myList,newList)
A

output=[1,2,3], [1,2,3,4]

a new reference has been assigned to param, hence my list and param no longer share the same reference ID.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
myVar = 127
def myFun (myVar):
    myVar = 7
    print(‘myVar: ’, myVar)
myFun(myVar)
print(‘myVar: ’, myVar)
A

myVar: 7 (This myVar is from the function as variables called in the functions have a different ID to compared to global variable)

myVar: 127 (This variable is from the global)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
myVar = 127
def myFun ():
    a = myVar + 1
    print(‘a: ’, a)
myFun()
A

‘a’ = 128

myVar adopts the value from the global variable to fit, following the rule of LEGB.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
myVar = 127
def myFun ():
    myVar = myVar + 1
myFun()
print(myVar)
A

Error.

the function alr has myVar, so the programm automatically takes it from the local variable instead of the global variable.

if the code happens to change to x = myVar+1
then the result of x will be 128

How well did you know this?
1
Not at all
2
3
4
5
Perfectly