Python Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How python manage memory?

A

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.

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

What is heap memory?

A

Objects and instance variables are created on heap memory

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

What is stack memory

A

methods and variables are created in stack memory

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

What is the difference between a list and a tuple?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How is string interpolation performed?

A

3 ways to do it.

x = ‘abc’

  1. print(f’Hello {x}’)
  2. print(‘Hello %s %s’ %(x,x))
  3. print(“Hi {}”.format((name)))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between is and ==

A
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)

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

What is the decorator

A

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

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

What is range function?

A

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]

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

Define class named car with 2 attributes, color and speed. Create a instance and return speed.

A
class car():
  def \_\_init\_\_(self, color, speed):
      self.color = color
      self.speed = speed

car = Car(‘red’,’100mph’)
print(car.speed)

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

What is the difference between instance, static and class methods in python?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between func and func()

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain how the map function works

A

=> [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)))

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

Does python call by reference or call by value?

A

In a nutshell, all names call by reference, but some memory locations hold objects while others hold pointers to yet other memory locations.

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

How to reverse a list?

A

li = [1,2,3,4]

li.reverse()

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

How does the string multiplication work?

A
'cat'*3
#==> 'catcatcat'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does the list multiplication work?

A
[1,2,3]*2
#==> [1,2,3,1,2,3]
16
Q

How does the list multiplication work?

A
[1,2,3]*2
#==> [1,2,3,1,2,3]
17
Q

What does “self” refer to in a class?

A
  • Self refers to the instance of the class itself

* give methods access to and the ability to update the object they belong to.

18
Q

How can you concatenate lists in python?

A
a = [1,2]
b = [3,4,5]

a+b
==> [1,2,3,4,5]

19
Q

What is the difference between lists and numpy arrays?

A
  • 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.
20
Q

how to concatenate numpy arrarys?

A

=> 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))

21
Q

What do you like about Python?

A

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.

22
Q

What is you favorite library in Python?

A

When working with a lot data, nothing is quite as helpful as pandas which makes manipulating and visualizing data a breeze.

23
Q

Name mutable and immutable objects

A
  • 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.
24
Q

Are dictionaries or lists faster for lookups?

A
  • Looking up a value in a list takes O(n) time because the whole list needs to be iterated through until the value is found.
  • Looking up a key in a dictionary takes O(1) time because it’s a hash table.
  • This can make a huge time difference if there are a lot of values so dictionaries are generally recommended for speed. But they do have other limitations like needing unique keys.
25
Q

How to remove duplicate elements from a list?

A

=> [1,2,3]

a = [1,1,2,3,3,4]

print(list(set(a)))

26
Q

How can you sort a dictionary by key, alphabetically?

A

=>[(‘a’,1),(‘b’,2),(‘c’,3),(‘d’,4)]

You can’t “sort” a dictionary because dictionaries don’t have order but you can return a sorted list of tuples which has the keys and values that are in the dictionary.

d = {‘c’:3, ‘d’:4, ‘b’:2, ‘a’:4}

sorted(d.items())

27
Q

How to check string for numbers, letters, numbers and letters?

A

String.isnumeric()
String.isalpha()
String.isalnum()

28
Q

Return a list of keys from a dictionary.

Return a list of values from a dictionary.

A

> > > d = {‘a’:1,’b’:2,’d’:4,’c’:3}

> > > list(d)
[‘a’, ‘b’, ‘d’, ‘c’]

> > > list(d.keys()) # another way
[‘a’, ‘b’, ‘d’, ‘c’]

> > > list(d.values())
[1, 2, 4, 3]

29
Q

What is the difference between remove, del and pop?

A

remove() remove the first matching value.
del removes an element by index.
pop() removes an element by index and returns that element.

30
Q

How is exception handling performed in Python?

A
try: 
    try something
except:
    do something if failed
finally:
    do something else at the end always