ptn Flashcards

1
Q

How to install pip3 in Ubuntu?

A

sudo apt-get install python3-pip

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

How to install PyQt5 on Ubuntu?

A

sudo -H python3 -m pip install pyqt5

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

Python: if an instance method is called with three arguments, how will the method signature look?

A

Signature will have four arguments, with ‘self’ being the first argument.

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

Python: How are ‘class methods’ defined?

A

They are decorated with @classmethod.
First argument in their signature is ‘cls’ representing the class, NOT an instance/self.

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

Python: What are ‘static methods’?

A

These are decorated with @staticmethod. Arguments at the time of calling and method signature exactly match. No ‘self’ or ‘cls’ argument is implicitly passed.

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

Python: What is an ‘iterable’?

A

Iterable is a sequence data structure, like a list, tuple etc.

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

Python: Write generator for even numbers less then 20

A

(i for i in range(20) if i%2 == 0)

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

Python: Can a generator be used again, after it is made use of?

A

No, once you iterate over a generator, you must define it again, if you want to use it again.

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

Python: What are instance attributes and class attributes?

A

Instance attributes are different for each object, they are always accessed as self.attr_name inside the class definition. They are most common kind of attributes.

Class attributes are shared by all objects of a class. They are normally declared outside __init__ xtor, without self.
Inside methods, class attributes are referred with self.attr_name, just like instance attributes.

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

What is special about class ‘object’ in python?

A

Every class in python is derived from ‘object’ class.

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

Python: What happens when you assign an object to another variable?

A

New variable name is a new reference to original object.
Any change in original object will be visible through the new variable.

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

Python: How to add an item at the end of a list?

A

lst.append(“new item”)

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

Python: How to remove an item from an index of a list?

A

removed_item = lst.pop(index)
lst.pop() without an argument will remove last item.

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

Python: How is dir() used with modules?

A

After importing a module mode, dir(mod) gives all new symbols added by mod.

dir() without any arguments gives list of all global names known.

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

What is a python module?

A

It is a regular python code file. It can be imported in another file, that way making variables defined in the module file become available here.

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

What is a python package?

A

A python package is a directory containing multiple module files.

from pkg_dir import module_file_1

pkg/modules are addressed like directory structure.
pkg/sub_pkg/module_file
import pkg.sub_pkg.module_file

Package directory can contain an __init__.py file

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

Python: What happens when you append b in front of a string, like b’ABCD1234’

A

In Python3, without b, each character in the string maybe represented by 16 or 32 bits, but with b in front, each character in the string is represented like old fashioned 8 bit character.

We should use b in front of string if we have to send the string over a serial line.

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

Python: What module is used to do multithreading?

A

import threading

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

Python: How to find the length of a list?

A

len(list_name)

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

What is the diff between
pip install django and
pip –user install django

A

without –user, pip installs a package on a system wide global location. With –user pip installs the package somewhere in the user’s home directory.

Instead of using –user, a better way is to use virtual environment, & not use –user.

21
Q

Python: How to list all packages installed?

A

pip list

22
Q

Python: How to get info about an installed package?

A

pip show django

23
Q

Python: How to search PyPI.org repository for a package?

A

pip search pyqt

24
Q

Python: How to uninstall a package?

A

pip3 uninstall flask

25
Q

Python: How to get a snapshot of all packages installed in a system.

A

pip freeze > requirements.txt

These packages can be installed in another system with,

pip install -r requirements.txt

26
Q

Python: After importing a module, how to find its path in file system?

A

import mod
mod.__file__

27
Q

Python: How to find all variables (names) known?

A

dir()

returns a list of strings.

28
Q

Python: How to find all variables (names) provided by an imported module?

A

import mod
dir(mod)

returns a list of strings.

29
Q

Python: How to find type of a variable?

A

type(var_name)

print( type(var_name) )

30
Q

Python: How do u define a complex number?

A

z = 3.14 + 2.71j

– or –

z = complex(3.14, 2.71)

type(z) = < class ‘complex’ >

complex(3) = 3 + 0j
complex() = 0 + 0j

31
Q

Python: Real & imaginary parts of a complex number z?

A

z.real
z.imag

32
Q

Python: Conjugate of a complex number z?

A

z.conjugate()

33
Q

Python: Absolute value of a complex number z?

A

abs(z)

34
Q

How to create a numpy array

A

Use numpy array() function.

import numpy as np
arr = np.array([13, 17, 19, 23])

35
Q

What is the type of numpy array?

A

numpy.ndarray

36
Q

How to find number of dimensions in a numpy array arr?

A

arr.ndim

37
Q

How to access elements in a 2-dim numpy array?

A

arr[3, 7]

38
Q

How to find all dimensions of a numpy array, arr?

A

arr.shape

39
Q

numpy: how does numpy.random.randint() function work?

A

With two arguments:
np.random.randint(low, high) # Returns a random number between low and high.

With three arguments:
np.random.randint(low, high, size=(4, 8))
Returns a 4x8 array of random nums

40
Q

numpy: How does numpy.random.randn() function work?

A

np.random.randn(5)
Returns an ndarray of 5 normally distributed nums.

np.random.randn(4, 8)
Returns a 2-D ndarray size (4, 8)

Normal distribution: Mean 0, variance 1

41
Q

How is a numpy ndarray stored in a file?

A

arr.tofile(‘abc.data’)
Type of data is inferred from type(arr[0]). This data type must be used while reading the file using numpy.fromfile() function.

Read a numpy ndarray from a file
np.fromfile(‘abc.data’, np.int64)
other types might be np.float64, np.complex128, np.complex64 etc.

42
Q

numpy: how are min/max/mean/abs of a numpy.array([])

A

import numpy as np
arr = np.array([101, 102, 103])

np.min(arr)
np.max(arr)
np.mean(arr)
np.abs(arr)

These functions can also be used with regular python lists.

43
Q

numpy: How to convert each element of an ndarray from complex128 to complex64?

A

Let arr_1 is of complex128 type.

arr_2 = arr_1.astype(np.complex64)

44
Q

anaconda: Create a virtual env with name env39 and python version 3.9

A

conda create –name env39 python=3.9

45
Q

anaconda: List all virtual environments

A

conda env list

46
Q

anaconda: How to install a module?

A

google: “conda install flask”. Use command from https://anaconda.org

conda install -c anaconda flask

47
Q

anaconda: How to remove a module?

A

conda remove pygame

48
Q

numpy: How does arange() work?

A

new_arr = np.arange(start, stop, step)
stop is excluded

default value of step is 1

np.arange(5)
array([0, 1, 2, 3, 4])

49
Q

np: How to create an ndarray?

A

use an existing regular python list.

lst_1 = [1, 3, 5, 7]

nd_arr1 = np.array(lst_1)

– or –

arr = np.array([1, 2, 3, 4 , 5])

arange is useful when interval is given, it is NOT calculated from end-points and number of elems.