Python Interview Questions

Introduction to Python:

Python  is one of the most used  programming languages and is interpreted in the nature thereby providing the flexibility of incorporating dynamic semantics. Python  is a free and open-source language with very  clean and simple syntax. This makes it  very easy for developers to learn python. Python  supports object-oriented programming and is most widely  used to perform general-purpose programming. 

Here is the list of most  commonly asked python interview question and answers:

Python Interview Questions for beginners

1. Describe  Python? What are the advantages of using Python ?

Python is  interpreted, high-level, general-purpose programming language. As a  general-purpose language, it can also be used to build any type of application using  the right tools/libraries. In addition python supports objects, threads, modules,exception,handling,  automatic memory management which help in  modelling the  real-world problems and building applications to resolve these problems.Advantages of Python:

  • Python is  general-purpose programming language which is a simple, easy-to-learn syntax , emphasizes readability and  reduces the cost of program maintenance. The language is also  capable of scripting, completely open-source, and supports all third-party packages encouraging modularities and code reuse.
  • Python is high-level data structures, combined with the dynamic typing and dynamic binding, attracting a huge community of developers for the  Rapid Application Development and deployment.

2. What is meant by dynamically typed language?

Before knowing about  dynamically typed language, we should understand about what is typing . Typing refers to  type-checking in the programming languages. In strongly-typed language, such as Python, “1” + 2 will result in a type error since these languages don’t allow for “type-coercion” .Type-checking can be done stage wise:

  • Static – static Data Types are checked before execution.
  • Dynamic -dynamic  Data Types are checked during execution.

Python is  interpreted language, executes at each statement line and  type-checking is done during execution. Hence, Python is called as Dynamically Typed Language.

3. Describe an Interpreted language?

 Interpreted language executes its statements line by line.  programming Languages like Python,R, Javascript,  PHP, and Ruby are prime examples of the Interpreted languages. Programs written in  interpreted language runs directly from  source code, with no intermediary compilation steps.

4. Explain PEP 8 and why is it considered important?

PEP stands for the  Python Enhancement Proposal.  PEP is an official design documented  providing information to  Python community, or describing a  feature for Python or also  its processes. PEP 8 is especially useful ,since it documents the style guidelines for the Python Code. Apparently contributing to  Python open-source community requires you to follow above style guidelines strictly and sincerely.

5. Scope in the Python?

Every object in the Python functions within a scope. Scope is  block of code where an object in the  Python remains relevant. Name spaces uniquely identify all the  above objects inside a program.These namespaces also have a scope defined  where you could use their objects without using any prefix. A few examples of this scope created during the code execution in Python are :

  • local scope refers to  local objects available in  current function.
  • global scope refers to objects available throughout  code execution since their inception.
  • module-level scope refers to  global objects of current module accessible in  program.
  • An outermost scope refers to all  built-in names callable in  program. The objects  used in this scope are searched at last to find the name referenced.

6. What are the lists and tuples? What is  key difference between the two terms?

Both Lists and Tuples are  sequence data types that allows to   store a collection of objects in the Python. Objects stored in both the sequences can have different data types. Here lists are represented with square brackets ['sara', 6, 10.19], while tuples represented with parantheses ('ansh', 5, 0897).
But the real difference between the two?The key difference between the two is while lists are mutabletuples  are immutable objects. This means that the lists can be modified, appended or sliced on  go but tuples remain constantly  and cannot be modified in any manner. You can run  following example on Python IDLE to confirm the generated difference:

my_tuple = ('sarada', 6, 4, 0.97)
my_list = ['sarada', 6, 4, 0.97]
print(my_tuple[0])     # output => 'sarada'
print(my_list[0])     # output => 'sarada'
my_tuple[0] = 'anshu'    # modifying tuple => throws an error
my_list[0] = 'anshu'    # modifying list => list modified
print(my_tuple[0])     # output => 'sarada
print(my_list[0])     # output => 'anshu'

7. What are common built-in data types  used in Python?

Several built-in data types  are there in Python. Although, Python doesn’t require  any data types to be defined explicitly during the variable declaration type errors are likely to occur if the knowledge of the data types and their compatibility with each other been neglected. Python provides isinstance() and type()  functions to check  type of these variables. These  type of data  can be grouped into  following categories-

  • None Type:
    None keyword  which represents the null values inthe  Python. Boolean equality operation can  also be performed using this None Type objects.
