0% found this document useful (0 votes)
14 views4 pages

Python Interview Questions Explained

Python interview questions cover topics like the difference between .py and .pyc files, automatic memory management in Python, the latest Python version, how to declare lists and tuples, the break and pass statements, modules, installing packages with pip, decorators, recursion, opening and reading/writing files, the os module, declaring functions, SSH modules, the Global Interpreter Lock (GIL), negative indexes, Python being an interpreted language, and Python namespaces. Key points are that .pyc files contain bytecode, memory is managed automatically, the latest version is 3.9, lists are mutable and tuples immutable, break exits a loop and pass is an empty block, modules contain reusable code, pip installs packages, decorators add functionality,

Uploaded by

Johin Shaik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Python Interview Questions Explained

Python interview questions cover topics like the difference between .py and .pyc files, automatic memory management in Python, the latest Python version, how to declare lists and tuples, the break and pass statements, modules, installing packages with pip, decorators, recursion, opening and reading/writing files, the os module, declaring functions, SSH modules, the Global Interpreter Lock (GIL), negative indexes, Python being an interpreted language, and Python namespaces. Key points are that .pyc files contain bytecode, memory is managed automatically, the latest version is 3.9, lists are mutable and tuples immutable, break exits a loop and pass is an empty block, modules contain reusable code, pip installs packages, decorators add functionality,

Uploaded by

Johin Shaik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Interview Questions

1. What is the difference between .py and .pyc files?


Ans: .py files contain the source code of a program. Whereas, .pyc file contains
the bytecode of your program. We get bytecode after compilation of .py file
(source code). .pyc files are not created for all the files that you run. It is only
created for the files that you import.
Before executing a python program python interpreter checks for the compiled
files. If the file is present, the virtual machine executes it. If not found, it checks
for .py file. If found, compiles it to .pyc file and then python virtual machine
executes it.
Having .pyc file saves you the compilation time.

2. What is Automatic memory management in python?


Ans: Python automatically allocates and reclaims (" garbage collects") objects
when no longer used and it grows and shrink on demand. The memory allocated
by the python memory manager is in form of a private heap space dedicated for
Python. Python keeps track of low-level memory details using this. All Python
objects are stored in this heap and being private, it is inaccessible to the
programmer.

3. Which is latest Python version released?


Ans: Python 3.9 (which version Did you use? Maybe python 2.7, 3.5 etc)

4. How do you declare a list and a tuple in python


Ans:
List = ["Geeks", "For", "Geeks"]
tuple = ('sara', 6, 5, 0.97)

list are mutable . easy to change or modify


tuple are non mutable. Not prone to easily modify

5. What is break and pass in Python?

Ans:
Break Statement terminates the Loop immediately.(Come out of a loop)
Pass : insert an empty block
Example:
pat = [1, 3, 2, 1, 2, 3, 1, 0, 1, 3]

for p in pat:
pass
if (p == 0):
current = p
break
elif (p % 2 == 0):
continue

6. What are modules in Python?


Ans: Modules, in general, are simply Python files with a .py extension and can
have a set of functions, classes or variables defined and implemented. They can
be imported and initialized once using the import statement.

[Link] can you install python package? Which command is used?


Ans: Can be installed using PIP Command Ex: pip install wget

8. What are decorators in Python?


Ans: Decorators in Python are essentially function that add functionality to an existing function
in Python.

9 . What is recursion? What is risk involved in using recursive functions?


Ans: Recursion is a function calling itself one or more times in its body.

One very important condition a recursive function should have to be used in a program is, it should
terminate, else there would be a problem of an infinite loop

10. Write a command to open the file [Link] for writing?


Ans:Command:
f= open(“[Link]”, “wt”)
[Link](“This is text”)
[Link]()

11. Which python built in module can be used to perform operation such as deleting or rename
a file?
Ans: Built in module called “os”

12. how to declare function in python?


Ans:

def sum_of_twonumbers(a,b):
print(a+b)

sum_of_twonumbers (4,5) --- > call this function

13. Name any python modules which can be used to remotely SSH

