Interview Questions and Answers - Python

Welcome to Python Interview Questions area. In this section we will discuss about different types of questions that can be used in Python interviews in general, in order for the employer to test your skills in Python. If you have any questions about Python that are not covered here or elsewhere on our website, please email .

Python Interview Questions and Answers

What is Python?

Python is a popular and powerful modern computer programming language. Python was developed by Guido van Rossum in 1991, and it is a free software. It is an interpreted, object-oriented , high-level programming language with dynamic semantics. It can also be extended to make system calls to almost all operating systems and to run code written in C or C++. Due to its ubiquity and ability to run on nearly every system architecture, Python is a universal language found in a variety of different applications. Unlike many similar languages, it's core language is very small and easy to master, while allowing the addition of modules to perform a virtually limitless variety of tasks .

The number of features in the Python itself is modest, requiring relatively little investment of time or effort to produce your earlier programs. The Python syntax is designed to be readable and straightforward . This simplicity makes Python an ideal teaching language , and it lets newcomers pick it up very quickly. For example, take a look at the code you'd use to print the text "Hello World" on your screen using the programming language Java:

  1. That's a lot of code for such a simple function in Java.
  2. Now take a look at the same exercise written in Python code:

What type of language is python? Programming or scripting?

Today, there is no meaningful distinction between a Scripting-language and a Programming-language . It is antiquated terminology. There's really no difference between the two, and when you think about it, there never really was. What used to be scripting languages like Perl and Python are perfectly valid programming languages. Typically, they were interpreted. So, forget about scripting versus non-scripting. It's nonsensical.

What are the key features of Python?

  1. Easy to code
  2. Easy to read
  3. Expressive
  4. Free and Open-Source
  5. High- Level Language
  6. Portable
  7. Interpreted Language
  8. Object-Oriented Language
  9. Extensible
  10. Embeddable
  11. Large Standard Library
  12. GUI Programming Support
  13. Dynamically Typed

And many more...

What is Python good for?

Python is an interpreted language and is easy to learn because the commands used can be easily understood by anyone who has a knowledge of English. It can be used for most any task as the libraries are expanding allowing one to do more and more things. These features of python have made it a perfect language for learning something else and not specifically programming. Meanwhile, we'll say we can use it for:

  1. Web and Internet Development
  2. Desktop GUI
  3. Scientific and Numeric Applications
  4. Data Science
  5. Software Development Applications
  6. Applications in Education
  7. Applications in Business
  8. Database Access
  9. Network Programming
  10. Games, 3D Graphics

Is Python platform independent?

Yes, Python is platform independent . Like we can Run the same code in different platforms without any changes. The Python bytecode itself is not platform-dependent. There are specific modules and functions that are only available on certain platforms, therefore Python source code can be made platform-dependent if it uses these. The documentation specifies if a name is only available on a restricted subset of platforms, so avoiding these will go far to make it platform-independent .

Is Python case-sensitive?

Yes. Like most of the widely used programming languages like Java, C, C++, etc, Python is also a case sensitive language. This means, "Hello" and "hello" are not the same. In other words, it cares about case- lowercase or uppercase .

What is Scope in Python?

A variable is only available from inside the region it is created is called scope . Think of it as the area of code where variables can be used. Every object in Python functions within a scope. A few examples of scope created during code execution in Python are as follows:

  1. Local Scope - A variable created inside a function belongs to the local scope of that function, and can only be used inside that function.

  2. Global Scope - A variable created in the main body of the Python code is a global variable and belongs to the global scope.

  3. Module Scope - It refers to the global objects of the current module accessible in the program.

How would you declare a comment in Python?

