Part 5 Flashcards

1
Q

What does the scope of a variable denote?

A

The area of the program from which it is accessible

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

If given a value outside of a function, what scope does a variable have?

A

Global - accessible from elsewhere in program

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

What is global scope?

A

Means variable can be accessed from anywhere in the program

However - cannot create a new variable with the same name even in a seperate function

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

What is local scope?

A

Variable only accessible from within the function it was defined in.

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

What Exception type is raised when a variable that does not exist is accessed?

A

NameError: name ‘variablename’ is not defined

Happens also when variablename has local scope but is attempted to be accessed outside of the function

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

What happens when you try to set the value of a global variable inside a function?

What would happen if you executed this:

def myfun():

print(x)

x = 5

x = 10

myfun()

A

It masks the global variable - so any changes within the function will not affect the global variable

The program shown would raise an UnboundLocalError - trying to access the variable before it is assigned

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

What is the lifetime of a variable?

A

The time it will remain in existence in a program

Global variables exist for the entire duration of the program after being assigned

Local variables only for the duration of their function

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

What happens when a mutable object is passed as an argument to a function, then modified within the function?

A

The original object is actually modified. Probably because they are pointers to the objects rather than copies of

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

May tuples contain multiple types?

A

Yes.

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

How can we create a tuple with just one element?

A

mytup = (1,)

Will contain just the element 1

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