ptn Flashcards
How to install pip3 in Ubuntu?
sudo apt-get install python3-pip
How to install PyQt5 on Ubuntu?
sudo -H python3 -m pip install pyqt5
Python: if an instance method is called with three arguments, how will the method signature look?
Signature will have four arguments, with ‘self’ being the first argument.
Python: How are ‘class methods’ defined?
They are decorated with @classmethod.
First argument in their signature is ‘cls’ representing the class, NOT an instance/self.
Python: What are ‘static methods’?
These are decorated with @staticmethod. Arguments at the time of calling and method signature exactly match. No ‘self’ or ‘cls’ argument is implicitly passed.
Python: What is an ‘iterable’?
Iterable is a sequence data structure, like a list, tuple etc.
Python: Write generator for even numbers less then 20
(i for i in range(20) if i%2 == 0)
Python: Can a generator be used again, after it is made use of?
No, once you iterate over a generator, you must define it again, if you want to use it again.
Python: What are instance attributes and class attributes?
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.
What is special about class ‘object’ in python?
Every class in python is derived from ‘object’ class.
Python: What happens when you assign an object to another variable?
New variable name is a new reference to original object.
Any change in original object will be visible through the new variable.
Python: How to add an item at the end of a list?
lst.append(“new item”)
Python: How to remove an item from an index of a list?
removed_item = lst.pop(index)
lst.pop() without an argument will remove last item.
Python: How is dir() used with modules?
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.
What is a python module?
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.
What is a python package?
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
Python: What happens when you append b in front of a string, like b’ABCD1234’
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.
Python: What module is used to do multithreading?
import threading
Python: How to find the length of a list?
len(list_name)
What is the diff between
pip install django and
pip –user install django
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.
Python: How to list all packages installed?
pip list
Python: How to get info about an installed package?
pip show django
Python: How to search PyPI.org repository for a package?
pip search pyqt
Python: How to uninstall a package?
pip3 uninstall flask
Python: How to get a snapshot of all packages installed in a system.
pip freeze > requirements.txt
These packages can be installed in another system with,
pip install -r requirements.txt
Python: After importing a module, how to find its path in file system?
import mod
mod.__file__
Python: How to find all variables (names) known?
dir()
returns a list of strings.
Python: How to find all variables (names) provided by an imported module?
import mod
dir(mod)
returns a list of strings.
Python: How to find type of a variable?
type(var_name)
print( type(var_name) )
Python: How do u define a complex number?
z = 3.14 + 2.71j
– or –
z = complex(3.14, 2.71)
type(z) = < class ‘complex’ >
complex(3) = 3 + 0j
complex() = 0 + 0j
Python: Real & imaginary parts of a complex number z?
z.real
z.imag
Python: Conjugate of a complex number z?
z.conjugate()
Python: Absolute value of a complex number z?
abs(z)
How to create a numpy array
Use numpy array() function.
import numpy as np
arr = np.array([13, 17, 19, 23])
What is the type of numpy array?
numpy.ndarray
How to find number of dimensions in a numpy array arr?
arr.ndim
How to access elements in a 2-dim numpy array?
arr[3, 7]
How to find all dimensions of a numpy array, arr?
arr.shape
numpy: how does numpy.random.randint() function work?
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
numpy: How does numpy.random.randn() function work?
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
How is a numpy ndarray stored in a file?
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.
numpy: how are min/max/mean/abs of a numpy.array([])
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.
numpy: How to convert each element of an ndarray from complex128 to complex64?
Let arr_1 is of complex128 type.
arr_2 = arr_1.astype(np.complex64)
anaconda: Create a virtual env with name env39 and python version 3.9
conda create –name env39 python=3.9
anaconda: List all virtual environments
conda env list
anaconda: How to install a module?
google: “conda install flask”. Use command from https://anaconda.org
conda install -c anaconda flask
anaconda: How to remove a module?
conda remove pygame
numpy: How does arange() work?
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])
np: How to create an ndarray?
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.