Python Flashcards

1
Q

What are Python namespaces?

A

A namespace in python refers to the name which is assigned to each object in python. The objects are variables and functions. As each object is created, its name along with space(the address of the outer function in which the object is), gets created.

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

What are decorators in Python?

A

Decorators are used to add some design patterns to a function without changing its structure. Decorators generally are defined before the function they are enhancing. To apply a decorator we first define the decorator function. Then we write the function it is applied to and simply add the decorator function above the function it has to be applied to. For this, we use the @ symbol before the decorator.

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

Q23.What is type conversion in Python?

A

Type conversion refers to the conversion of one data type into another.

int() – converts any data type into integer type

float() – converts any data type into float type

ord() – converts characters into integer
chr() - converts int to char

hex() – converts integers to hexadecimal

oct() – converts integer to octal

tuple() – This function is used to convert to a tuple.

set() – This function returns the type after converting to set.

list() – This function is used to convert any data type to a list type.

dict() – This function is used to convert a tuple of order (key, value) into a dictionary.

str() – Used to convert integer into a string.

complex(real,imag) – This function converts real numbers to complex(real,imag) number.

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

What is __init__?

A

__init__ is a method or constructor in Python. This method is automatically called to allocate memory when a new object/ instance of a class is created. All classes have the __init__ method.

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

What is a lambda function?

A

An anonymous function is known as a lambda function. This function can have any number of parameters but, can have just one statement.

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

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

A

from random import shuffle
x = [‘Keep’, ‘The’, ‘Blue’, ‘Flag’, ‘Flying’, ‘High’]
shuffle(x)
print(x)

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

How can you generate random numbers in Python?

A

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 independent multiple random number generators.

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

What is the purpose of ‘is’, ‘not’ and ‘in’ operators?

A

is: returns true when 2 operands are true (Example: “a” is ‘a’)

not: returns the inverse of the boolean value

in: checks if some element is present in some sequence

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

What advantages do NumPy arrays offer over (nested) Python lists?

A

Python’s lists are efficient general-purpose containers. They support (fairly) efficient insertion, deletion, appending, and concatenation, and Python’s list comprehensions make them easy to construct and manipulate.
They have certain limitations: they don’t support “vectorized” operations like elementwise addition and multiplication, and the fact that they can contain objects of differing types mean that Python must store type information for every element, and must execute type dispatching code when operating on each element.
NumPy is not just more efficient; it is also more convenient. You get a lot of vector and matrix operations for free, which sometimes allow one to avoid unnecessary work. And they are also efficiently implemented.
NumPy array is faster and You get a lot built in with NumPy, FFTs, convolutions, fast searching, basic statistics, linear algebra, histograms, etc.

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

How to remove values to a python array?

A

Array elements can be removed using pop() or remove() method. The difference between these two functions is that the former returns the deleted value whereas the latter does not.

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