Types and objects Flashcards

1
Q

What three things does every python object have?

A

Value, type, ID

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

Explain mutability vs immutability

A

The value of a mutable object can be changed, whereas an immutable object has the same value for its whole life.

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

Give three examples of mutable objects

A

Dictionary, set, list

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

Give four examples of immutable objects

A

String, integer, tuple, float

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

If we assign a=b, explain what happens if b is immutable vs mutable.

A

a = b creates a copy of b. a = something else sets a to a different object if b is immutable. Changing a mutable object means it can have the same ID, so a would change too.

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

How would you find the number of references to an object?

A

import sys

sys.getrefcount(object)

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

How does the garbage collector in Python work?

A

Once objects have no references, they are deleted from memory.

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

Explain the difference between the ‘is’ operator and the ‘==’ operator

A

is : compares IDs of left and right object.

== : compares values of left and right object

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

What does it mean to say that in Python, all objects are first class?

A

All variables that can be named can be treated like data.

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

Name 5 categories for representing data in python, give examples of each

A
None: None
Numbers: int, float
Sequence: list, tuple, string, generator
Mapping: dict
Set: set
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Name 5 operations on sequence types?

A
len()
slicing [:] (\_\_getitem\_\_)
in
concatenation +
copying *
aggregation (min, max,...)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the criterion for a key to be valid in python?

A

It must be hashable/immutable

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

How would you get the keys, and the values of a dictionary?

A

dict.keys()
dict.values()
for key, value in dict.items()

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

Name 5 operations that can be performed on set types?

A
intersection
union
difference
issubset
isdisjoint
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Name 3 categories of types for representing program structure.

A

Classes, callables (functions, methods), modules

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

How can you tell if an object is callable?

A

callable(object)

17
Q

Name 5 string methods

A

string. count()
string. replace()
string. strip()
string. find()
string. upper()
string. lower()