Q) What
is Python?
Python is a high-level, interpreted, interactive, and
object-oriented scripting language. Python is designed to be highly
readable.
Q) What are the
key features of Python?
·
Python is an interpreted language, so it doesn’t need to be compiled
before execution, unlike languages such as C.
·
Python is dynamically typed, so there is no need to declare a variable
with the data type. Python Interpreter will identify the data type on the basis
of the value of the variable.
For example, in Python, the following code line will run without any
error:
a = 100
a = "Umesh"
·
Python follows an object-oriented programming paradigm with the
exception of having access specifiers. Other than access specifiers (public and
private keywords), Python has classes, inheritance, and all other usual OOPs
concepts.
·
Python is a cross-platform language, i.e., a Python program written on a
Windows system will also run on a Linux system with little or no modifications
at all.
·
Python is literally a general-purpose language, i.e., Python finds its
way in various domains such as web application development, automation, Data
Science, Machine Learning, and more.
Q) What is the purpose
of PYTHONSTARTUP, PYTHONCASEOK, and PYTHONHOME environment variables?
·
PYTHONSTARTUP: It contains the path of an
initialization file having Python source code. It is executed every time we
start the interpreter. It is named as .pythonrc.py in Unix, and it contains
commands that load utilities or modify PYTHONPATH.
·
PYTHONCASEOK: It is used in Windows to instruct
Python to find the first case-insensitive match in an import statement. We can
set this variable with any value to activate it.
·
PYTHONHOME: It is an alternative module search
path. It is usually embedded in PYTHONSTARTUP or PYTHONPATH directories to make
switching of module libraries easy.
Q) Which data
types are supported in Python?
Python has five standard data types:
·
Numbers
·
Strings
·
Lists
·
Tuples
·
Dictionaries
Q) What is the difference between lists and tuples?
|
Lists
|
Tuples
|
|
Lists are mutable, i.e., they can be edited.
|
Tuples are immutable (they are lists that cannot be edited).
|
|
Lists are usually slower than tuples.
|
Tuples are faster than lists.
|
|
Syntax:
list_1
= [10, ‘Umesh’, 20]
|
Syntax:
tup_1
= (10, ‘Umesh’ , 20)
|
Q) How is memory
managed in Python?
·
Memory in Python is managed by Python private heap space. All Python
objects and data structures are located in a private heap. This private heap is
taken care of by Python Interpreter itself, and a programmer doesn’t have
access to this private heap.
·
Python memory manager takes care of the allocation of Python private
heap space.
·
Memory for Python private heap space is made available by Python’s
in-built garbage collector, which recycles and frees up all the unused memory.
Q) Explain Inheritance
in Python with an example.
As Python follows an object-oriented programming paradigm, classes in
Python have the ability to inherit the properties of another class. This
process is known as inheritance. Inheritance provides the code reusability
feature. The class that is being inherited is called a superclass and
the class that inherits the superclass is called a derived or child
class. Following types of inheritance are supported in Python:
·
Single inheritance: When a class
inherits only one superclass
·
Multiple inheritance: When a class
inherits multiple superclasses
·
Multilevel inheritance: When a class
inherits a superclass and then another class inherits this derived class
forming a ‘parent, child, and grandchild’ class structure
·
Hierarchical inheritance: When one
superclass is inherited by multiple derived classes
Q) What is a dictionary in Python?
Python dictionary is one of the
supported data types in python. It is an unordered collection of elements. The
elements in dictionaries are stored as key–value pairs. Dictionaries are
indexed by keys.
For example, below we have a
dictionary named ‘dict’. It contains two keys, Country and Capital, along with
their corresponding values, India and New Delhi.
dict={'Country':'India','Capital':'New Delhi',}
Q) How are range and xrange different from one another?
Functions in Python, range()
and xrange() are used to iterate in a for loop for a fixed number of times.
Functionality-wise, both these functions are the same. The difference comes
when talking about Python version support for these functions and their return
values.
|
The
range() Method
|
The
xrange() Method
|
|
In Python 3, xrange() is not supported; instead, the range()
function is used to iterate in for loops.
|
The xrange() function is used in Python 2 to iterate in for
loops.
|
|
It returns a list.
|
It returns a generator object as it doesn’t really generate a
static list at the run time.
|
|
It takes more memory as it keeps the entire list of iterating
numbers in memory.
|
It takes less memory as it keeps only one number at a time in
memory.
|
Q) What is a Python module?
Modules are independent Python
scripts with the .py extension that can be reused in other Python codes or
scripts using the import statement. A module can consist of functions, classes,
and variables, or some runnable code. Modules not only help in keeping Python
codes organized but also in making codes less complex and more efficient. The
syntax to import modules in Python is as follows:
import module_name #include at the top-most part of program.
Q) What do file-related modules in Python do? Can you name some
file-related modules in Python?
Python comes with some
file-related modules that have functions to manipulate text files and binary
files in a file system. These modules can be used to create text or binary
files, update their content, copy, delete, and more.
Some file-related modules are
os, os.path, and shutil.os. The os.path module has functions to access the file
system, while the shutil.os module can be used to copy or delete files.
Q) Explain all file
processing modes supported in Python.
Python has various file processing modes.
·
For opening files, there are three modes:
o read-only mode (r)
o write-only mode (w)
o read–write mode
(rw)
·
For opening a text file using the above modes, we will have to append
‘t’ with them as follows:
o read-only mode (rt)
o write-only mode
(wt)
o read–write mode
(rwt)
·
Similarly, a binary file can be opened by appending ‘b’ with them as
follows:
o read-only mode (rb)
o write-only mode
(wb)
o read–write mode
(rwb)
·
To append the content in the files, we can use the append mode (a):
o For text files, the
mode would be ‘at’
o For binary files,
it would be ‘ab’
Q) Is indentation optional in Python?
Indentation in Python is
compulsory and is part of its syntax.
Q) How are Python arrays and Python lists different from each
other?
In Python, when we say
‘arrays’, we are usually referring to ‘lists’. It is because lists are
fundamental to Python just as arrays are fundamental to most of the low-level
languages.
However, there is indeed a
module named ‘array’ in Python, which is used or mentioned very rarely.
Following are some of the differences between Python arrays and Python lists.
|
Arrays
|
Lists
|
|
Arrays can only store homogeneous data (data of the same
type).
|
Lists can store heterogeneous and arbitrary data.
|
|
Since only one type of data can be stored, arrays use memory
for only one type of objects. Thus, mostly, arrays use lesser memory than
lists.
|
Lists can store data of multiple data types and thus require
more memory than arrays.
|
|
The length of an array is pre-fixed while creating it, so more
elements cannot be added.
|
Since the length of a list is not fixed, appending items to it
is possible.
|
Q) What is __init__ in Python?
Equivalent to constructors in
OOP terminology, __init__ is a reserved method in Python classes. The __init__
method is called automatically whenever a new object is initiated. This method
allocates memory to the new object as soon as it is created. This method can
also be used to initialize variables.
Q) What do you understand by Tkinter?
Tkinter is an in-built Python
module that is used to create GUI applications. It is Python’s standard toolkit
for GUI development. Tkinter comes with Python, so there is no installation
needed. We can start using it by importing it in our script.
Q) Is Python fully object oriented?
Python does follow an
object-oriented programming paradigm and has all the basic OOPs concepts such
as inheritance, polymorphism, and more, with the exception of access
specifiers. Python doesn’t support strong encapsulation (adding a private
keyword before data members). Although, it has a convention that can be used
for data hiding, i.e., prefixing a data member with two underscores.
Q) What is lambda function in
Python?
A lambda function is an
anonymous function (a function that does not have a name) in Python. To define
anonymous functions, we use the ‘lambda’ keyword instead of the ‘def’ keyword,
hence the name ‘lambda function’. Lambda functions can have any number of
arguments but only one statement.
Q)
What is
self-keyword in Python?
Self-keyword is used as the
first parameter of a function inside a class that represents the instance of
the class. The object or the instance of the class is automatically passed to
the method that it belongs to and is received in the ‘self-keyword.’ Users can
use another name for the first parameter of the function that catches the
object of the class, but it is recommended to use ‘self-keyword’ as it is more
of a Python convention.
Q) What is the
difference between append() and extend() methods?
Both append() and extend() methods are methods used to add elements at
the end of a list.
·
append(element): Adds the given element at the end
of the list that called this append() method
·
extend(another-list): Adds the elements
of another list at the end of the list that called this extend() method
Q) What are loop interruption
statements in Python?
There are two types of loop interruption statements in Python that let
users terminate a loop iteration prematurely, i.e., not letting the loop run
its full iterations.
Following are the two types of loop interruption statements:
·
Python break statement: This statement
immediately terminates the loop entirely, and the control flow of the program
is shifted directly to the outside of the loop.
·
Python continue statement: Continue
statement terminates the current loop iteration and moves the control flow of
the program to the next iteration of the loop, letting the user skip only the
current iteration.
Q) What is docstring in Python?
Python lets users include a
description (or quick notes) for their methods using documentation strings or
docstrings. Docstrings are different from regular comments in Python as, rather
than being completely ignored by the Python Interpreter like in the case of
comments, Python documentation strings can actually be accessed at the run time
using the dot operator when docstring is the first statement in a method or
function.
Q) Which one of the following is not the correct syntax for creating a set in Python?
1. set([[1,2],[3,4],[4,5]])
2. set([1,2,2,3,4,5])
3. {1,2,3,4}
4. set((1,2,3,4))
Answer: set([[1,2],[3,4],[4,5]])
Explanation: The argument given for the set must be an iterable.
Q) What is functional
programming? Does Python follow a functional programming style? If yes, list a
few methods to implement functionally oriented programming in Python.
Functional programming is a coding style where the main source of logic
in a program comes from functions.
Incorporating functional programming in our codes means writing pure
functions.
Pure functions are functions that cause little or no changes outside the
scope of the function. These changes are referred to as side effects. To reduce
side effects, pure functions are used, which makes the code easy-to-follow,
test, or debug.
Python does follow a functional programming style. Following are some
examples of functional programming in Python.
·
filter(): Filter lets us filter some values
based on a conditional logic.
>>> list(filter(lambda x:x>6,range(9))) [7, 8]
·
map(): Map applies a function to every
element in an iterable.
>>> list(map(lambda x:x**2,range(5))) [0, 1, 4, 9, 16, 25]
·
reduce(): Reduce repeatedly reduces a
sequence pair-wise until it reaches a single value.
>>> from functools import reduce >>> reduce(lambda
x,y:x-y,[1,2,3,4,5]) -13
Q) Do we need to declare variables with
data types in Python?
No. Python is a dynamically
typed language, I.E., Python Interpreter automatically identifies the data type
of a variable based on the type of value assigned to the variable.
Q) What is the output
of following programs:
print '{0:.2}'.format(1.0
/ 3)
Answer:
0.33
print '{0:-2%}'.format(1.0
/ 3)
Answer:
33.33%