Question 1 : What is PYTHONPATH?
Answer : It is an environment variable which is used when a module is imported. Whenever a module is imported, PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load.
Learn Python to Unleash a Modern Career
Question 2 : What is pep 8?
Answer : PEP stands for Python Enhancement Proposal. It is a set of rules that specify how to format Python code for maximum readability.
Question 3 : Is indentation required in python?
Answer : Indentation is necessary for Python. It specifies a block of code. All code within loops, classes, functions, etc is specified within an indented block. It is usually done using four space characters. If your code is not indented necessarily, it will not execute accurately and will throw errors as well.
Question 4 : What are local variables and global variables in Python?
Answer :
Global Variables:
Variables declared outside a function or in global space are called global variables. These variables can be accessed by any function in the program.
Local Variables:
Any variable declared inside a function is known as a local variable. This variable is present in the local space and not in the global space.
Question 5 : Is python a case sensitive language?
Answer : Yes. Python is a case sensitive programming language.
Question 6 : What is the output of print str if str = ‘Hello World!’?
Answer : It will print complete string. The Output would be Hello World!.
Question 7 : How will you convert a String to an object in python?
Answer : eval(str) − Evaluates a string and returns an object.
Question 8 : What is the purpose break statement in python?
Answer : The break statement in Python terminates the loop statement and transfers execution to the statement immediately following the loop.
Question 9 : Name the python Library used for Machine learning.
Answer : Scikit-learn python Library used for Machine learning
Question 10 : Which programming Language is an implementation of Python programming language designed to run on Java Platform?
Answer : Jython – (Jython is successor of Jpython.)
Learn Python to Unleash a Modern Career
Question 11 : What is dictionary in Python?
Answer : The built-in datatypes in Python is called a dictionary. It defines a one-to-one relationship between keys and values. Dictionaries contain a pair of keys and their corresponding values. Dictionaries are indexed by keys.
Let’s take an example:
The following example contains some keys. The country, Capital & PM. Their corresponding values are India, Delhi, and Modi respectively.
dict={‘Country’:’India’,’Capital’:’Delhi’,’PM’:’Modi’}
print dict[Country]
Question 12 : What is a Variable in Python?
Answer : When we say Name = ‘john’ in Python, the name is not storing the value ‘john’. But, ‘Name’ acts as a tag to refer to the object ‘john’. The object has types in Python but variables do not, all variables are just tags. All identifiers are variables in Python. Variables never store any data in Python.
Question 13 : How Python is interpreted?
Answer : 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.
Question 14 : What is namespace in Python?
Answer : 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.
Question 15 : What is negative index in Python?
Answer : Python sequences can be an index in positive and negative numbers. For positive index, 0 is the first index, 1 is the second index and so forth. For a negative index, (-1) is the last index and (-2) is the second last index and so forth.
Question 16 : Explain how to delete a file in Python?
Answer : By using a command os.remove (filename) or os.unlink(filename).
Question 17 : Explain what is Dogpile effect? How can you prevent this effect?
Answer : Dogpile effect is referred to the event when the cache expires, and websites are hit by the multiple requests made by the client at the same time. This effect can be prevented by using a semaphore lock. In this system when value expires, the first process acquires the lock and starts generating new value.
Question 18 : Explain split(), sub(), subn() methods of “re” module in Python.
Answer : To modify the strings, Python’s “re” module is providing 3 methods. They are:
- split() – uses a regex pattern to “split” a given string into a list.
- sub() – finds all substrings where the regex pattern matches and then replace them with a different string
- subn() – it is similar to sub() and also returns the new string along with the no. of replacements.
Question 19 : When Is The Python Decorator Used?
Answer : Python decorator is a relative change that you do in Python syntax to adjust the functions quickly.
Question 20 : What is map function in Python?
Answer : map function executes the function given as the first argument on all the elements of the iterable given as the second argument. If the function given takes in more than 1 arguments, then many iterables are given. #Follow the link to know more similar functions.
Learn Python to Unleash a Modern Career
Question 21 : Explain how Memcached should not be used in your Python project?
Answer :
- Memcached common misuse is to use it as a data store, and not as a cache
- Never use Memcached as the only source of the information you need to run your application. Data should always be available through another source as well
- Memcached is just a key or value store and cannot perform query over the data or iterate over the contents to extract information
- Memcached does not offer any form of security either in encryption or authentication
Question 22 : What is a Python module?
Answer : A module is a Python script that generally contains import statements, functions, classes and variable definitions, and Python runnable code and it “lives” file with a ‘.py’ extension. zip files and DLL files can also be modules. Inside the module, you can refer to the module name as a string that is stored in the global variable name.
Question 23 : Explain what is Flask & its benefits?
Answer : Flask is a web micro framework 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.
Question 24 : Is Python object-oriented? what is object-oriented programming?>Answer : Yes. Python is Object Oriented Programming language. OOP is the programming paradigm based on classes and instances of those classes called objects. The features of OOP are:
Encapsulation, Data Abstraction, Inheritance, Polymorphism.
Question 25 : Mention the use of the split function in Python?
Answer : 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 words present in the string.
Question 26 : Does Python supports interfaces like in Java? Discuss.
Answer : Python does not provide interfaces like in Java. Abstract Base Class (ABC) and its feature are provided by the Python’s “abc” module. Abstract Base Class is a mechanism for specifying what methods must be implemented by its implementation subclasses. The use of ABC’s provides a sort of “understanding” about methods and their expected behavior. This module was made available from Python 2.7 version onwards.
Question 27 : Mention five benefits of using Python?
Answer :
- 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 the built-in data types saves programming time and effort from declaring variables
Question 28 : How Does Python Handle Memory Management?
Answer :
- Python uses private heaps to maintain its memory. So the heap holds all the Python objects and the data structures. This area is only accessible to the Python interpreter; programmers can’t use it.
- And it’s the Python memory manager that handles the Private heap. It does the required allocation of the memory for Python objects.
- Python employs a built-in garbage collector, which salvages all the unused memory and offloads it to the heap space
Question 29 : Explain how can you access a module written in Python from C?
Answer : You can access a module written in Python from C by the following method,
Module = =PyImport_ImportModule(“<modulename>”);
Question 30 : How do you perform pattern matching in Python? Explain
Answer : Regular Expressions/REs/ regexes enable us to specify expressions that can match specific “parts” of a given string. For instance, we can define a regular expression to match a single character or a digit, a telephone number, or an email address, etc. The Python’s “re” module provides regular expression patterns and was introduce from later versions of Python 2.5. “re” module is providing methods for search text strings, or replacing text strings along with methods for splitting text strings based on the pattern defined.
Learn Python to Unleash a Modern Career
Question 31 : Explain how can you generate random numbers in Python?
Answer : To generate random numbers in Python, you need to import command as
import random
random.random()
This returns a random floating point number in the range [0,1)
Question 32 : Whenever Python exits, why isn’t all the memory de-allocated?
Answer :
- Whenever Python exits, especially those Python modules which are having circular references to other objects or the objects that are referenced from the global namespaces are not always de-allocated or freed.
- It is impossible to de-allocate those portions of memory that are reserved by the C library.
- On exit, because of having its own efficient cleanup mechanism, Python would try to de-allocate/destroy every other object.
Question 33 : What are the supported data types in Python?
Answer : Python has five standard data types −
- Numbers
- String
- List
- Tuple
- Dictionary
Question 34 : What is unit test in Python?
Answer : A unit testing framework in Python is known as unit test. It supports sharing of setups, automation testing, shutdown code for tests, aggregation of tests into collections etc.
Question 35 : What is Python? What are the benefits of using Python?
Answer : 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, built-in data structure and it is an open source.