Python Programming Interview Questions & Answers. Below is a collection of frequently asked Python interview questions with responses for both new and experienced. Python Programming Interview Questions & Answers. Python Programming Interview Questions.
Python Programming Interview Questions & Answers
Here is the list of Python Programming Interview Questions & Answers:
Q.No.1) What is Python? What are the benefits of using Python?
Ans: Python is a programming language with objects, modules, threads, exceptions and automatic memory management. The benefits of pythons are that it is simple and easy, portable, extensible, build-in data structure and it is open-source.
Q.No.2) What is PEP 8?
Ans: PEP 8 is a coding convention, a set of recommendations, about how to write your Python code more readable.
Q.No.3) What is pickling and unpickling?
Ans: The pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using the dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.
Q.No.4) How Python is interpreted?
Ans: Python language is an interpreted language. Python program runs directly from the source code. It converts the source code that is written by the programmer into an intermediate language, which is again translated into machine language that has to be executed.
Q.No.5) How memory is managed in Python?
Ans: Python memory is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have access to this private heap and the interpreter takes care of this Python private heap.
The allocation of Python heap space for Python objects is done by the Python memory manager. The core API gives access to some tools for the programmer to code.
Python also has an inbuilt garbage collector, which recycles all the unused memory and frees the memory and makes it available to the heap space.
Q.No.6) What are the tools that help to find bugs or perform static analysis?
Ans: PyChecker is a static analysis tool that detects the bugs in Python source code and warns about the style and complexity of the bug. Pylint is another tool that verifies whether the module meets the coding standard.
Q.No.7) What are Python decorators?
Ans: A Python decorator is a specific change that we make in Python syntax to alter functions easily.
Q.No.8) What is the difference between list and tuple?
Ans: The difference between list and tuple is that the list is mutable while tuple is not. Tuple can be hashed for e.g as a key for dictionaries.
Q.No.9) How are arguments passed by value or by reference?
Ans: Everything in Python is an object and all variables hold references to the objects. The reference values are according to the functions; as a result, you cannot change the value of the references. However, you can change the objects if it is mutable.
Q.No.10) What is Dict and List comprehensions are?
Ans: They are syntax constructions to ease the creation of a Dictionary or List based on existing iterable.
Q.No.11) What is the built-in type does python provides?
Ans: There are mutable and Immutable types of Pythons built-in types Mutable built-in types
- Immutable built-in types
Q.No.12) What is namespace in Python?
Ans: In Python, every name introduced has a place where it lives and can be hooked for. This is known as a namespace. It is like a box where a variable name is mapped to the object placed. Whenever the variable is searched out, this box will be searched, to get the corresponding object.
Q.No.13) What is lambda in Python?
Ans: It is a single expression anonymous function often used as an inline function.
Q.No.14) Why lambda forms in python do not have statements?
Ans: A lambda form in python does not have statements as it is used to make new function object and then return them at runtime.
Q.No.15) What is a pass in Python?
Ans: Pass means, no-operation Python statement, or in other words, it is a place holder in a compound statement, where there should be a blank left and nothing has to be written there.
Q.No.16) In Python what are iterators?
Ans: In Python, iterators are used to iterate a group of elements, containers like a list.
Q.No.17) What is a unit test in Python?
Ans: A unit testing framework in Python is known as a unit test. It supports sharing of setups, automation testing, shutdown code for tests, aggregation of tests into collections, etc.
Q.No.18) In Python what is slicing?
Ans: A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as slicing.
Q.No.19) What are the generators in Python?
Ans: The way of implementing iterators are known as generators. It is a normal function except that it yields expression in the function.
Q.No.20) What is docstring in Python?
Ans: A Python documentation string is known as docstring, it is a way of documenting Python functions, modules and classes.
Q.No.21) How can you copy an object in Python?
Ans: 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.
Q.No.22) What is negative index in Python?
Ans: Python sequences can be index in positive and negative numbers. For positive index, 0 is the first index, 1 is the second index and so forth. For negative index, (-1) is the last index and (-2) is the second last index and so forth.
Q.No.23) How you can convert a number to a string?
Ans: In order to convert a number into a string, use the inbuilt function str(). If you want an octal or hexadecimal representation, use the inbuilt function oct() or hex().
Q.No.24) What is the difference between Xrange and range?
Ans: Xrange returns the xrange object while range returns the list, and uses the same memory and no matter what the range size is.
Q.No.25) What is module and package in Python?
Ans: In Python, the module is the way to structure the program. Each Python program file is a module, which imports other modules like objects and attributes. The folder of the Python program is a package of modules. A package can have modules or subfolders.
Q.No.26) Mention what are the rules for local and global variables in Python?
Ans: Local variables: If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be local. Global variables: Those variables that are only referenced inside a function are implicitly global.
Q.No.27) How can you share global variables across modules?
Ans: To share global variables across modules within a single program, create a special module. Import the config module in all modules of your application. The module will be available as a global variable across modules.
Q.No.28) Explain how can you make a Python Script executable on Unix?
Ans: To make a Python Script executable on Unix, you need to do two things,
- Script file’s mode must be executable and
- the first line must begin with # ( #!/usr/local/bin/python)
Q.No.29) Explain how to delete a file in Python?
Ans: By using a command OS.remove (filename) or os.unlink(filename)
Q.No.30) Explain how can you generate random numbers in Python?
Ans: To generate random numbers in Python, you need to import command as:
- import random
- This returns a random floating-point number in the range [0,1)
Q.No.31) Explain how can you access a module written in Python from C?
Ans: You can access a module written in Python from C by following method,
Module = =PyImport_ImportModule(“<modulename>”);
Q.No.32) Mention the use of // operator in Python?
Ans: It is a Floor Divisionoperator, which is used for dividing two operands with the result as quotient showing only digits before the decimal point. For instance, 10//5 = 2 and 10.0//5.0 = 2.0.
Q.No.33) Mention five benefits of using Python?
Ans: Python comprises of a huge standard library for most Internet platforms like Email, HTML, etc.
Python does not require explicit memory management as the interpreter itself allocates the memory to new variables and free them automatically
- Provide easy readability due to the use of square brackets
- Easy-to-learn for beginners
- Having built-in data types saves programming time and effort from declaring variables
Q.No.34) Mention the use of the split function in Python?
Ans: The use of the split function in Python is that it breaks a string into shorter strings using the defined separator. It gives a list of all the words present in the string.
35) Explain what is Flask & its benefits?
Ans: Flask is a web microframework for Python based on “Werkzeug, Jinja 2 and good intentions” BSD licensed. Werkzeug and jingja are two of its dependencies.
Flask is part of the micro-framework. Which means it will have little to no dependencies on external libraries. It makes the framework light while there is a little dependency to update and fewer security bugs.
Q.No.36) Mention what is the difference between Django, Pyramid, and Flask?
Ans: Flask is a “microframework” primarily build for a small application with simpler requirements. In a flask, you don’t have to use external libraries. Flask is ready to use.
A pyramid is built for larger applications. It provides flexibility and lets the developer use the right tools for their project. The developer can choose the database, URL structure, templating style and more. The pyramid is heavily configurable.
Like Pyramid, Django can also be used for larger applications. It includes an ORM.
Q.No.37) Mention what is Flask-WTF and what are their features?
Ans: Flask-WTF offers simple integration with WTForms. Features include for Flask WTF are
- Integration with wtforms
- Secure form with csrf token
- Global csrf protection
- Internationalization integration
- Recaptcha supporting
- File upload that works with Flask Uploads
Q.No.38) Explain what is the common way for the Flask script to work?
Ans: The common way for the flask script to work is:
- Either it should be the import path for your application
- Or the path to a Python file
Q.No.39) Explain how you can access sessions in Flask?
Ans: A session basically allows you to remember information from one request to another. In a flask, it uses a signed cookie so the user can look at the session contents and modify them. The user can modify the session if only it has the secret key Flask.secret_key.
Q.No.40) You are having multiple Memcache servers running Python, in which one of the Memcache servers fails, and it has your data, will it ever try to get key data from that one failed server?
Ans: The data in the failed server won’t get removed, but there is a provision for auto-failure, which you can configure for multiple nodes. Fail-over can be triggered during any kind of socket or Memcached server level errors and not during normal client errors like adding an existing key, etc.