Comments are an integral part of any program. There are two types of commenting features available in Python: These are single-line comments and multi-line comments .

  1. Single-line comments are created by beginning a line with the hash (#) character, and they are automatically terminated by the end of line.


Python ignores everything after the hash mark (#) and up to the end of the line. You can insert them anywhere in your code, even inline with other code:

  1. Multi-line comments are created by triple double quote (""") or triple single quote (''') . It is used at the beginning and end of the block to comment on the entire block.


top Python Interview questions

What are the built-in type does python provides?

The most fundamental type that python provides as a built-in is Object , anything that you care to name is a subclass of an object even an integer digit.

The principal built-in types are:

  1. numerics
  2. sequences
  3. mappings
  4. classes
  5. instances
  6. exceptions

There are mutable and Immutable types of Pythons built in types.

Mutable built-in types

The methods that add, subtract, or rearrange their members in place, and don't return a specific item, never return the collection instance itself but None.

  1. List
  2. Sets
  3. Dictionaries

Immutable built-in types

  1. Strings
  2. Tuples
  3. Numbers

What are the differences between Python 2.x and Python 3.x?

Python 2 was created in the year 2000 and Python 3 is the modern version, it was created in 2008. Python 3 is easier for newcomers to learn, and several aspects of the core language are more consistent than those in Python 2 . Also, Python 3 eliminates many of the quirks that can easily trip up beginning programmers learning Python.

  1. The most visible difference between Python2 and Python3 is in print statement (function). In Python2 print is like a command but in Python3 print() is a function.

  2. In Python 2, a string is by default ASCII. But in Python3 string is by default Unicode.

  3. In Python2 the integer divide works in C/C++ style but Python3 will return the expected result. For example, In Python 2 5/2 will return 2 but in Python 3 it will return 2.5.

  4. In Python 2 the Exceptions are enclosed in parenthesis while in python 3 Exceptions are enclosed in notations.

  5. In Python 2, the xrange() is used for iterations while in python 3 the new Range() function introduced to perform iterations.

  6. In python 3 there is no raw_input() method. The raw_input() method is replaced by input() in python 3. If you still want to use the input() method like in python 2 then you can use eval() method.

What are python modules? How to get a list of all available modules in Python?

A Python Module is file containing a set of functions, classes, variables, constants or any other Python objects you want to include in your application. Contents of this file can be made available to any other program. There are several built-in modules in Python, which you can import whenever you like. To display a list of all available modules, use the following command in the Python console:

How would you define a block of code in Python?

A block of code is composed of several statements that are intended to execute when certain condition is met. In Python, a block of code is implemented using indentation . For any kind of statements, we possibly need to define a block of code under them. However, Python does not support curly braces ({}). This means we must end such statements with colons (:) and then indent the blocks under those with the same amount.

Be consistent when you program in Python : choose either to indent using tabs or spaces and follow your chosen convention throughout your program.

What are the different types of operators in Python?

Operators are used to perform operations on one or more operands . An operand is a variable or a value on which we perform the operation. Python supports the following types of operators.

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Assignment Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity Operator

What is the purpose of PYTHONSTARTUP environment variable?

PYTHONSTARTUP is an environment variable that you will define specifying the location of the path to a python file . This python script will be run by python before starting the python interactive mode (interpreter).

What is lambda in Python?

A lambda function allows you to write quick throw away functions without naming them. It can take any number of arguments, but can only have one expression . Also, it provides a nice way to write closures . Also,


What is self in Python?

Python self keyword represents the instance of the class. Using self, you can access the attributes and methods of the class in python. The reason you need to use "self" is because Python does not use the @ syntax to refer to instance attributes. Its use is similar to the use of "this" keyword in Java, i.e. to give a reference to the current object.

What is slicing?

Slicing in Python is a feature that enables accessing parts of sequences like strings, tuples, and lists. You can specify where to start the slicing, and where to end . You can also specify the step , which allows you to e.g. slice only every other item. Keep in mind that start and end are indices of the list. The value at the end index is not included in the sublist.

What is slicing in python

What are Python decorators?

A decorator takes in a function, adds some functionality and returns it. It allow you to extend and modify the behaviour of a callable (functions, methods, and classes) without permanently modifying the callable itself.

What are Python decorators?

What are negative indices?

Negative indices mean that you count from the right instead of the left . So, list[-1] refers to the last element, list[-2] is the second-last, and so on.

What are negative indices?

You can also use negative indices in slices, and as with positive indexes , when you do a slice operation, the last element is not included in the slice.


What is the purpose of ** operator?

Python ** operator does exponentiation. x ** y is x raised to the y power . The same ** symbol is also used in function argument and calling notations , with a different meaning (passing and receiving arbitrary keyword arguments).

What is the purpose of // operator?

Python // is a division operation that returns an integer by discarding the remainder.

This is the standard form of division using the / in most programming languages (like C, C++). However, Python 3.x changed the behaviour of / to perform floating-point division even if the arguments are integers. The // operator was introduced in Python 2.6 and Python 3 to provide an integer-division operator that would behave consistently between Python 2.x and Python 3.x. This means:

Python operators

You can also get the new behaviour in Python 2 by putting from __future__ import division at the top of your files.

How will you convert an object to a string in python?

Python str() is meant to produce a string representation of the object's data .

Also, repr() is a similar method, which returns a printable representation of the given object.

How will you check in a string that all characters are digits?

Python isdigit() method check if all the characters in the text are digits.

How will you convert a string to an int in python?

int() is the Python standard built-in function to convert a string into an integer value. You call it with a string containing a number as the argument, and it returns the number converted to an integer:

int(a,base) : This function converts any data type to integer. Base specifies the base in which string is if data type is string.

What are membership operators?

There are two membership operators in Python: in operator and not in operator

  1. in operator - Evaluates to true if it finds a variable in the specified sequence and false otherwise.

  2. not in operator- Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.

What is zip() function in Python?

Python zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.

Python zip() function

How do you find the type and identification number of an object in Python?

Every object has an identity , a type and a value. An object's identity never changes once it has been created; you may think of it as the object's address in memory . An object's type is also unchangeable . It determines the operations that an object supports and also defines the possible values for objects of that type.

  1. type() gives the type of the object that variable is pointing to.

  2. id()give the unique identification number of the object that variable is pointing to.

What is the output of print str[0] if str = 'Python Interview Questions'?

Output would be the first character of the string, "P" .

What is the output of print(txt * 2) if txt = 'Python Tutorial!'?

It will print the string 2 times.

Output would be Python Tutorial!Python Tutorial! .

Difference between Deep copy and Shallow copy?

  1. In the deep copy, a new compound object is created and then, recursively, inserts copies into it of the objects found in the original.

  2. In shallow copy, a new compound object is created and then inserts references into it to the objects found in the original.

How to determine the type of an object?

To get the actual type of an object , you can use the built-in type() function . Passing an object as the parameter will return the type object of that object.

type object of that object in python

Difference between del[], remove() and pop() on list?

  1. remove() : removes the first matching value, not a specific index.
  2. del[] : removes the item at a specific index.
  3. pop() : removes the item at a specific index and returns it.


  1. remove() is the only one which searches the object


  1. del[] allows the removal of a range of indexes because of list slicing.


  1. pop() is the only one which returns the value.

If you know the index of element in the list use pop() del[]and .
If you know the element in the list use remove().

In the following assignment operation, what is the data type of 'x' that Python output as?

Unlike other languages, Python output 'x' as a tuple . When you print 'x', the output is (10,20,30). This type of assignment is called "Tuple Packing ".

Tuple packing is nothing but the creation of a tuple, whereas tuple unpacking means to extract tuple values and store them in individual variables . This means that, if you write variables on the left side separated by commas "," elements of a tuple on the right side will be assigned to each variable .

Python infosys interview questions and answers

How to check whether the two variables are pointing to the same object in Python?

You can use Python "is operator" to check if two objects have the same identity. This means that x is y returns True if x and y are the same object .

Python is operator

Write a generator expression to get the numbers that are divisible by 3?

How many kinds of sequences are supported by Python? What are they?

In Python, sequence is the generic term for an ordered set . The order in which we put them in is the order in which we get an item out from them. Following three are the basic sequence type classes in python.

  1. list
  2. tuple
  3. range

There are some additional sequence type objects, these are binary data and text string .

See more Python Interview Questions and answers in the following sections..... (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About