Python Flashcards

1
Q

What’s the difference between list and tuples?

A

Lists
- 1) lists are mutable i.e. they can be edited

  • 2) lists are slower than tuples
  • 3) Syntax: list_1 = [10, ‘chelsea’, 20]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the key features of Python?

A
  • Python is an interpreted language. That means that, unlike languages such as C and its variants, Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby
  • Python is dynamically typed, this means that you don’t need to state the types of variables when you declare them or anything like that. You can do things like x=111 and then x=’Im a string’ without error.
  • Python is well suited to object oriented programming in that it allows the definition of classes along with composition and inheritance. Python does not have access specifiers (like C++’s public, private), the justification for this point is given as ‘we are all adults here’
  • Functions are first class objects. this means that they can be assigned to variables, returned from other functions and passed into functions. Classes are also first class objects.
  • Writing Python code is quick but running it is often slower than compiled languages. Fortunately, Python allows the inclusion of C based extensions so bottlenecks can be optimized away and often are. The numpy package is a good example of this, it’s really quite quick because a lot of the number crunching it does isn’t actually done by Python.
  • Python finds use in many spheres - web applications, automation, scientific modelling, big data applications and many more. Its also often used as a ‘glue’ code to get other languages and components to play nice.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the difference between deep and shallow copy?

A

Shallow copy is used when a new instance type gets created and it keeps the values that are copied in the new instance. Shallow copy is used to copy the reference pointers just like it copies the values. THese references point ot he original objects and the changes made in any member of the class will also affect the original copy of it. Shallow copy allows faster execution of the program and it depends on the size of the data that is used.

  • Deep copy is used to store teh values that are already copied. Deep copy doesn’t copy the reference pointers to the objects. It makes the reference to an object and the new object that is pointed by some other object gets stored. The changes made in the original copy won’t affect any other copy that uses the object. Deep copy makes execution of the program slower due to making certain copies for each object that is been called.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is multithreading achieved in Python?

A
  1. Python has a multi-threading package but if you want to multi-thread to speed up your code, then its usually not a good idea to use it.
  2. Python has a construct called the global interpreter lock (GIL). the GIL makes sure that only one of your ‘threads’ can execute at any one time. A thread acquires the GIL, does a littler work, then passes the GIL onto the next thread.
  3. THis happens very quickly so to the human eye it may seem like your threads are executing in parallel, but they are really just making turns using the same CPU core.
  4. All this GIL passing adds overhead to execution. This means that if you want to make your code run faster than using the threading package often isn’t a good idea.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can the ternary operators be used in python?

A

The ternary operator is the operator that is used to show the conditional statements. This consists of the true or false values with a statement that has to be evaluated for it.

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

How is memory managed in Python?

A

Memory management in python is managed by Python private heap space. All python objects and data structures are located in a private heap. The programmer does not have access to this private heap. THe python interpreter takes care of this instead.

  1. Python also has an inbuilt garbage collector, which recycles all the unused memory and so that it can be made available to the heap space.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain inheritance in Python with an example

A

Inheritance allows one class to gain all the members (say attributes and methods) of another class. Inheritance provides code reusability, makes it easier to create and maintain an application. The class from which we are inheriting this is called super-class and the class that is inherited is called a derived/child class.

They are different types of inheritance supported by Python:

    Single Inheritance – where a derived class acquires the members of a single super class.
    Multi-level inheritance – a derived class d1 in inherited from base class base1, and d2 are inherited from base2.
    Hierarchical inheritance – from one base class you can inherit any number of child classes
    Multiple inheritance – a derived class is inherited from more than one base class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the usage of help() and dir() function in Python?

A

Help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions.

  • Help() function: The help() function is used to display the documentation string and also facilitates you to see the help related to modules, keywords, attributes, etc.
  • Dir() function: The dir() function is used to display the defined symbols.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Whenever Python exits, why isn’t all the memory de-allocated?

A
  • Whenever Python exits, especially those Python modules which are having circular references to other objects or the objects that are referenced from the global namespaces are not always de-allocated or freed.
  • It is impossible to de-allocate those portions of memory that are reserved by the C library.
  • On exit, because of having its own efficient clean up mechanism, Python would try to de-allocate/destroy every other object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a dictionary in Python

A

The built-in datatypes in Python is called dictionary. It defines one-to-one relationship between keys and values. Dictionaries contain pair of keys and their corresponding values. Dictionaries are indexed by keys.

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

What is monkey patching in Python?

A

In Python, the term monkey patch only refers to dynamic modifications of a class or module at run-time.

for example, you can create a class with method create_this(). You can then later reassign this method for an instantiation using assignment to produce a different result.

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

What does this mean: *args, **kwargs? And why would we use it?

A

We use *args when we aren’t sure how many arguments are going to be passed to a function, or if we want to pass a stored list or tuple of arguments to a function. **kwargsis used when we don’t know how many keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments. The identifiers args and kwargs are a convention, you could also use *bob and **billy but that would not be wise.

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

What are negative indexes and why are they used?

A

The sequences in Python are indexed and it consists of the positive as well as negative numbers. The numbers that are positive uses ‘0’ that is uses as first index and ‘1’ as the second index and the process goes on like that.

The index for the negative number starts from ‘-1’ that represents the last index in the sequence and ‘-2’ as the penultimate index and the sequence carries forward like the positive number.

The negative index is used to remove any new-line spaces from the string and allow the string to except the last character that is given as S[:-1]. The negative index is also used to show the index to represent the string in correct order.

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

How can you randomize the items of a list in place in Python?

A

from random import shuffle

x = [‘keep’, ‘this’, ‘change’]

shuffle(x)

#this is now shuffled
print(x)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain split(), sub(), subn() methods of “re” module in Python.

A

To modify the strings, Python’s “re” module is providing 3 methods. They are:

  • split() – uses a regex pattern to “split” a given string into a list.
  • sub() – finds all substrings where the regex pattern matches and then replace them with a different string
  • subn() – it is similar to sub() and also returns the new string along with the no. of replacements.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you generate random numbers in Python?

A

Random module is the standard module that is used to generate the random number. The method is defined as:

import random
random.random

The statement random.random() method return the floating point number that is in the range of [0, 1). The function generates the random float numbers. The methods that are used with the random class are the bound methods of the hidden instances. The instances of the Random can be done to show the multi-threading programs that creates different instance of individual threads. The other random generators that are used in this are:

    randrange(a, b): it chooses an integer and define the range in-between [a, b). It returns the elements by selecting it randomly from the range that is specified. It doesn’t build a range object.
    uniform(a, b): it chooses a floating point number that is defined in the range of [a,b).Iyt returns the floating point number
    normalvariate(mean, sdev): it is used for the normal distribution where the mu is a mean and the sdev is a sigma that is used for standard deviation.
    The Random class that is used and instantiated creates an independent multiple random number generators.
17
Q

What is the difference between range & xrange?

A

For the most part, xrange and range are the exact same in terms of functionality. They both provide a way to generate a list of integers for you to use, however you please. The only difference is that range returns a Python list object and x range returns an xrange object.

This means that xrange doesn’t actually generate a static list at run-time like range does. It creates the values as you need them with a special technique called yielding. This technique is used with a type of object known as generators. That means that if you have a really gigantic range you’d like to generate a list for, say one billion, xrange is the function to use.

This is especially true if you have a really memory sensitive system such as a cell phone that you are working with, as range will use as much memory as it can to create your array of integers, which can result in a Memory Error and crash your program. It’s a memory hungry beast.

18
Q

What is pickling and unpickling?

A

Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.