1) What is Python? What are the benefits of using Python?
Python is a programming language with objects, modules, threads, exceptions and automatic
memory management. The benefits of python are that it is simple and easy, portable, extensible,
built-in data structure and it is an open source.
2) What is PEP 8?
PEP stands for Python Enhancement Proposal. It is a set of rules that specify how to format Python
code for maximum readability.
3) What is pickling and unpickling?
Pickle module accepts any Python object and converts it into a string representation and dumps it
into a file by using dump function, this process is called pickling. While the process of retrieving
original Python objects from the stored string representation is called unpickling.
4) How is Python interpreted?
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.
5) How memory is managed in Python?
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 an access to this private heap and
interpreter takes care of this Python private heap.
The allocation of Python heap space for Python objects is done by 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.
6) What are the tools that help to find bugs or perform static analysis?
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.
7) What are Python decorators?
A Python decorator is a specific change that we make in Python syntax to alter functions easily.
8) What is the difference between list and tuple?
The difference between list and tuple is that list is mutable while tuple is not. Tuple can be hashed
for e.g. as a key for dictionaries.
Lists are slower than tuples.
The list syntax uses square brackets whereas usage of parentheses defines a tuple.
9) How are arguments passed by value or by reference?
Everything in Python is an object and all variables hold references to the objects. The references
values are according to the functions; as a result you cannot change the value of the references.
However, you can change the objects if they are mutable.
10) What are Dict and List comprehensions?
They are syntax constructions to ease the creation of a dictionary or a list based on an existing
iterable.
11) What built-in types python provides?
Mutable built-in types
List
Sets
Dictionaries
Immutable built-in types
Strings
Tuples
Numbers
12) What is namespace in Python?
A namespace is a naming system used to make sure that names are unique to avoid naming
conflicts. Every name introduced has a place where it lives and can be hooked for. 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 corresponding object.
13) What is lambda in Python?
It is a single expression anonymous function often used as inline function.
14) Why lambda forms in python do not have statements?
A lambda form in python does not have statements as it is used to make a new function object and
then return it at runtime.
15) What is pass in Python?
In Python, pass keyword is used to execute nothing; it means, when we don't want to execute code,
the pass can be used to execute “empty”. It just makes the control to pass by without executing any
code. If we want to bypass any code, the pass statement can be used.
16) In Python what are iterators?
In Python, iterators are used to iterate a group of elements, or containers like list.
17) What is unittest in Python?
A unit testing framework in Python is known as unittest. It supports sharing of setups, automation
testing, shutdown code for tests, aggregation of tests into collections etc.
18) In Python, what is slicing?
A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as
slicing.
19) What are generators in Python?
There are two terms involved when we discuss generators.
Generator-Function: A generator-function is defined like a normal function, but whenever it
needs to generate a value, it does so with the yield keyword rather than return. If the body of
a def contains yield, the function automatically becomes a generator function.
Generator-Object: Generator functions return a generator object. Generator objects are used
either by calling the next method on the generator object or using the generator object in a
“for in” loop.
20) What is docstring in Python?
A Python documentation string is known as docstring, it is a way of documenting Python functions,
modules and classes.
21) 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.
22) What is negative index in Python?
Python sequences can be indexed 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.
23) How can you convert a number to a string?
In order to convert a number into a string, use the inbuilt function str(). If you want a octal or
hexadecimal representation, use the inbuilt function oct() or hex().
24) What is the difference between Xrange and range?
Xrange returns the xrange object while range returns the list, and uses the same memory no matter
what the range size is.
25) What is module and package in Python?
In Python, module is the way to structure a program. Each Python program file is a module, which
imports other modules like objects and attributes.
A set of modules make up a package.
26) Mention what are the rules for local and global variables in Python?
Local variables: If a variable is assigned a new value anywhere within the function's body, it is
assumed to be local.
Global variables: Those variables that are only referenced inside a function are implicitly global.
27) How can you share global variables across modules?
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.
28) Explain how can you make a Python Script executable on Unix?
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)
29) Explain how to delete a file in Python?
By using a method “os.remove (filename)” or “os.unlink(filename)” of the os module.
30) Explain how can you generate random numbers in Python?
import random
random.random()
This returns a random floating point number in the range [0, 1).
31) Explain how you can access a module written in Python from C?
Module = = PyImport_ImportModule("<modulename>");
32) Mention the use of the // operator in Python?
It is a Floor Division operator, 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.
33) Mention five benefits of using Python?
a. Python comprises of a huge standard library for most Internet platforms like Email, HTML, etc.
b. Python does not require explicit memory management as the interpreter itself allocates the
memory to new variables and frees them automatically.
c. Provides easy readability due to the use of square brackets.
d. Easy-to-learn for beginners.
e. Having built-in data types saves programming time and effort from declaring variables.
34) Mention the use of the split function in Python?
The split function in Python breaks a string into shorter strings using the defined separator. It gives a
list of all words present in the string.
35) What are the key features of Python?
Python is an interpreted language. That means that, unlike languages like C and its variants,
Python does not need to be compiled before it is run. Other interpreted languages include PHP
and Ruby.
Python is dynamically typed, this means that you don’t need to state the types of variables
when you declare them.
Python is well suited to object oriented programming in that it allows the definition of classes
along with composition and inheritance. Python does not have access specifiers (like C++’s
public, private).
In Python, functions are first-class objects. This means that they can be assigned to variables,
returned from other functions and passed into functions. Classes are also first class objects.
Writing Python code is quick but running it is often slower than compiled languages.
Fortunately , Python allows the inclusion of C based extensions so bottlenecks can be
optimized away and often are. The numpy package is a good example of this, it’s really quite
quick because a lot of the number crunching it does isn’t actually done by Python.
Python finds use in many spheres – web applications, automation, scientific modeling, big data
applications and many more. It’s also often used as “glue” code to get other languages and
components to play nice.
36) What type of language is python? Programming or scripting?
Python is capable of scripting, but in general sense, it is considered as a general-purpose
programming language.
37) How is Python an interpreted language?
An interpreted language is any programming language which is not in machine level code before
runtime. Therefore, Python is an interpreted language.
38) What is PYTHONPATH?
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.
39) What are local variables and global variables in Python?
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.
40) Is python case sensitive?
Yes. Python is a case sensitive language.
41) What is type conversion in Python?
Type conversion refers to the conversion of one data type into another.
int() – converts any data type into integer type
float() – converts any data type into float type
ord() – converts characters into integer
hex() – converts integers to hexadecimal
oct() – converts integer to octal
tuple() – This function is used to convert to a tuple.
set() – This function returns the type after converting to set.
list() – This function is used to convert any data type to a list type.
dict() – This function is used to convert a tuple of order (key,value) into a dictionary.
str() – Used to convert integer into a string.
complex(real,imag) – This functionconverts real numbers to complex(real,imag) number.
42) Is indentation required in python?
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.
43) What is the difference between Python Arrays and lists?
Arrays and lists, in Python, have the same way of storing data. But, arrays can hold only elements of
a single data type whereas lists can hold any data type.
44) What are functions in Python?
A function is a block of code which is executed only when it is called. To define a Python function,
the def keyword is used.
45) What is __init__?
__init__ is a method or constructor in Python. This method is automatically called to allocate
memory when a new object/ instance of a class is created. All classes have the __init__ method.