Top 50 Python Interview Questions and Answers for Fresher’s in 2021

Education

Written by:

Reading Time: 4 minutes

If you are a fresher and looking out for python interview questions, you have stepped in at the right place!

Here is a consolidated list of python interview questions which is short and crisp! 

Read On…

1.What is Python?

  • Python was developed by Guido van Rossum in 1991, is one of the smart programming languages which can automate the boring stuff! 
  • Python is an open-source build-in-data structure that has modules, threads, objects, and exceptions. 

2. What are the advantages of Python?

  • Presence of third-party modules
  • Extensive support libraries
  • Open source and community development
  • Easy to learn
  • User-friendly data structures
  • High-level language
  • Object-oriented language
  • Portable and Interactive
  • Portable across Operating systems

3. Give one reason to use yield in Python?

  • Yield can be used to get control over the access to read each iteration in some cases.

4. What are the key features of Python?

  • Interpreted
  • Dynamically-typed
  • Object-oriented
  • Concise and simple
  • Free
  • Has a large community

5.  What is pep 8?

  • Pep 8 (  Python Enhancement Proposal )  is a set of rules used to format a Python code to its maximum readability.

6. What is namespace in Python?

  • Name place is introduced to give a unique name to avoid name conflicts.

7.  What are the in-built Python modules commonly used?

  • Python is considered as .py file and these modules have a particular set of Python codes that may function in classes or variables
    The standard modules are:
  • OS
  • Random
  • Data Time
  • Sys
  • Math
  • JSON

8. What are Python decorators?

  • To alter the functions easily, a Python decorator is used.

9. List the difference between list and tuple?

  • The list is mutable and has a variable size.
  • The tuple is not mutable and has a fixed size.

10. What is the purpose of is and in operators?

  • is: True if the operands are identical.
  • in: True if the value is found in the sequence.

11. What is Dict and List comprehensions are?

  • They are syntax, to ease the creation of a Dictionary or List based on existing iterable.

12. What are built-in type does python provides?

  • There are mutable and Immutable types of Pythons built-in types are: 
  • List Sets
  • Dictionaries Immutable built-in types
  • Strings Tuples Numbers

13. Is Python case-sensitive?

  • Yes, python is a case-sensitive language.

14. What is lambda in Python?

  • It is an anonymous function often used as an In-line function.

15. Why lambda forms do not have statements?

  • Because it has only a single expression that returns by default.

16. What is a pass in Python?

  • Pass is a no-operation Python statement, when executed nothing happens but it will avoid getting an error when empty code is executed.

17. In Python what are iterators?

  • An iterator is an object which is used to iterate a group of elements.
  • Iterator protocol : __iter__() and __next__() 

18. What is a unit test in Python?

  • It is a software testing method that helps in automation testing, shutdown code for tests, aggregation of tests into collections, etc.

19. In Python what is slicing?

  • It is a process of selecting a range of items from sequence types like list, tuple, strings, etc. is known as slicing.

20. What are generators in Python?

  • It is a simple way of creating iterators. 

21. What is docstring in Python?

  • Docstring is a way of documenting Python functions, modules, and classes.

22. How to copy an object in Python?

To copy an object in Python :

  •  copy. copy () or copy.a deep copy() for the general case. 

23. What is a negative index in Python?

  • In Python negative index is  (-n) as 1st index -(n-1) as the second index
    &  (-1) is the last index, and (-2) is the second last index.

24. How to convert a number to a string?

To convert a number into a string 

  • Use inbuilt function str(). 
  • For octal or hexadecimal use inbuilt function oct() or hex().

25. What is the difference between Xrange and range?

  • Xrange returns the xrange object. 
  • Range returns the list.

26. What is module and package in Python?

  • The module is a simple Python file which imports other modules like objects and attributes.
  • The folder of the Python program is a simple directory having collections of modules.

27. Mention what are the rules for local and global variables in Python?

  • Local variables: If a variable is assigned a new value anywhere in the body of the function, it’s assumed to be local.
  • Global variables: Those variables that are inside a body of the function are implicitly global.

28. How to share global variables across modules?

  • To share global variables across modules within a single program, create a config module. 

29. How to append to a string in Python?

  • If the string is a list of characters, the join()  function can also be used.

30. How to convert a string to lowercase in Python?

use lower() function.

Example:

s=’MYSTRING’

prints.lower()

31. How to convert a string to uppercase in Python?

  • use upper() function.
  • Example:
  • s=’MYSTRING’
  • prints.lower()

32. How to check if string A is a substring of string B?

  • Use the “in” operator.

>>> ‘abc’ in ‘abcdefg’

True

33. How can files be deleted in Python?

  • Use the os. remove() function.

Example:

12import osos.remove(“xyz.txt”)

34. What are the built-in types of python?

Built-in types in Python are 

  • Integers
  • Floating-point
  • Complex numbers
  • Strings
  • Boolean
  • Built-in functions

35. What are Python libraries? Name a few of them.

  • Python libraries are a collection of Python packages. 
  • Libraries are Numpy, Pandas, Matplotlib, Scikit-learn, etc…

36. What is split used for?

  • The split() is used to separate a given string in Python.

37. How to import modules in python?

  • Modules can be imported using the import keyword.  

38.How are classes created in Python?

  • Class is created using the class keyword.

39.What is monkey patching in Python?

  • The term monkey patch refers to effective alterations of a class or module at run-time.

40. Does python support multiple inheritances?

  • Python does support multiple inheritances, unlike Java.

41. What is Polymorphism in Python?

  • Polymorphism is the ability to take multiple forms. 

42.  Define encapsulation in Python?

  • Binding the code and the data together is encapsulation. 

43. How to do data abstraction in Python?

  • Data Abstraction can be achieved by using interfaces and abstract classes that provide only the required details and hide the implementation from the world.

44.  How to add new value to the list?

  • Using Insert() function

45. Which one is doing concurrent execution?

  • Thread

 46.How to get the last value in a list?

  • a[-1]

47.How to add an existing module in python?

  • Using import

 48.Check the type of the variable in python?

  • typeof()

49.How to send no. of arguments in function?

  • Use “ *”  with parameter-name

50. Which collection is used to create a one-to-one relationship?

  • Dictionary

Next steps…

Your preparations have just begun…

If you wish to see yourself as a certified Python developer, here is an opportunity for you to enhance your Python skill with Great learning academy for free.

Good Luck!