• 04Apr,18

    Interview Questions-Python

    Python is a high-level programming language that is widely used for programming on a general purpose level. It features a dynamic type system and automatic memory management, with the ability to sustain multiple programming models, which include imperative, object-oriented, functional programming and procedural styles. Python was built by "Guido van Rossum" and made available for use in 1991.

    Python is quite popular and it has interpreters for several operating systems, giving room for its code to run on a broad diversity of systems. It comes with a big and complete standard library. The reference implementation of Python (CPython) is open source software and it is handled by the Python Software Foundation (a Non-Profit Organization).




    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 pythons are that it is simple and easy, portable, extensible, built-in data structure and it is an open source.

    2)Name some of the features of Python?

    Some of the features of Python 

    • It supports functional and structured programming methods as well as OOP.

    • It can be used as a scripting language or can be compiled to byte-code for building large applications.

    • It provides very high-level dynamic data types and supports dynamic type checking.

    • It supports automatic garbage collection.

    • It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.

    3)Is python a case-sensitive language?

    Yes! Python is a case sensitive programming language.

    4)What are the supported data types in Python?

    • Numbers

    • String

    • List

    • Tuple

    • Dictionary

    5)What is PEP 8?

    PEP 8 is a coding convention, a set of recommendation, about how to write your Python code more readable.


    6)What are Python's dictionaries?

    Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

    7)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.

    8)What is a Python decorator?

    A Python decorator is a specific change that is made in Python syntax to effortlessly modify functions.

    9)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.

    10)What is the difference between list and tuple?

    While the list is mutable, a tuple is not. One can hash tuple, for instance, as a key for dictionaries.

    11) What is the built-in type does Python provides?

    There are mutable and Immutable types of Pythons built in types Mutable built-in types

    • List
    • Sets
    • Dictionary

    Immutable built-in types

    • Strings
    • Tuples
    • Numbers

    12)What are Dict and List Comprehensions?

    These are syntax constructions that help to simplify the creation of a dictionary or list based on iterable that already exists.

    13) What are the tools that help to find bugs or perform the 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.


    14)What is Lambda in Python?

    Lambda is a single expression anonymous function that is mostly used as an inline function.


    15)What are iterators?

    Iterators are used for iterating a group of elements, containers like a list

    16) What is namespace in Python?

    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.


    17)What is pass in Python?

    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.


    18)What is slicing?

    Slicing is a mechanism that helps you in selecting a range of items from sequence types such as a tuple, string, list, etc.


    19)What is docstring?

    Docstring is another name for a Python documentation string. It is a way to document Python modules, classes, and functions.


    20)What is unittest?

    Unittest is the unit testing framework in Python. It aids in automation testing, sharing of setups, aggregation of tests into collections, shutdown code tests, etc.


    21)What are generators in Python?

    The way of implementing iterators are known as generators.  It is a normal function except that it yields expression in the function.


    22)What is the difference between Xrange and range?

    While Xrange returns xrange objects, range on the other hand, returns the list and makes use of the same memory irrespective of the range size.


    23)How do you delete a file in Python?

    You simply make use of the command os.remove (filename) or os.unlink (filename)


    24)What is the use of the split function in Python?

    The split function in Python helps in breaking a string into shorter strings by using the defined separator. It provides a list of the words contained in the string.


    25)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.


    26) What is a negative index in Python?

    Python sequences can be an index in positive and negative numbers.   For a 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.


    27)What is module and package in Python?

    In Python, a module is a way to structure program. Each Python program file is a module, which imports other modules like objects and attributes.

    The folder of Python program is a package of modules.  A package can have modules or subfolders.

    28)Explain how Python does Compile-time and Run-time code checking?

    Python performs some amount of compile-time checking, but most of the checks such as type, name, etc are postponed until code execution. Consequently, if the Python code references a user -defined function that does not exist, the code will compile successfully. In fact, the code will fail with an exception only when the code execution path references the function which does not exist.


    29)Whenever Python exists Why does all the memory is not deallocated/freed when Python exits?

    Whenever Python exists, 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/freed/uncollectable.
    It is impossible to deallocate 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 deallocate/destroy every object.


    30)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’s assumed to be local.

    Global variables: Those variables that are only referenced inside a function are implicitly global.

    31)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.

    32)Explain Python's zip() function?

    zip() function- it will take multiple lists say list1, list2, etc and transform them into a single list of tuples by taking the corresponding elements of the lists that are passed as parameters

    list1 = ['A','B','C'] and list2 = [10,20,30].zip(list1, list2) # results in a list of tuples say [('A',10),('B',20),('C',30)] 
    when the first list ends.


    whenever the given lists are of different lengths, zip stops generating tuples

    33) 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)

    34)Explain how can you generate random numbers in Python?

    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)
    35) Explain Python's pass by references Vs pass by value?

    In Python, by default, all the parameters (arguments) are passed “byreference” to the functions. Thus, if you change the value of the parameter within a function, the change is reflected in the calling function.We can even observe the pass “by value” kind of a nature of them.whenever we pass the arguments to functions that are of type say numbers, strings, tuples. This is because of the immutable

    36)Explain how can you access a module written in Python from C?
    You can access a module written in Python from C by following method,Module = =  PyImport_ImportModule(“”);

    37)How will you get all the values from the dictionary?

    Using dictionary.values() function, we can get all the values from the dictionary object

    print dict.values()   # Prints all the values

    38)How will you convert a string to an int in python?

    int(x [,base]) - Converts x to an integer. base specifies the base if x is a string.

    39)How will you convert a string to a long in python?

    long(x [,base] ) - Converts x to a long integer. base specifies the base if x is a string.

    40)What is the purpose of ** operator?

    ** Exponent − Performs exponential (power) calculation on operators. a**b = 10 to the power 20 if a = 10 and b = 20.

    41)What is the purpose of // operator?

    // Floor Division − The division of operands where the result is the quotient in which the digits after the decimal point are removed.

    42)What is the purpose of is operator?

    is − Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. x is y, here is results in 1 if id(x) equals id(y).

    43)What is the purpose of not in operator?

    not in − Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. x not in y, here not in results in a 1 if x is not a member of sequence y.

    44)What is the purpose break statement in python?

    break statement − Terminates the loop statement and transfers execution to the statement immediately following the loop.

    45)What is the purpose continue statement in python?

    continue statement − Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

    46)How will you capitalize the first letter of a string?

    capitalize() − Capitalizes first

    47)Mention five benefits of using Python?

    • 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 use of square brackets
    • Easy-to-learn for beginners
    • Having the built-in data types saves programming time and effort from declaring variable

    48)Explain what is Flask & its benefits?


    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.

    49)What is Web Scraping? How do you achieve it in Python?
    WebScrapping is a way of extracting the large amounts of information which is available on the websites and saving it onto the local machine or onto the database tables.In order to scrap the web: load the web page which is interesting to you.

    To load the web page, use “requests” module.parseHTML from the web page to find the interesting information.Python has few modules for scraping the web. They are urllib2, scrapy, pyquery,BeautifulSoap, etc.

    50)What is a Python module?

    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.A module can be imported by other modules in one of the two ways. They are

    1. import

    2. from module-name import

Copyrights © 2018 venolearn. All rights reserved.