Class Name: Description:
NoneType Represents  NULL values in Python.
  • Numeric Types:
    These are three different numeric types – integers, numbers,floating-point  and complex numbers. Additionally, booleans are  sub-type of integers.
Class Name Description
int Stores integer are literals including  octal,hex and binary num as integers
float Stores literals  which containing decimal values and exponent signs as floating-point no’s
complex Stores complex numbers in  form (A + Bj) and has attributes: imag and real
bool Store boolean value (True / False).

sequence Types:

  • According to the Python Docs, there are three most  basic Sequence Types –  tuples, lists,and range objects. Sequence types consist the in and not in operators defined for their traversing  elements. These operators shares   same priority as the comparison operations.
Class Name Description
list Mutable sequence used to store the collection of items.
tuple Immutable sequence used to store the collection of items.
range Represents an immutable sequence of the numbers generated during execution.
str Immutable sequence ofthe  Unicode code points to store textual data.

 The standard library also includes additional types of  processing:
1. Binary data and
2. Text string

  • Mapping Types:

Mapping object can  be map hashable values to the random objects in the Python. Mapping objects are mutable and  also there is currently one and only  standard mapping type, the dictionary.

Class Name Description:
dict Stores are comma-separated list of key: value pairs
  • Set Types:
    Python has 2 built-in set types – set and frozensetset type mutable and supports methods like add() and remove()frozenset type  immutable and can’t be modified after creating.

Note: set is mutable and thus cannot be used as key for a dictionary. On the other hand, frozenset is immutable and thus, hashable, and can be used as a dictionary key or as an element of another set.

  • Modules:
    Module is an additional built-in type supported by the Python Interpreter. It supports one special operation, i.e., attribute accessmymod.myobj, where mymod is a module and myobj references a name defined in m’s symbol table. The module’s symbol table resides in a very special attribute of the module __dict__, but direct assignment to this module is neither possible nor recommended.
  • Callable Types:
    Callable types are the types to which function call can be applied. They can be user-defined functions, instance methods, generator functions, and some other built-in functions, methods and classes.
    Refer to the documentation at for a detailed view of the callable types.

8. Describe pass in Python?

The pass keyword is represents a null operation in Python. which  is generally used for  purpose of filling up empty blocks of code which may be execute during runtime but has  to be written. Without  pass statement in  following code, we may run into some type of errors during code execution.

def myEmptyFunc():
   # do nothing
myEmptyFunc()    # nothing happens
## Without the pass keywords
# File "<stdin>", line 2
# IndentationError: expected an indented blocks

9. Explain  modules and packages in the Python?

Python packages and Python modules are of  two mechanisms that permits  for modular programming in Python. Modularizing has  several benfits

  • Simplicity: Working on  single module helps you to focus on relatively small portion of  problem at hand. This makes the development easier and less errors.
  • Maintainability: Modules are designed to enforce the logical boundaries between the different problem domains. If they are written in  manner that reduces the interdependency, it is always less likely that modifications in  module might impact other parts of  program.
  • Reusability: Functions defined in  module can be easily reused by other parts of  application.
  • Scoping: Modules typically defines a separate namespace, which  can help to avoid confusion between identifiers from other parts of program.

In general MODULES , are simple Python files with a  extension .py  and can have set of  classes,functions,  or variables defined and implemented. They can be imported and initialized once by  using the import statement. If partial functionality   needed, import the requisite classes and functions using from foo import bar.

Packages allow the  hierarchial structuring of the module namespace using the  dot notation. As, modules help to avoid clashes between global variable names, in  similar manner, packages helps to  avoid clashes between the module names.
Creating a package is easy since it makes the use of  system’s inherent file structure. So just stuff modules into a folder and there you  will have it, the folder name as the name of  package . Importing a module or its contents to  this package requires the package name as the  prefix to the module name joined by a dot.

10.Explain global, protected and private attributes in Python?

  • Global variables are  the public variables that are defined in  global scope. To use this  variables in the global scope inside a function, we  must use the global keyword.
  • Protected attributes are the attributes defined with an underscore pre-fixed to their identifier like  _sara. They can still be accessed and modified from outside  class they are defined in .A  responsible developer should refrain from doing so.
  • Private attributes are the attributes with double underscore prefixed to their identifier like  __ansh. They cannot be accessed or modified from  outside directly and  also will result in AttributeError if such an attempt  made.

If you are looking to get certified in Python? Check out the Scaler Topic’s course  with certification. 


Leave a Reply

Your email address will not be published. Required fields are marked *