What are python modules? Name some commonly used built-in modules in Python?
Python modules are files containing Python code. This code can either be functions classes or variables. A Python module is a .py file containing executable code.
Some of the commonly used built-in modules are:
-
os
-
sys
-
math
-
random
-
data time
-
JSON
What is Dict and List comprehensions are?
They are syntax constructions to ease the creation of a Dictionary or List based on existing iterable.
What are the built-in type does python provides?
There are mutable and Immutable types of Pythons built-in types Mutable built-in types
-
List
-
Sets
-
Dictionaries
Immutable built-in types
-
Strings
-
Tuples
-
Numbers
What are local variables and global variables in Python?
Global Variables: Variables declared outside a function or in global space are called global variables. These variables can be accessed by any function in the program.
Local Variables: Any variable declared inside a function is known as a local variable. This variable is present in the local space and not in the global space.
Example:
a=2
def add():
b=3
c=a+b
print(c)
add()
Output: 5
When you try to access the local variable outside the function add(), it will throw an error.
Is python case sensitive?
A language is case-sensitive if it distinguishes between identifiers like myname and Myname. In other words, it cares about case-lowercase or uppercase. Let’s try this with Python.
>>> myname='Ayushi'
>>> Myname
Traceback (most recent call last):
File “<pyshell#3>”, line 1, in <module>
Myname
NameError: name ‘Myname’ is not defined
As you can see, this raised a NameError. This means that Python is indeed case-sensitive.
What is type conversion in Python?
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
hex() – converts integers to hexadecimal
oct() – converts an 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 an integer into a string.
complex(real, imag) – This function converts real numbers to complex(real, imag) number.
Explain help() and dir() functions in Python.
The help() function displays the documentation string and helps for its argument.
>>> import copy
>>> help(copy.copy)
Help on function copy in module copy:
copy(x)
Shallow copy operation on arbitrary Python objects.
See the module’s __doc__ string for more info.
The dir() function displays all the members of an object(any kind).
>>> dir(copy.copy)
[‘__annotations__’, ‘__call__’, ‘__class__’, ‘__closure__’, ‘__code__’, ‘__defaults__’, ‘__delattr__’, ‘__dict__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__get__’, ‘__getattribute__’, ‘__globals__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__kwdefaults__’, ‘__le__’, ‘__lt__’, ‘__module__’, ‘__name__’, ‘__ne__’, ‘__new__’, ‘__qualname__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’]
Whenever you exit Python, is all memory de-allocated?
The answer here is no. The modules with circular references to other objects, or to objects referenced from global namespaces, aren’t always freed on exiting Python.
Plus, it is impossible to de-allocate portions of memory reserved by the C library.
What is monkey patching?
Dynamically modifying a class or module at run-time.
>>> class A:
def func(self):
print("Hi")
>>> def monkey(self):
print "Hi, monkey"
>>> m.A.func = monkey
>>> a = m.A()
>>> a.func()
Hi, monkey
What is a dictionary in Python?
A python dictionary is something I have never seen in other languages like C++ or Java programming. It holds key-value pairs.
>>> roots={25:5,16:4,9:3,4:2,1:1}
>>> type(roots)
<class ‘dict’>
>>> roots[9]
3
A dictionary is mutable, and we can also use a comprehension to create it.
>>> roots={x**2:x for x in range(5,0,-1)}
>>> roots
{25: 5, 16: 4, 9: 3, 4: 2, 1: 1}
.png)