Python Flashcards
How python manage memory?
Garbage Collection: Automatically managed, no need manual garbage collection
Reference Counting If number of reference counters to an object is zero, garbage collector will invoke and clean the memory.
A new stack frame is created when invoking a function and deleted as soon as it return something
Garbage collector will clean up the zero reference counted objects and instance variables.
What is heap memory?
Objects and instance variables are created on heap memory
What is stack memory
methods and variables are created in stack memory
What is the difference between a list and a tuple?
List
[1,3,4,20,5] (usually same kind, can be any kind though)
Mutable (Can change after creation)
Have order (usually same data type)
Tuple
(15,5.8,’Sumedhe’) # age, height, name
Un-mutable
Have structure (usually different data types)
How is string interpolation performed?
3 ways to do it.
x = ‘abc’
- print(f’Hello {x}’)
- print(‘Hello %s %s’ %(x,x))
- print(“Hi {}”.format((name)))
What is the difference between is and ==
is - Check identity == check equality a =[1,2,3] b = a c=[1,2,3]
a==b –True
a==c – True
a is b – True
a is c – False (because a and c are not pointed to the same object, doesn’t have the same ID)
What is the decorator
A decorator allows adding functionality to an existing function by passing that existing function to a decorator, which executes the existing function as well as additional code.
ex @logging operator automatically start logging
What is range function?
A function to generate a list of integers
• list(range(5)) [0,1,2,3,4]
• list(range(2,10)) [2,3,4,5,6,7,8,9]
• list(range(2,10,2)) [2,4,6,8]
Define class named car with 2 attributes, color and speed. Create a instance and return speed.
class car(): def \_\_init\_\_(self, color, speed): self.color = color self.speed = speed
car = Car(‘red’,’100mph’)
print(car.speed)
What is the difference between instance, static and class methods in python?
Instance methods : accept self parameter and relate to a specific instance of the class. Static methods : use @staticmethod decorator, are not related to a specific instance, and are self-contained (don’t modify class or instance attributes) Class methods : accept cls parameter and can modify the class itself
What is the difference between func and func()
- func is the object representing the function which can be assigned to a variable or passed to another function.
- func() with parentheses calls the function and returns what it outputs
Explain how the map function works
=> [4,5,6]
map returns a map object (an iterator) which can iterate over returned values from applying a function to every element in a sequence. The map object can also be converted to a list if required.
def add_three(x): return x+3
li = [1,2,3]
print(list(map(add_three, li)))
Does python call by reference or call by value?
In a nutshell, all names call by reference, but some memory locations hold objects while others hold pointers to yet other memory locations.
How to reverse a list?
li = [1,2,3,4]
li.reverse()
How does the string multiplication work?
'cat'*3 #==> 'catcatcat'