1. What is Python?
Python is an interpreted, cooperative, object-oriented programming language. It embraces exceptions, modules, very high level dynamic data types,dynamic typing and classes. Python combines notable power with very strong syntax. It has interfaces to many system libraries and calls, as well as to various window systems, and is extensible in C++ or C. Python is also working as an extension language for applications that need a programmable interface. Lastly, Python is portable which means it runs on numerous Unix variants, OS/2, Mac, PCs under MS-DOS, Windows NT and Windows.
2. State some programming language features of Python?
Salient features of Python are:
–Simple & Easy: Python is simple language & easy to learn.
–Free/open source: it means everybody can use python without purchasing license.
–High level language: when coding in Python one need not worry about low-level details.
–Portable: Python codes are Machine & platform independent.
–Extensible: Python program supports usage of C/ C++ codes.
–Embedded Language: Python code can be embedded within C/C++ codes & can be used a scripting language.
–Standard Library: Python standard library contains pre-written tools for programming.
–Build-in Data Structure: contains lots of data structure like lists, numbers & dictionaries.
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 Python is 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 have an inbuilt garbage collector, which recycle all the unused memory and frees the memory and makes it available to the heap space.
6. What are the rules for local and global variables in Python?
If a variable is defined outside function then it is implicitly global. If variable is assigned new value inside the function means it is local. If we want to make it global we need to explicitly define it as global. Variable referenced inside the function are implicit global. Following code snippet will explain further the difference
# Filename: variable_localglobal.py
print ‘a:’, a
print ‘local a: ‘, a
a = 100
print ‘a outside fun1:’, a
print ‘b: ‘, b
b = 33
print ‘global b:’, b
print ‘b outside fun2′, b
$ python variable_localglobal.py
local a: 33
a outside fun1: 100
global b: 33
b outside fun2: 33
7. What is LIST comprehensions features of Python used for?
LIST comprehensions features were introduced in Python version 2.0, it creates a new list based on existing list.
It maps a list into another list by applying a function to each of the elements of the existing list.
List comprehensions creates lists without using map() , filter() or lambda form.
8. How is memory managed in python?
- Memory management in Python involves a private heap containing all Python objects and data structures. Interpreter takes care of Python heap and that the programmer has no access to it.
- The allocation of heap space for Python objects is done by Python memory manager. The core API of Python provides some tools for the programmer to code reliable and more robust program.
- Python also has a build-in garbage collector which recycles all the unused memory. When an object is no longer referenced by the program, the heap space it occupies can be freed. The garbage collector determines objects which are no longer referenced by the program frees the occupied memory and make it available to the heap space.
- The gc module defines functions to enable /disable garbage collector:
enable() -Enables automatic garbage collection.
gc.disable() – Disables automatic garbage collection.
9. How do you make a higher order function in Python?
A higher-order function accepts one or more functions as input and returns a new function. Sometimes it is required to use function as data to make high order function , we need to import functools module.
The functools.partial() function is used often for high order function.
10. Describe how to generate random numbers in Python?
The standard module random implements a random number generator.
There are also many other in this module, such as:
uniform(a, b) returns a floating point number in the range [a, b].
randint(a, b)returns a random integer number in the range [a, b].
random()returns a floating point number in the range [0, 1].
Following code snippet show usage of all the three functions of module random:
Note: output of this code will be different evertime it is executed.
i = random.randint(1,99)# i randomly initialized by integer between range 1 & 99
j= random.uniform(1,999)# j randomly initialized by float between range 1 & 999
k= random.random()# k randomly initialized by float between range 0 & 1
print(“i :” ,i)
print(“j :” ,j)
print(“k :” ,k)
(‘i :’, 64)
(‘j :’, 701.85008797642115)
(‘k :’, 0.18173593240301023)
(‘i :’, 83)
(‘j :’, 56.817584548210945)
(‘k :’, 0.9946957743038618)
11. What are the tools that help to find bugs or perform static analysis?
To detect the bugs in Python source code and warns about the style and complexity of the bug,PyChecker, a static analysis tool is used. Toverify whether the module meets the coding standard, Pylint is used.
12. What are Python decorators?
To make modifications to callable objects like functions, methods, or classes,Decorators are used. Decorators are a syntactic convenience that allows a Python source file to say what it is going to do with the result of a function or a class statement before rather than after the statement.
13. What is the difference between list and tuple?
14. How are arguments passed by value or by reference?
In Python everything is object and all variables hold references to those objects. As references values are according to the functions, change the value of the references is not possible. Although, you can change the objects if it is mutable.
15. What is Dict and List comprehensions are?
They are syntax constructions to simplify the creation of a Dictionary or List based on remainingiterable.
16. What are the built-in type does python provides?
Mutable built-in types are as follows:
Immutable built-in types are as follows:
17. What is namespace in Python?
A namespace is acharting from names to objects. Utmost namespaces are implemented as Python dictionaries. Examples of namespaces are: the set of built-in names, the global names in a component; and the local names in a function invocation. In a logic the group of attributes of an object also form a namespace.
18. What is lambda in Python?
To create small anonymous functions or functions without a name, the lambda operator or lambda function is used. These functions are called throw-away functions as they are just required where they have been generated. Lambda functions are mostly used in grouping with the functions reduce(),map() and filter().
19. Why lambda forms in python does not have statements?
A lambda form in python does not have statements because it is used to create new function object and then return them at runtime.
20. What is pass in Python?
When you do not want any command or code to execute but when a statement is required syntactically, pass is used. The pass statement is considered as a null operation; which means it will only execute and nothing happens during its execution.
21. In Python what are iterators?
To implement the iterator protocol, an iterator object is used. The iterator protocol contains of two methods. The __iter__() method must return the iterator object and the next() method returns the next element from a sequence.
22. What is unittest in Python?
Python’s unittest module, alsoknown as PyUnit, is created on the XUnit framework design. The similar pattern is reiterated in numerous other languages, which includes C, perl, Java, and Smalltalk. The framework applied by unittest supports test suites, fixtures and a test runner to empower automated testing of code.
23.In Python what is slicing?
To select a range of items from sequence types like list, tuple, strings etc., a mechanism known as slicing is used.
24. What are generators in Python?
Generators are a simple and dominantoption to create or to generate iterators. On the external they look like functions, but there is syntactical and a semantical difference between them. Instead of return statements you will find inside of the body of a generator only yield statements, i.e. one or more yield statements.
25. What is docstring in Python?
Python documentation strings (or docstrings) provide anappropriatemethod of relating documentation with Python modules,functions, methods,andclasses. An object’s docsting is well-defined by including a string constant as the first declaration in the object’s definition.
26. How can you copy an object in Python?
To copy an object in Python, trycopy.copy () or copy.deepcopy() methods for the overalluse. You cannot allow to copy all objects but still u can able to copy most of them.
27. How you can convert a number to a string?
str() an in built function is used to convert a number into a string. oct() function is used for a octal representation while hex () function is used for a hexadecimal representation
28. What is the difference between Xrange and range?
Xrange and range are the preciselyequivalent in terms of functionality. They both offer a technique to create a list of integers to use. However the only difference between these two is that range returns a Python list object and Xrange returns an Xrange object.
29. What is module and package in Python?
A module is just a python file that can be used by importing it using the ‘import’ or ‘from module import var,function’ statements.A package is basically a way to organize code. Packages let you import directories on your computer into your programs using the ‘import’ or ‘from import’
A Article from : https://intellipaat.com/blog/python-interview-questions