IZETAM TECHNOLOGIES - Interview Questions of Python For 2 Years Experienced
IZETAM TECHNOLOGIES - Interview Questions of Python For 2 Years Experienced
1. What is Python?
Python is a high-level, interpreted, interactive, and object-oriented scripting language. It uses English keywords
frequently. Whereas, other languages use punctuation, Python has fewer syntactic constructions.
Python is designed to be highly readable and compatible with different platforms such as Mac, Windows, Linux,
Raspberry Pi, etc.
Lists Tuples
Lists are mutable, i.e., they can be edited Tuples are immutable (they are lists that cannot be edited)
Lists are usually slower than tuples Tuples are faster than lists
Lists consume a lot of memory Tuples consume less memory when compared to lists
Lists are less reliable in terms of errors as Tuples are more reliable as it is hard for any unexpected
unexpected changes are more likely to occur change to occur
Lists consist of many built-in functions. Tuples do not consist of any built-in functions.
Syntax: Syntax:
4. What is pep 8?
PEP in Python stands for Python Enhancement Proposal. It is a set of rules that specify how to write and design Python code for
maximum readability.
Memory in Python is managed by Python private heap space. All Python objects and data structures are located in a private
heap. This private heap is taken care of by Python Interpreter itself, and a programmer doesn’t have access to this private heap.
1. Python memory manager takes care of the allocation of Python private heap space.
2. Memory for Python private heap space is made available by Python’s in-built garbage collector, which recycles and frees
up all the unused memory.
6. What is PYTHONPATH?
PYTHONPATH has a role similar to PATH. This variable tells Python Interpreter where to locate the module files imported into a
program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH
is sometimes preset by Python Installer.
os
sys
data time
math
random
JSON
Local Namespace consists of local names inside a function. It is temporarily created for a function call and gets cleared
once the function returns.
Global Namespace consists of names from various imported modules/packages that are being used in the ongoing
project. It is created once the package is imported into the script and survives till the execution of the script.
Built-in Namespace consists of built-in functions of core Python and dedicated built-in names for various types of
exceptions.
Hello, my age is 22
Number
String
Tuple
List
Dictionary
set
After that, install it on your PC by looking for the location where PYTHON has been installed on your PC by
executing the following command on command prompt;
Visits advanced system settings and after that add a new variable and name it as PYTHON_NAME and paste
the path that has been copied.
Search for the path variable -> select its value and then select ‘edit’.
Add a semicolon at the end of the value if it’s not present and then type %PYTHON_HOME%
List Array
Consists of elements belonging to different data Consists of only those elements having the same
types data type
No need to import a module for list declaration Need to explicitly import a module for array
declaration
Can be nested to have different type of elements Must have all nested elements of the same size
Recommended to use for shorter sequence of data Recommended to use for longer sequence of data
items items
More flexible to allow easy modification (addition or Less flexible since addition or deletion has to be
deletion) of data done element-wise
Consumes large memory for the addition of Comparatively more compact in memory size while
elements inserting elements
Can be printed entirely without using looping A loop has to be defined to print or access the
components
Syntax: Syntax:
list = [1,”Hello”,[‘a’,’e’]] import array
array_demo = array.array(‘i’, [1, 2, 3])
(array as integer type)
21. How can you randomize the items of a list in place in Python?
This can be easily achieved by using the Shuffle () function from the random
25. Explain split (), sub (), subn () methods of “re” module in Python?
These methods belong to the Python Reg Ex or‘re’ module and are used to modify strings.
Split (): This method is used to split a given string into a list.
Sub (): This method is used to find a substring where a regex pattern matches, and then it replaces the
matched substring with a different string.
Sub (): This method is similar to the sub () method, but it returns the new string, along with the number of
replacements.
String Literals
These literals are formed by enclosing text in the single or double quotes.
For Example:
“Intellipaat”
‘45879’
Numeric Literals
Integer: I=10
Float: i=5.2
Complex: 1.73j
Boolean Literals
Boolean literals help to denote Boolean values. It contains either True or False.
x=True
For opening a text file using the above modes, we will have to append‘t’ with them as follows:
Similarly, a binary file can be opened by appending ‘b’ with them as follows:
To append the content in the files, we can use the append mode (a):
33. What do file-related modules in Python do? Can you name some file-related modules in Python?
Python comes with some file-related modules that have functions to manipulate text files and binary files in a file
system. These modules can be used to create text or binary files, update their content, copy, delete, and more.
Some file-related modules are os, os. Path and shuttle. The OS Path module has functions to access the file system,
while the shutil.os module can be used to copy or delete files.
is: returns the true value when both the operands are true (Example: “x” is ‘x’)
Not: returns the inverse of the Boolean value based upon the operands (example:”1” returns “0” and vice-
versa.
In: helps to check if the element is present in a given Sequence or not.
35. Whenever Python exits, why isn’t all the memory de-allocated?
Whenever Python exits, especially those Python modules which are having circular references to other
objects or the objects that are referenced from the global namespaces are not always de-allocated or freed.
It is not possible to de-allocate those portions of memory that are reserved by the C library.
On exit, because of having its own efficient clean up mechanism, Python would try to de-allocate every
object.
39. What advantages do NumPy arrays offer over (nested) Python lists?
Nested Lists:
Python lists are efficient general-purpose containers that support efficient operations like insertion,
appending, deletion and concatenation.
The limitations of lists are that they don’t support “vectorized” operations like element wise addition and
multiplication, and the fact that they can contain objects of differing types means that Python must store type
information for every element, and must execute type dispatching code when operating on each element.
The pass keyword represents a null operation in Python. It is generally used for the purpose of filling up empty
blocks of code which may execute during runtime but has yet to be written. Without the pass statement in the
following code, we may run into some errors during code execution.
47. What is docstring in Python?
Documentation string or docstring is a multiline string used to document a specific code segment.
The docstring should describe what the function or method does.
Output
25
Here lambda function adds 20 to the argument and prints the result.
Shallow copy: - In shallow copy when we copy one object into another object, then whatever we perform any
changes in the second object, it will also reflect into the first object. To perform shallow copy we have “copy ()”
function.
deep copy () :- In a deep copy, when we copy one object into another object, whatever we perform, any
changes in the second object will not reflect into the first object. To perform deep copy we have a “deepcopy ()”
function.