Types and objects Flashcards
What three things does every python object have?
Value, type, ID
Explain mutability vs immutability
The value of a mutable object can be changed, whereas an immutable object has the same value for its whole life.
Give three examples of mutable objects
Dictionary, set, list
Give four examples of immutable objects
String, integer, tuple, float
If we assign a=b, explain what happens if b is immutable vs mutable.
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 would you find the number of references to an object?
import sys
sys.getrefcount(object)
How does the garbage collector in Python work?
Once objects have no references, they are deleted from memory.
Explain the difference between the ‘is’ operator and the ‘==’ operator
is : compares IDs of left and right object.
== : compares values of left and right object
What does it mean to say that in Python, all objects are first class?
All variables that can be named can be treated like data.
Name 5 categories for representing data in python, give examples of each
None: None Numbers: int, float Sequence: list, tuple, string, generator Mapping: dict Set: set
Name 5 operations on sequence types?
len() slicing [:] (\_\_getitem\_\_) in concatenation + copying * aggregation (min, max,...)
What is the criterion for a key to be valid in python?
It must be hashable/immutable
How would you get the keys, and the values of a dictionary?
dict.keys()
dict.values()
for key, value in dict.items()
Name 5 operations that can be performed on set types?
intersection union difference issubset isdisjoint
Name 3 categories of types for representing program structure.
Classes, callables (functions, methods), modules