Python Interview Questions

  1) What is the difference between a module and a package in Python?

A 1) Each Python program file is a module that imports other modules like objects. Thus, a module is a way to structure the program. The folder of a Python program is called a package of modules. know more at Python online training

Q 2) What are the built-in types available in Python?

A 2) One of the most common python interview question, There are mutable and immutable built-in types.

The mutable ones include:

  • List
  • Sets
  • Dictionaries

The immutable types include:

  • Strings
  • Tuples
  • Numbers

Q 3) What is lambda function in Python?

A 3) It is often used as an inline function and is a single expression anonymous function. It is used to make a new function object and return them at runtime.

Lambda is an anonymous function in Python that can accept any number of arguments and can have any number of parameters. However, the lambda function can have only a single expression or statement. Usually, it is used in situations that require an anonymous function for a short time period. Lambda functions can be used in either of the two ways: online training on python

Here’s an example of the lambda function:

a = lambda x,y : x+y 

print(a(5, 6))

Output: 11

Q 4) What is meant by namespace?

A namespace refers to a naming system that is used to ensure that all object names in a Python program are unique, to avoid any conflicts. In Python, these namespaces are implemented as dictionaries with ‘name as key’ mapped to a corresponding ‘object as value.’ As a result, multiple namespaces can use the same name and map it to a different object. 

Below are the three types of namespaces in Python: 

  • Local namespace – It includes local names inside a function. A local namespace is temporarily created for a function call and is cleared when the function returns.
  • Global namespace – It consists of the names from various imported packages/ modules that are currently being used in a project. A global namespace is created when a package is imported in the script, and it lasts until the script is executed.
  • Built-in namespace – It includes built-in functions of core Python and built-in names for the different types of exceptions.

Q 5 ) Explain the difference between a list and a tuple?

A 5) Any Python Interview Question and Answers guide won’t complete without this question. The list is mutable while the tuple is not. Tuples can be hashed as in the case of making keys for dictionaries.

Q 6) Difference between pickling and unpickling?

Any Python Interview Question and Answers guide won’t complete without this question. In Python, the pickle module accepts any Python object, transforms it into a string representation, and dumps it into a file by using the dump function. This process is known as pickling. The function used for this process is pickle.dump().

On the other hand, the process of retrieving the original Python object from the stored string representation is called unpickling. The function used for this process is pickle.load().

Q 7) What are decorators in Python?

A 7) A Python decorator is a specific change made in the Python syntax for the easy alteration of functions.

Q 8) Difference between generators and iterators?

A 8) In Python, iterators are used to iterate over a group of elements (in a list, for example). The way of implementing these iterators is known as generators. It yields an expression in the function, but otherwise behaves like a normal function.

Q 9) How to convert a number into a string?

A 9) One of the most common python interview questions. We can use the inbuilt str() function. For an octal or hexadecimal representation, we can use the other inbuilt functions like oct() or hex().

Q 10) What is the use of the // operator in Python?

A 10) Using the // operator between 2 numbers gives the quotient when the numerator is divided from the denominator. It is called the Floor Division operator. It is one of the general questions from the Python interview questions and answers guide.

Q 11) Does Python have a Switch or Case statement like in C?

A 11) No, it does not. However, we can make our own Switch function and use it. 

Q 12) What is the range() function and what are its parameters?

A 12) The range() function is used to generate a list of numbers. Only integer numbers are allowed, and hence, parameters can be both negative and positive. The following parameters are acceptable: know more at online training in python

range(stop)

Where ‘stop’ is the no. of integers to generate, starting from 0. Example: range(5) == [0,1,2,3,4]

range([start], stop[, step])

Start: gives the starting no. of the sequence

Stop: specifies the upper limit for the sequence

Step: is the incrementing factor in the sequence

Q 13) What is the use of %s?

A 13) %s is a format specifier which transmutes any value into a string.

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

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

Q 19) 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 Since it is a general-purpose programming language and it comes with an assortment of libraries, you can use Python for developing almost any type of application. know more at python online training Hyderabad

Some of its key features are:

  • Interpreted
  • Dynamically-typed
  • Object-oriented
  • English-like syntax

Q 20) Explain memory management in Python.

A 20) In Python, the Python Memory Manager takes care of memory management. It allocates the memory in the form of a private heap space that stores all Python objects and data structures,

This private space is inaccessible to the programmer. However, the core API allows the programmer to access some tools for coding purposes. Plus, Python is equipped with an in-built garbage collector that recycles the unused memory for the private heap space.

Q 21) What is PYTHONPATH?

A 21) PYTHONPATH is an environment variable that is used to incorporate additional directories when a module/package is imported. Whenever a module/package is imported, PYTHONPATH is used to check if the imported modules are present in the existing directories

Q 22) Is Python case-sensitive?

A 22) A programming language is deemed to be case-sensitive if it distinguishes between identifiers like “myname” and “Myname.” In simple words, it cares about the case – lowercase or uppercase. 

Let’s see an example:

  1. >>> myname=’John’
  2. >>> Myname

Traceback (most recent call last):

File “<pyshell#3>”, line 1, in <module>

Myname

NameErrorname ‘Myname’ is not defined

Since it raises a NameError, it means that Python is a case-sensitive language.

 23) Explain the use of “help()” and “dir()” functions.

A 23) One of the most common question in any Python interview question and answers guide. In Python, the help() function is used for showing the documentation of modules, classes, functions, keywords, and so on. If the help() function receives no parameter, it launches an interactive help utility on the console. know more at Python training

The dir() function is used to return a valid list of attributes and methods of the object it is called upon. Since the function aims to produce the most relevant data (instead of showing the complete information), it behaves differently with different objects:

  • For modules/library objects, the dir() function returns a list of all attributes contained in that module.
  • For class objects, the dir() function returns a list of all valid attributes and base attributes.
  • When no parameters are passed to it, the dir() function returns a list of attributes in the current scope.

Comments

Popular posts from this blog

What is Azure?

SharePoint Interview Questions

Selenium Interview Questions