Python Interview Questions and Answers – Tec Geeks
What is Python?
Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high-level dynamic data types, and classes. Python combines remarkable power with very clear syntax. It has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in C or C++. It is also usable as an extension language for applications that need a programmable interface. Finally, Python is portable: it runs on many Unix variants, on the Mac, and on PCs under MS-DOS, Windows, Windows NT, and OS/2.
Is there a tool to help find bugs or perform the static analysis?
Yes. PyChecker is a static analysis tool that finds bugs in Python source code and warns about code complexity and style.
Are there coding standards or a style guide for Python programs?
Why are Python strings immutable?
There are several advantages.
One is performance: knowing that a string is immutable means we can allocate space for it at creation time, and the storage requirements are fixed and unchanging. This is also one of the reasons for the distinction between tuples and lists. Another advantage is that strings in Python are considered as “elemental” as numbers. No amount of activity will change the value 8 to anything else, and in Python, no amount of activity will change the string “eight” to anything else.
Why can’t lambda forms contain statements?
Python lambda forms cannot contain statements because Python’s syntactic framework can’t handle statements nested inside expressions. However, in Python, this is not a serious problem. Unlike lambda forms in other languages, where they add functionality, Python lambdas are only a shorthand notation if you’re too lazy to define a function.
Why isn’t all memory freed when Python exits?
Objects referenced from the global namespaces of Python modules are not always deallocated when Python exits. This may happen if there are circular references. There are also certain bits of memory that are allocated by the C library that are impossible to free (e.g. a tool like Purify will complain about these). Python is, however, aggressive about cleaning up memory on exit and does try to destroy every single object.
Is there an equivalent to C’s onexit() in Python?
The atexit module provides a register function that is similar to C’s on exit.
Why don’t my signal handlers work?
The most common problem is that the signal handler is declared with the wrong argument list. It is called as handler(signum, frame). So it should be declared with two arguments:
def handler(signum, frame):
How do I create documentation from docstrings?
The pydoc module can create HTML from the docstrings in your Python source code. An alternative for creating API documentation purely from docstrings is epydoc. Sphinx can also include docstring content.