Top 40 Python Interview Questions for 2021

Table of Contents

1. What is Python?

Python is a high-level, general-purpose, object-oriented programming language that can run equally on all platforms like Windows, Macintosh, Linux, etc. It is easy to learn and provides high readability for content browsing.

2. What are the features of Python?

  • Object-oriented
  • High readability
  • High-level language
  • open-source
  • Portable
  • Cross-platform
  • Integrated
  • Interpreted
  • Easy to learn

3. What are the applications of Python?

Below are the applications that can be developed using Python:

  • Web development
  • Gaming
  • Audio or video-based applications
  • Enterprise
  • Image application
  • GUI based desktop applications

4. What are the types of tokens in Python?

  • Keywords
  • Operators
  • Literals
  • Identifiers

5. Write a simple program to print “PYTHON LANGUAGE”

text = "PYTHON LANGUAGE"
print text

6. What are the types of strings supported in Python?

  • Single line string
  • Multiline string

7. What are the different types of Python operators?

  • Arithmetic
  • Logical
  • Assignment
  • Bitwise
  • Relational
  • Identity
  • Membership

8. What are the types of comments in Python?

  • Single line comment
  • Multiple line comment

9. Who is the developer of Python?

Guido van Rossum is the developer of Python.

10. What is a dictionary in Python?

A dictionary is a collection of key-value pairs. We can retrieve the values using the square bracket.

11. How to create a dictionary in Python?

>>> dictionary={'name':'Akash','rollno':'111'}

12. What are tuples?

A tuple is a collection of data that cannot be edited. The values are separated by a comma and enclosed within a square bracket.

13. How to assign a single value to multiple variables in Python?

x=y=z=100
print x
print y
print z

14. What are the different types of literals in Python?

  • String
  • Numeric
  • Boolean
  • Special
  • Literal collections

15. What are the different types of Numeric literals?

  • int
  • long
  • float
  • complex

16. What is the command to delete a file in Python?

os.remove(filename)

17. What is the capitalize() function?

The capitalize() function capitalizes the first character in the string.

18. How can you copy an object in Python?

We can copy an object using the copy() method or deepcopy() method.

19. What is docstring in Python?

The docstring stands for documentation string which is the way to document function, modules, and classes.

20. What are the different types of membership operator?

  • in operator
  • not in operator

21. What is PEP 8?

PEP stands for Python Enhancement Proposal. It is a set of rules that denotes the code format for Python for better readability which means it provides stict guidelines that we must follow while writing python code.

22.How is memory managed in Python?

Python manages memory using the memory manager that stores objects in a private heap space that is inaccessible to the developer. It is a dedicated memory for all Python objects . We can use the core Python API functions to work on these private memory spaces. It also has an in-built garbage collector that reuses the unused heap memory space.

23. What is a namespace in Python?

A namespace is a mechanism that avoids naming conflict in a code and ensures that we use only unique names. There are different types of namespaces like local namespace, global namespace and built-in namespace.

24. What is PYTHONPATH?

PYTHONPATH is an environment variable that it uses to set additional directories to load specific modules and packages. This is useful when we do not need it in default global location. The interpreter uses the PYTHONPATH to load the required modules.

25. What is _init_ ?

_init_ is a special type of constructor or method that is called automatically whenever we create a new object or instance. This is mainly to differentiate between class and local variables.

26. What is the difference between list and tuples?

The main difference between list and tuples is list elements can be changed or updated and are enclosed within paranthesis () while tuples elements cannot be changed and are enclosed within the square brackets[].

27. What do you mean by continue, break and pass in python?

Continue: It terminates the current iteration by skipping the remaining code and continues with the next iteration.

break: It terminates the loop and passes the control to the statement after the loop

pass: It is just an empty space that is similar to semicolon ;

28. What is self in python?

In python, self is an object of a class that we use as the first parameter in the init method and denotes the new object. In methods, it refers to the object that calls the method.

29. What is the difference between xrange and range?

Both range and xrange are similar in functionality where it generates integer numbers. The main difference is that the range returns Python list while the range returns an xrange object. Also, xrange generates a dynamic list unlike range which generates a static list. However, xrange support has been deprecated from Python3.x

30. What are generators in python?

Generators are functions that returns a set of items through which we can iterate.

31. What is lambda function in python?

Lambda is an anonymous function that accepts multiple arguments or parameters but can contain only 1 statement. It does not allow block of statements and does not return any value.

32. How to remove whitespace from a string in python?

We can use the whitespace and trailing space using the built-in strip() method.

33. What is a python decorator?

Python decorator is a function that adds extra functionality or modifies the behavior of the existing function without affecting or changing the function structure.

34. What is pickling and unpickling in Python?

The process of converting the Python object to string representation is pickling while the process of retrieving the python object from the string is unpickling.

35. What is the use of help() and dir() functions?

The help() function displays the help content related to keywords, modules, attributes and documentation string.

The dir() function displays the defined symbols.

36. What is the output of val[-3] from the list : val=[4,2,6,7,9,5]?

Output of val[-3] returns 7 which means the 3rd element from the right

37. What is the use of [::-1]?

It reverses the order of elements in an array without affecting the original array.

38. Does Python support multiple inheritance?

Yes Python supports multiple inheritance

39. What is the difference between .py and .pyc files?

The source code file represents .py and the byte code file represents the .pyc file. After compiling the source code, it generates the byte code file (.pyc file).

40. What do you mean by *args and **kwargs?

*args denotes the number of arguments or variables that we can pass to a function definition.

**kwargs represents the number of keywords that we can pass to a function definition.

Translate ยป