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'
How does the list multiplication work?
[1,2,3]*2 #==> [1,2,3,1,2,3]
How does the list multiplication work?
[1,2,3]*2 #==> [1,2,3,1,2,3]
What does “self” refer to in a class?
- Self refers to the instance of the class itself
* give methods access to and the ability to update the object they belong to.
How can you concatenate lists in python?
a = [1,2] b = [3,4,5]
a+b
==> [1,2,3,4,5]
What is the difference between lists and numpy arrays?
- Lists exist in python’s standard library. Arrays are defined by Numpy.
- Lists can be populated with different types of data at each index. Arrays require homogeneous elements.
- Arithmetic on lists adds or removes elements from the list. Arithmetic on arrays functions per linear algebra.
- Arrays also use less memory and come with significantly more functionality.
how to concatenate numpy arrarys?
=> array([1,2,3,4,5,6])
import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])
np.concatenate((a,b))
What do you like about Python?
Python is very readable and there is a pythonic way to do just about everything, meaning a preferred way which is clear and concise.
I’d contrast this to Ruby where there are often many ways to do something without a guideline for which is preferred.
What is you favorite library in Python?
When working with a lot data, nothing is quite as helpful as pandas which makes manipulating and visualizing data a breeze.
Name mutable and immutable objects
- Immutable means the state cannot be modified after creation. Examples are: int, float, bool, string and tuple.
- Mutable means the state can be modified after creation. Examples are list, dict and set.