Python Interview Questions

lella keerthi
5 min readJul 30, 2021

--

1. What is the Difference Between a Shallow Copy and Deep Copy?

Deepcopy creates a different object and populates it with the child objects of the original object. Therefore, changes in the original object are not reflected in the copy.

copy.deepcopy() creates a Deep Copy.

Shallow copy creates a different object and populates it with the references of the child objects within the original object. Therefore, changes in the original object are reflected in the copy. know more at Python online training

copy.copy creates a Shallow Copy.

2. How Is Multithreading Achieved in Python?

Multithreading usually implies that multiple threads are executed concurrently. The Python Global Interpreter Lock doesn’t allow more than one thread to hold the Python interpreter at that particular point of time. So multithreading in python is achieved through context switching. It is quite different from multiprocessing which actually opens up multiple processes across multiple threads.

3. Discuss Django Architecture.

Django is a web service used to build your web pages. Its architecture is as shown:

  • Template: the front end of the web page
  • Model: the back end where the data is stored
  • View: It interacts with the model and template and maps it to the URL
  • Django: serves the page to the user

4. What Advantage Does the Numpy Array Have over a Nested List?

Numpy is written in C so that all its complexities are backed into a simple to use a module. Lists, on the other hand, are dynamically typed. Therefore, Python must check the data type of each element every time it uses it. This makes Numpy arrays much faster than lists.

Numpy has a lot of additional functionality that list doesn’t offer; for instance, a lot of things can be automated in Numpy.

Q4.Python an interpreted language. Explain.

Ans: An interpreted language is any programming language which is not in machine-level code before runtime. Therefore, Python is an interpreted language.

Q5.What is pep 8?

Ans: PEP stands for Python Enhancement Proposal. It is a set of rules that specify how to format Python code for maximum readability.

Q6. How is memory managed in Python?

Ans: Memory is managed in Python in the following ways:

  1. Memory management in python is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have access to this private heap. The python interpreter takes care of this instead.
  2. The allocation of heap space for Python objects is done by Python’s memory manager. The core API gives access to some tools for the programmer to code. know more at Python training
  3. Python also has an inbuilt garbage collector, which recycles all the unused memory and so that it can be made available to the heap space.

Q7. What is namespace in Python?

Ans: A namespace is a naming system used to make sure that names are unique to avoid naming conflicts.

Q8. What is PYTHONPATH?

Ans: It is an environment variable which is used when a module is imported. Whenever a module is imported, PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load.

Q9. What are python modules? Name some commonly used built-in modules in Python?

Ans: 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

Q10.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.

11. What Is the Purpose of the Pass Statement?

The pass statement is used when there’s a syntactic but not an operational requirement. For example — The program below prints a string ignoring the spaces.

var=”Si mplilea rn”

for i in var:

if i==” “:

pass

else:

print(i,end=””)

Here, the pass statement refers to ‘no action required.’

12. How Will You Check If All the Characters in a String Are Alphanumeric?

Python has an inbuilt method isalnum() which returns true if all characters in the string are alphanumeric.

Example -

>> “abcd123”.isalnum()

Output: True

>>”abcd@123#”.isalnum()

Output: False

Another way is to use regex as shown.

>>import re

>>bool(re.match(‘[A-Za-z0–9]+$’,’abcd123’))

Output: True

>> bool(re.match(‘[A-Za-z0–9]+$’,’abcd@123’))

Output: False

13. How Will You Merge Elements in a Sequence?

There are three types of sequences in Python:

  • Lists
  • Tuples
  • Strings

Example of Lists -

>>l1=[1,2,3]

>>l2=[4,5,6]

>>l1+l2

Output: [1,2,3,4,5,6]

Example of Tuples -

>>t1=(1,2,3)

>>t2=(4,5,6)

>>t1+t2

Output: (1,2,3,4,5,6)

Example of String -

>>s1=“Simpli”

>>s2=“learn”

>>s1+s2

Output: ‘Simplilearn’

14. How Would You Remove All Leading Whitespace in a String?

Python provides the inbuilt function lstrip() to remove all leading spaces from a string.

>>“ Python”.lstrip

Output: Python

15. How Would You Replace All Occurrences of a Substring with a New String?

The replace() function can be used with strings for replacing a substring with a given string. Syntax:

str.replace(old, new, count)

replace() returns a new string without modifying the original string.

Example -

>>”Hey John. How are you, John?”.replace(“john”,“John”,1)

Output: “Hey John. How are you, John?

14) Is it mandatory for a Python function to return a value?

A 14) No

Q 15) Does Python have a main() function?

A 15) Yes, it does. It is executed automatically whenever we run a Python script. To override this natural flow of things, we can also use the if statement.

Q 16) What is GIL?

A 16) GIL or the Global Interpreter Lock is a mutex, used to limit access to Python objects. It synchronizes threads and prevents them from running at the same time. know more at Python online course

Q 17) Before the use of the ‘in’ operator, which method was used to check the presence of a key in a dictionary?

A 17) The has_key() method

Q 18) How do you change the data type of a list?

A 18) To change a list into a tuple, we use the tuple() function

To change it into a set, we use the set() function

To change it into a dictionary, we use the dict() function

To change it into a string, we use the .join() method

What are the key features of Python?

A 19) It is one of the common python interview questions. Python is an open-source, high-level, general-purpose

--

--

No responses yet