Python - Variables Flashcards

Review Major Concepts of the Variables in the Python Programming Language

1
Q

What is a variable?

A

Think of variables as labels that you can assign to values. You can also say that a variable references a certain value.

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

What does it mean to ‘bind’ a variable?

A

Binding a variable means to set a name to hold a reference to some object. Object deletion may automatically follow, by garbage collection, when no more references to an object exist.

A del statement consists of the keyword del, followed by one or more target references separated by commas (,).

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

What does it mean to ‘unbind’ a variable?

A

Unbinding a variable means to reset the variable name so it no longer holds a reference.

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

What does the ‘del’ statement do?

A

The ‘del’ statement unbinds a variable reference, although doing so is rare.

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

What does it mean to ‘rebind’ a variable?

A

Binding a reference that was already bound is also known as rebinding.

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

What are some naming conventions for a variable?

A

Cannot use a keyword.

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

Name some keywords.

A

and
break
elif
from
is
pass
with
as
class
else
global
lambda
raise
yield
assert
continue
except
if
nonlocal
return
False
async
def
finally
import
not
try
None
await
del
for
in
or
while
True

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

What is a ‘global’ variable?

A

A global variable is an attribute of a module object.

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

What is a ‘local’ variable?

A

A local variable lives in a function’s local namespace .

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

What does ‘namespace’ or ‘scope’ mean?

A

The function body, make up the function’s local namespace, also known as its local scope.

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

What does ‘assignment’ mean for variables in Python?

A

Assignment is when an value or object binds to a variable name (i.e. identifier)

Example: target = expression

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

What are the two types of assignments for variables in Python?

A

The two types of assignments for variables in Python are Plain Assignment and Augmented Assignment.

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

What is a ‘plain assignment’ for Python variables?

A

Plain assignment to a variable (e.g., name = value) is how you create a new variable or rebind an existing variable to a new value.

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

What is an ‘augmented assignment’ for Python variables?

A

Augmented assignment can
* rebind a variable,
* ask an object to rebind one of its existing attributes or items, or
* request the target object to modify itself.

Augmented assignment (e.g., name += value) cannot, per se, create new references.

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

What is a ‘Plain Assignment’ for variables in Python?

A

Plain assignment to a variable (e.g., name = value) is how you create a new variable or rebind an existing variable to a new value.

target = expression

17
Q

What is the ‘unpacking assignment’ for variables in Python?

A

The unpacking assignment in Python binds each reference in the target to the corresponding item in the Right Hand Side:

a, b, c = x # x must be an iterable with the exact amount of values
a, b = b, a # swapping value without a 3rd variable
first, *middle, last = x # the starred identifier gets the left over value(s)
first, middle, last = x[0], x[1:-1], x[-1] # same as previous

18
Q

What is an ‘Augmented Assignment’ for variables in Python?

A

An augmented assignment (sometimes called an in-place assignment) differs from a plain assignment in that, instead of an equals sign (=) between the target and the expression, it uses an augmented operator, which is a binary operator followed by =. The augmented operators are +=, -=, *=, /=, //=, %=, **=, |=,&raquo_space;=, «=, &=, ^=, and @=.

Augmented assignment does not support multiple targets.