Python interview questions answers

Interview
Question & Answers
Tutorials

Python Interview Questions Answers Python programming language is popular day by day because there is a huge requirement of Python programmers in the market and in this post "Python Interview Questions Answers" I have tries to cover the concepts and usually asked interview questions to Python programmers by the interviewers. Python is one of the most popular programming languages for Big Data professionals who are engaged in dealing with huge volume of data. Python Interview Questions Answers have been designed such a way that you get basic and advance knowledge of Python programming language.

Python Interview Questions Answers List

1. What is Python?

Python is a high level  programming  open source.language which consists of following:

  • objects
  • modules
  • threads
  • exceptions

2. List the  data types in Python.

  • Mutable Data Type
    • List
    • Dictionary
    • Sets
  • Immutable Data Types
    • Numbers
    • String
    • Tuple

3. List key features of Python.

  • Simple & Easy to Learn: Python is a simple and minimalistic language and easy to learn.
  • Free Open Source : Python is free open source i.e cab be distributed freely.
  • High-level Language :  Like other languages C#, Java adn R python is also high level language.
  • Portable : Due to its open-source nature, Python programs can work on any of multiple platforms.
  • Interpreted :  Python converts the source code into an intermediate form called byte codes and then translates this into the native language of your specific computer and then runs it.
  • Object Oriented : Python supports all concepts of OOPs like abstraction, Inheritance, Polymorphism.
  • Extensive Libraries : The Python Standard Library can help you do various things involving regular expressions, documentation generation, unit testing, threading, databases, web browsers, CGI, ftp,  etc.
  • Case sensitive : Python makes difference between lower case and upper case declarations.

4. Explain pickling and unpickling.

The pickle module supports binary protocols for serializing and de-serializing a Python object structure. Converting a Python object  into byte stream(serialization) and storing in file is called “Pickling” and “unpickling” means restoring the byte stream(from binary file) into python object in original form is called de-serializing.

5.  list and tuples.

List

Tuples

List is mutable. Once it is created, values can be changed. Tuples are immutable . Once it is created, values can not be changed.
Lists are homogeneous sequences. Tuples are heterogeneous data structures
Lists are for variable length Tuples are for fixed length
lists show order Tuples show structure

6. Define Python decorators.

Using Python Decorators you can change or modify  callable objects like functions, methods, or classes. In other words you can  add functionality to an existing code.

7. Define dictionary in python.

Dictionary is predefined datatype in Python. Dictionaries contain pair of keys and their associated values. Dictionaries are indexed by keys. Dictionaries are created using  curly braces ({ }) and values can be assigned and accessed using square braces ([]).

dict = {}
dict['First'] = "This is First Value"
dict[2] = "This is Second Value"
mydict= {"ECode":"34567","EmpName": "John Pyhon", "Dept": "IT"}

8. Define namespace in Python.

In Python Namespaces  is like a dictionaries where key is mapped to value, every entity or  name resides in reserved place, that reserved space is called namespace. Whenever you want to access  an entity or name python searches that specific namespace having that entity.

9. Define lambda in Python.

It is a single expression anonymous function often used as inline function.

10. Explain iterators in Python.

To access each element from groups of element In Python, iterators are used.

11. what is slicing in python?

The slice() function returns a slice object. Slice() allows you take out the selected range of string, list etc.

Example:
a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = slice(3, 5)
print(a[x])
OUTPUT: ('d','e')

12. Define generators in Python.

To create iterators you can use Generators. You can create iterators using generators.  Python Generator works like a function that returns an object(iterator).

13. Define negative indexes in python.

A negative index accesses elements from the end of the list counting backwards. In simple words -1 will present last element in index and -2 second last and so on.

Example:
lst1 = ['Red', 'Black','white', 'Yellow', 'Green', 'Orange','Gray']
print(lst1[-1])// Output 'Gray'
print(lst1[-2])// Output 'Orange'
print(lst1[-1])// Output 'Green'

14. How can you copy an object in Python?

To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the general case. You cannot copy all objects but most of them.

import copy
newobj = copy.copy(oldobj) # shallow copy
newobj = copy.deepcopy(oldobj) # deep (recursive) copy

15. How to convert a number to a string?

We can convert numbers to strings through using the str() method.

16. Define xrange and range.

  • range() : The output of  range() function is a list of numbers created using range() function.
  • xrange() : The output of  xrange() function is a  generator object that can be used to display numbers only by looping.

17. How to delete a file in Python?

By using a command os.remove (filename) or os.unlink(filename).

18. How to generate random numbers in Python?

import random
random.random()
//This returns a random floating point number in the range [0,1)

19. How to get all the keys from the dictionary?

Using dictionary.keys() function.

20. What is ternary operators in python?

Ternary operators cab be use as replacement of multi-line conditions in single line. it returns true or false as output.

Example:
a, b = 10, 20
# Copy value of a in min if a < b else copy b
min = a if a < b else b
print(min)

21. Define help() and dir() function in Python.

  • help()  return the Python documentation of a particular object, method, attributes, etc
  • dir() shows a list of attributes for the object passed in as argument.

22. Define map function in Python.

Python map function executes for each item of iterator and return result in same order.  It is a function to which map passes each element of given iterable.

23. Define  rules for local and global variables in Python.

  • Global variable: Variable  defined outside function is Global.
  • Local Variable:  Variable is assigned new value inside the function then it is local.

24. How to share global variable in Python?

Create  config file and store the global variable. All modules can access these variables.

25. How to pass optional or keyword parameters from one function to another in Python?

We can arrange arguments using the * and ** specifiers in the function’s parameter list.

26. List different types of sequences in Python?

  • xrange objects
  • Strings
  • buffers
  • Unicode strings
  • lists
  • tuples

27. What is Lambda form in Python?

It creates a  small random anonymous throw away functions.

28. How to send email from a Python Script?

The smtplib module supports SMTP client session for sending mail.

29. Define split(), sub() and subn() methods in python.

  • Split():  regex pattern in Split() function splits  given string.
  • Sub(): It will find all the substring where this regex pattern will match and then replace the string.
  • Subn():  It is similar to a sub(), it will return the new string along with the other no. of replacements.

30. Define  append() and extend methods.

  • append() adds the element at the end.
  • extend() adds the elements of a different list at the end.

31. What is TKIner?

The Tklner is the library of Python. It is one of the toolkits for developing the GUI.

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

In some case where numbers of parameters not known passed to a function, like when we want to pass a list or a tuple of values, we use *args.

33. How would you randomize the contents of a list in-place?

Import the function shuffle() from the module random.

>>> from random import shuffle
>>> shuffle(mylist)
>>> mylist

34. Explain join() and split() in Python.

  • join()  joins characters from a string together by a character we specify.
  • split()  splits a string around the character we specify.

35. Explain what Flask is and its benefits?

Flask (web framework) Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. ... Applications that use the Flask framework include Pinterest, LinkedIn, and the community web page for Flask itself.(WIKIPEDIA).

36. What is monkey patching in Python?

 Run time dynamic modifications of a class or module is called monkey patch.

 

Conclusion

This post “Python Interview Questions Answers”  has been designed by experts of Python language. All Python Interview Questions Answers have with examples have been tested to give you accurate answers.  Python is one of the most popular programming languages for Big Data professionals who are engaged in dealing with huge volume of data.  Python Programming language is used for statistical computing and graphics. Python  is also an interpreted language, using command line interpreter users can access it.