Facebook Page Twitter Page LinkedIn Page
× Technical Interview


Several jobs require candidates to have a profound knowledge of Python. These Python Interview Questions have been designed specially to get you acquainted with the nature of questions that you may encounter during your interview for the subject of Python.

1. What is Python?

Python is a high level, interpreted and interactive programming language. It was first developed in 1991 by Guido Van Rossum and designed with the philosophy of code readability which enables programmers to express ideas and logic in fewer lines of code. It is used for web development (server-side), system scripting, software development, data handling and performing complex mathematical operations.


2. What are the features of Python language?

Some important features of Python are given below:

  • Interactive Language – Python supports command line shell which provides output for each statement.
  • Interpreted Language – Python is an interpreted language. It executes code line by line which enables easy debugging and learning.
  • Integrated Language – Python can be easily integrated with other languages like C, C++ and JAVA etc.
  • Open-Source Language – Python is an open source language which makes it freely usable and distributable, even for commercial use.
  • Modular Language – Python allows user to logically organize python code in a file which can be further imported in another python code.
  • Dynamic Language – Python is a dynamic language. It allows user to change functions and objects at run-time.
  • Object-Oriented Programming Language – Python supports object oriented programming.
  • Portable Language – Python can be interpreted on various operating systems including UNIX-based systems, Mac OS and various versions of Windows.
  • High-Level Language – Python is a high-level language and enables development of a program in user friendly way and generally independent of computer’s hardware architecture.
  • Extensible in C++ & C – Python can be used in C++ and C program which enables scripting capabilities for a programmer.
  • Extensive Library Support – Python standard library is very huge. Along with this, it also supports third party library as well which enables fast programming.

3. Explain Python Functions?

A function is a block of statements which executes only when it is called somewhere in the program. Function provides re-usability of same code for different inputs, hence saves time and resources. There are many in-built functions in Python and one of the common function is print(). A user can create their own function which is also termed as user-defined functions.

Create Function: In Python, a function is defined using def keyword along with function's name followed by parenthesis containing function's parameter(s), if it has any.

Call Function: After defining the function, it can be called anywhere in the program with it's name followed by parenthesis containing function's parameter(s), if it has any.

The syntax of creating and calling a function is given below:

#Defining function
def function_name(parameters):
    statements

#Calling function
function_name(parameters)

4. What is zip() function in Python?

The zip() function is used to return a zip object which contains mapped values from similar indexes of multiple iterators. The values in the zipped object are mapped as tuples.

zip(iterator1, iterator2, ...)

In the below example, three iterators called month and avg_temp are mapped using zip() function.

month = ['JAN', 'FEB', 'MAR']
avg_temp = [-5, 2, 5]

x = zip(month, avg_temp)
y = list(x)

#Accessing elements from zipped object
print(y[0])
print(y[1])
print(y[2])

The output of the above code will be:

('JAN', -5) 
('FEB', 2)
('MAR', 5)

5. What is swapcase() function in the Python?

The swapcase() function is used to convert all lowercase characters of the string into uppercase and all uppercase characters of the string into lowecase. Any symbol, space, special character or number in the string is ignored while applying this function. Only Alphabets are converted.

In the below example, swapcase() method is used to convert all lowercase characters of the string into uppercase and all uppercase characters of the string into lowercase.

MyString = "Hello John!"
print(MyString.swapcase())

The output of the above code will be:

hELLO jOHN!

6. How to remove whitespaces from a string in Python?

The strip() function is used to trim the string. This function has one optional parameter which is used to specify trim character(s). Default value of this parameter is whitespace.

In the below example, strip() function to used to trim the string called MyString.

MyString = "   Hello World!   "
print(MyString.strip())

MyString = ",*#Hello, World#@!"
print(MyString.strip(",*#@!"))

The output of the above code will be:

Hello World!

Hello, World

7. What is the use of continue statement?

The continue statement in Python let the program skip a block of codes for current iteration in a loop. Whenever continue statement condition is fulfilled, it brings the program to the start of loop.

When the continue statement is used in a nested loop (loop inside loop), it will skip innermost loop's code block, whenever condition is fulfilled.

In the below example, continue statement is used to skip the while loop if the value of variable j becomes 4.

j = 0
while (j < 6):
  j = j + 1
  if(j == 4):
    print('this iteration is skipped.')
    continue
  print(j)

The output of the above code will be:

1
2
3
this iteration is skipped.
5
6

8. What is the use of break statement?

The break statement in python is used to terminate the program out of the loop containing it whenever the condition is met.

In the below example, break statement is used to get out of the while loop if the value of variable j becomes 4.

j=1
while (j < 10):
  if (j == 4):
    print("Getting out of the loop.")
    break
  print(j)
  j = j + 1

The output of the above code will be:

1
2
3
Getting out of the loop.

9. How can a number be converted into string?

In Python, a number can be converted into a string using the built-in function - str(). The str() function takes in any python data type and converts it into a string.

i=10
j = str(i)
print(j)
print(type(j))

The output of the above code will be:

10
<class 'str'> 

10. How is a lambda function in Python?

In Python, lambda function is also termed as anonymous function and it is a function without a name. An anonymous function is not declared by def keyword, rather it is declared by using lambda keyword.

