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

Python Interview Questions: Ans: .Py Files Contain The Source Code of A Program. Whereas, .Pyc File Contains

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
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
37 views4 pages

Python Interview Questions: Ans: .Py Files Contain The Source Code of A Program. Whereas, .Pyc File Contains

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
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

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.

7.How 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 hello.txt for writing?


Ans:Command:
f= open(“hello.txt”, “wt”)
f.write(“This is text”)
f.close()

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.

You might also like