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)