Intermediate Level

What is __init__ in Python?

_init_ methodology is a reserved method in Python aka constructor in
OOP. When an object is created from a class and _init_ methodology is
called to access the class attributes.

What are the tools present to perform static analysis?

The two static analysis tools used to find bugs in Python are Pychecker
and Pylint. Pychecker detects bugs from the source code and warns
about its style and complexity. While Pylint checks whether the module
matches up to a coding standard.

What is pass in Python?

Pass is a statement that does nothing when executed. In other words, it
is a Null statement. This statement is not ignored by the interpreter,
but the statement results in no operation. It is used when you do not
want any command to execute but a statement is required.

How can an object be copied in Python?

Not all objects can be copied in Python, but most can. We can use the
“=” operator to copy an object to a variable.
ex:
var=copy.copy(obj)

How can a number be converted to a string?

The inbuilt function str() can be used to convert a number to a string.

What are modules and packages in Python?

Modules are the way to structure a program. Each Python program file
is a module, importing other attributes and objects. The folder of a
program is a package of modules. A package can have modules or
subfolders.

What is the object() function in Python?

In Python, the object() function returns an empty object. New
properties or methods cannot be added to this object.

What is the difference between NumPy and SciPy?

NumPy stands for Numerical Python while SciPy stands for Scientific
Python. NumPy is the basic library for defining arrays and simple
mathematical problems, while SciPy is used for more complex
problems like numerical integration and optimization and machine
learning and so on.

Define encapsulation in Python?

Encapsulation means binding the code and the data together. A Python
class for example.

What is the type () in Python?

type() is a built-in method that either returns the type of the object or
returns a new type of object based on the arguments passed.
ex:

a = 100
type(a)
o/p: int

What is the split() function used for?

Split function is used to split a string into shorter strings using defined
separators.

letters= ('' A, B, C”)
n = text.split(“,”)
print(n)
o/p: [‘A’, ‘B’, ‘C’ ]

What are the built-in types does python provide?

Python has following built-in data types:
Numbers: Python identifies three types of numbers:
1. Integer: All positive and negative numbers without a fractional
part
2. Float: Any real number with floating-point representation
3. Complex numbers: A number with a real and imaginary
component represented as x+yj. x and y are floats and j is
1(square root of -1 called an imaginary number)
Boolean: The Boolean data type is a data type that has one of two
possible values i.e. True or False. Note that ‘T’ and ‘F’ are capital letters.
String: A string value is a collection of one or more characters put in
single, double or triple quotes.
List: A list object is an ordered collection of one or more data items
that can be of different types, put in square brackets. A list is mutable
and thus can be modified, we can add, edit or delete individual
elements in a list.
Set: An unordered collection of unique objects enclosed in curly
brackets
Frozen set: They are like a set but immutable, which means we cannot
modify their values once they are created.
Dictionary: A dictionary object is unordered in which there is a key
associated with each value and we can access each value through its
key. A collection of such pairs is enclosed in curly brackets. For
example {‘First Name’: ’Tom’, ’last name’: ’Hardy’} Note that Number
values, strings, and tuples are immutable while List or Dictionary
objects are mutable.

What is docstring in Python?

Python docstrings are the string literals enclosed in triple quotes that
appear right after the definition of a function, method, class, or module.
These are generally used to describe the functionality of a particular
function, method, class, or module. We can access these docstrings
using the __doc__ attribute.
Here is an example:

def square(n):
'''Takes in a number n, returns the square of n'''
return n**2
print(square.__doc__)

Ouput: Takes in a number n, returns the square of n

How to Reverse a String in Python?

In Python, there are no in-built functions that help us reverse a string.
We need to make use of an array slicing operation for the same.

str_reverse = string[::-1]