Lambda function is defined using lambda keyword along with parameter(s) name followed by a single statement. The syntax is given below:


#Defining lambda
lambda parameters: statement

In the below example, two parameters a and b are used to return the product of passed parameters.

x = lambda a, b : a * b
print(x(3, 4))

The output of the above code is:

12

11. What is dictionary in Python?

A Dictionary is a type of data container in Python which is used to store multiple data in one variable. It contains data in key-value pair and can contain elements of different data types. Elements in a dictionary are unordered and hence it is not possible to access dictionary's element using index number. Dictionary's elements are immutable and hence not changeable. Additionally, it does not allow multiple elements with same values (no duplicate elements.)

A dictionary can be created by separating it's elements by comma (,) and enclosing with curly bracket { }.

#Dictionary with multiple datatypes
Info = {
   "name": "John",
   "age": 25,
   "city": "London"
}
print(Info)

The output of the above code will be:

{'name': 'John', 'age': 25, 'city': 'London'}

12. What is Pass in Python?

The Python pass keyword is used as a placeholder for future code. Python does not allow to create conditional statements, loops, functions or classes with empty codes and raises exception if created with empty codes. Instead of empty codes, pass keyword can be used to avoid such errors.

In the below example, empty if statement is created, which raises exception upon execution.

MyString = "Python"
if MyString is "Python":

print("pass statement allows to create objects with empty codes.")

The output of the above code will be:

IndentationError: expected an indented block

When pass statement is used instead of empty if statement, no exception will be raised and program will execute next line of codes.

MyString = "Python"
if MyString is "Python":
  pass

print("pass statement allows to create objects with empty codes.")

The output of the above code will be:

pass statement allows to create objects with empty codes.

13. What is the Python decorator?

Decorators are powerful and useful tool in Python. It adds functionality to an existing code and modify the behaviour of the code while compiling it. for example - It allows a programmer to wrap a function (a.k.a wrapper function) around a given function (a.k.a wrapped function) in order to modify the behavior of the given function, without permanently modifying it. The syntax for using decorator is given below:

# defining decorator
def decorator_name(funct):
  def wrapper_function():
    statements
    funct
    statements
  return wrapped_function

Function vs Decorator - A function is a block of code that performs a specific task whereas a decorator is a function that modifies other functions.


14. What is join() function in Python and how to use it?

The join() is defined as a string method and it is used to return a string which contains all elements of an iterable separated by the specified character(s). For this method, all elements of the iterable must be string.

In the below example, join() method to used to join all elements of the given list, each separated by a single whitespace.

MySeparator = " "
MyList = ["John", "25", "London"]
print(MySeparator.join(MyList))

The output of the above code will be:

John 25 London

15. What are iterators in Python?

There are various iterable objects in Python like lists, tuples, sets, dictionaries, and strings, etc. These iterables can get iterator form which means that an iterator can be iterated upon and each elements of the iterator can be accessed using a loop statement. In Python, an iterator object must implement following methods:

  • __iter__(): to initialize an iterator.
  • __next__(): to perform operations for next element and return next element of the iterator.

In the below example, an iterator called MyIter is created from the iterable called MyList and after that next() method is used to access next element of the iterator. Please note that, a StopIteration exception is raised when the iterator do not have more elements to iterate upon.

MyList = ['MON', 'TUE', 'WED']
MyIter = iter(MyList)

print(next(MyIter))
print(next(MyIter))
print(next(MyIter))

The output of the above code will be:

MON
TUE
WED

16. What is tuple in Python?

A Tuple is a type of data container in Python which is used to store multiple data in one variable. It can contain elements of different data types. Elements in a tuple are ordered and can be accessed using it's index number. Unlike lists, tuples are immutable and hence elements of a tuple are not changeable.

It can be created by separating it's elements by comma (,) and enclosing with round brackets ( ).

#Tuple with single datatype
Numbers = (10, 20, 30) 
print(Numbers)

#Tuple with multiple datatypes
Info = ('John', 25, 'London') 
print(Info)

The output of the above code will be:

(10, 20, 30)

('John', 25, 'London')

17. What is a negative index in Python?

An element of a sequence can be accessed with it's index number. Python allows positive indexing and negative indexing. Positive indexing starts with 0 in forward direction and negative indexing starts with -1 in backward direction.


18. What is the output of below code?

MyList = [10, 20, 30, 40, 50]
print(MyList[-1])

Answer is 50. Python allows negative indexing which starts with -1 in backward direction. Hence, the output of above code will be last term of the list which is 50.


19. What is the output of below code?

MyList = [10, 100]
print(MyList*2)

Answer is [10, 100, 10, 100].


20. What is enumerate () function in Python?

The Python enumerate() function is used to return enumerated object of the specified iterable. An iterable object can be any data structure like list, tuple, set, string and dictionary, etc. It adds a counter as the key to the enumerated object.

In the below example, the enumerate() is used to create enumerated object using iterable called MyList.

MyList = [10, 20, 30, 40, 50]
x = enumerate(MyList)
print(list(x))

The output of the above code will be:

[(0, 10), (1, 20), (2, 30), (3, 40), (4, 50)]