Header Ads

  • Breaking Now

    Short questions and answers on Python - I

    In 1991, Guido van Rossum released first edition of Python programming language which is a high level programming language that got popular gradually for many mission critical projects ever since its inception. Python has a dynamic type system and automatic memory management. It is supported within different operating systems through its interpreters. It supports multiple programming paradigms, including object-oriented, imperative, functional and procedural.

    In this post and more such posts(shall keep list of questions to 10 per post so that readability is eased out), I tried compiling questions which have short answers attached to them in world of Python programming language. I hope Python enthusiasts shall find these interesting and in case you have any feedback or more questions to post, please let me know in comments section.

    Q1. What are the key features of Python and what are the differences between Python and Java?
    A: Key features of Python are listed as given below:
    a.  It supports multiple programming paradigms, including object-oriented, aspect-oriented, imperative, functional and procedural.
    b. Uses dynamic typing and a combination of reference counting and a cycle-detecting garbage collector for memory management.
    c. Python is designed to be highly extensible
    d. A simpler, less-cluttered grammar. There is only one way and its philosophy says "there should be one—and preferably only one—obvious way to do it"
    e. Interpreted language which has different OS dependent interpreters.
    f. Open source technology
    g. Large standard library
    h. GUI can be developed using Python

    Differences with Java:
    a. Python is dynamically typed while Java is statically typed. In Java, it is imperative to declare objects explicitly and if one tries to assign them a wrong type then a typecasting error is thrown. In case of Python, an object can be of any type and can be assigned to any type dynamically.

    b. Python is crisp and brief in expression while Java is verbose. It means same quanta of work will require more lines of codes in Java as compared to Python.

    c. Python is easy to read as compared to Java

    d. When it comes to speed Java is faster. As Python is interpreted, so it runs slower than its' counterparts.

    e.  From portability perspective, Java edges over Python as JVM is widely present.

    f. In enterprise level critical data intensive applications, Java provides more robustness when it comes to handling database related operations than Python.

    Both languages have their own benefits. It all boils down to what you choose for your project. Python is simple and concise whereas Java is fast and more portable. Python is dynamically-typed while Java is statically-typed.Both are powerful in their own areas, what is your decision while selecting them, is driven by your need and wish.

    Q2. How is Python executed?
    A: The execution of a Python program results in execution of the byte code on the Python Virtual Machine (PVM). Every time a Python script is executed, script converts into byte code. When a Python script is imported as a module, the byte code will be stored in the corresponding .pyc file which would need PVM for its execution.

    Q3. What is the difference between .py and .pyc files?
    A: When a Python module is loaded, the .py file is "byte compiled" to .pyc files. This is done to load module faster rather run faster. The timestamp is recorded in pyc files.

    Q4. How do you invoke the Python interpreter for interactive use?
    A: Python has two basic modes: script and interactive. In normal mode, the scripted and finished .py files are run in the Python interpreter.

    Interactive mode is a command line shell which gives immediate feedback for each statement, while running previously fed statements in active memory. Interactive mode is a good way to play around and try variations on syntax.

    Q5. How do you execute a Python Script?
    A: You can simply call from command prompt:

    python /filename.py

    Please make sure python runtime is included in system PATH variable.

    Q6. Explain the use of try: except raise, and finally:
    A: In Python programs, exception handling is done through try, except, raise and finally statements. In try block of code, that snippet is written which is expected to throw an exception or error and the code that handles exception is written in except clause. The exceptions are raised when corresponding errors occur at run time, but we can forcefully raise it using the keyword raise. Every try statement in Python may have an optional finally clause which is executed no matter what, and is generally used to release external resources.

    Q7. What happens if an error occurs that is not handled in the except block?
    A: If an error happening in try block is not handled in except block, such a code may create problems in production environment(in severe mishandling situations, may crash production system too if error creates memory intensive leakages) since it is not properly tackled what to do when a certain error happens.

    Q8. How are modules used in a Python program?
    A: A module in Python is a file containing Python code. It can define functions, classes and variables. One can import module by using keyword called "import". Python has many standard, pre defined modules too which can be utilized as per need.

    Q9. How do you create a Python function?
    A: Keyword "def" marks the start of function header.

    def function_name(parameters):
    """string to define what function does"""
    statement(s)
    One can call the function by just simply invoking it by name.

    function_name(parameters)

    Q10. How is a Python class created and how is it instantiated?
    A: In python a class is created by the keyword 'class' while object is created using the constructor of the class. This object will then be called the instance of the class. In Python we create classes in the following manner.

    class ClassName:
       
        .
        .
        .
       
    Instance of the class can be invoked something similar to given below:

    instance = ClassName(arguments)

    More questions on Python shall be posted in subsequent posts so keep checking this blog in near future.

    Please share your comments or queries, if you have any.

    Post Top Ad

    Post Bottom Ad