Ans: ssh2 or paramiko

14. what is purpose of GIL in python


Ans: Global Interpreter Lock (GIL). The GIL ensures that only one of your ‘threads’ can execute at one
time in multi treaded application.

15. What are negative indexes and why are they used?
Ans: Negative indexes are the indexes from the end of the list or tuple or string. They cab be used to
access ending elements of list or tuple.

16. What is an Interpreted language?

Ans: An Interpreted language executes its statements line by line. Languages such as Python,
Javascript, R, PHP and Ruby are prime examples of Interpreted languages. Programs written in an
interpreted language runs directly from the source code, with no intermediary compilation step.

17. What are Python namespaces? Why are they used?


A namespace in Python ensures that object names in a program are unique and can be used without
any conflict. Python implements these namespaces as dictionaries with 'name as key' mapped to a
corresponding 'object as value'. This allows for multiple namespaces to use the same name and map
it to a separate object.

A few examples of namespaces are as follows:

Local Namespace includes local names inside a function. the namespace is temporarily created for a
function call and gets cleared when the function returns.
Global Namespace includes names from various imported packages/ modules that is being used in
the current project. This namespace is created when the package is imported in the script and lasts
until the execution of the script.

Built-in Namespace includes built-in functions of core Python and built-in names for various types of
exceptions.

Common questions

Powered by AI

Modules in Python are files with a .py extension containing predefined functions, classes, and variables that can be reused across different programs. They extend Python's functionality by allowing code organization into logical collections. Modules are typically utilized by importing them into a program using the 'import' statement, enabling access to their functionalities and reducing code duplication .

Python's automatic memory management system allocates and deallocates memory automatically by employing a private heap space dedicated to Python objects. This mechanism, known as garbage collection, ensures that objects no longer in use are reclaimed, thereby freeing up resources. The primary advantage of this system is that it abstracts memory management details from the programmer, reducing the likelihood of memory leaks and freeing them to focus on other aspects of program development .

Recursion in Python is the process where a function calls itself to solve smaller instances of a problem. A major risk associated with recursion is the potential for the function to enter an infinite loop if a proper base case is not defined, leading to stack overflow errors and thus inefficiencies or program crashes .

Namespaces in Python are implemented as dictionaries that map object names to objects, ensuring that names are unique within their context to prevent conflicts. This concept allows objects with the same name to exist in different namespaces, such as local, global, and built-in namespaces, thus providing a structured way to manage variable scope and program organization .

Decorators in Python are functions that allow the enhancement of other functions without permanently modifying their external structure. By wrapping another function, decorators can extend functionality, such as modifying input arguments or outputs and managing function execution times, thereby improving code modularity and reusability .

The key differences between a list and a tuple in Python are mutability and syntax. Lists are mutable and defined with square brackets, allowing modification after creation, making them suitable for collections that require frequent changes. Tuples, defined with parentheses, are immutable, providing performance advantages due to their static nature and proving useful for fixed data sequences where data integrity is crucial .

Negative indexes in Python allow for accessing elements from the end of a sequence, such as a list, tuple, or string. They are particularly useful for quickly retrieving or manipulating elements at the end of a sequence without needing to calculate the positive index, which can simplify code when dealing with dynamically sized collections .

Python is referred to as an interpreted language because its code is executed line by line at runtime by the interpreter, which means there is no need for prior compilation into machine-level code. This facilitates a more flexible and dynamic programming environment, allowing easier debugging and rapid prototyping but may also result in slower execution compared to compiled languages .

.py files contain the source code of a Python program, whereas .pyc files contain the compiled bytecode of the program. The advantage of using .pyc files is that they save compilation time as they are executed directly by the Python virtual machine if available, bypassing the need to recompile the source code [.py files].

The Global Interpreter Lock (GIL) in Python is a mutex that ensures only one thread executes Python bytecode at a time in a multi-threaded program. While it simplifies the implementation of CPython and allows thread-safe memory management, it can be a limitation for CPU-bound programs because it prevents the full exploitation of multi-core processors, as only one thread runs at a time .

You might also like