Python Interview Answers

  1. 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.
  2. Is there a tool to help find bugs or perform static analysis?
    Yes. PyChecker is a static analysis tool that finds bugs in Python source code and warns about code complexity and style.
  3. What are the rules for local and global variables in Python?
    In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a new value anywhere within the function's body, it's assumed to be a local. If a variable is ever assigned a new value inside the function, the variable is implicitly local, and you need to explicitly declare it as 'global'.
  4. How can I find the methods or attributes of an object?
    For an instance x of a user-defined class, dir(x) returns an alphabetized list of the names containing the instance attributes and methods and attributes defined by its class.
  5. Is there an equivalent of C's "?:" ternary operator?
  6. How do I convert a number to a string?
    To convert, e.g., the number 144 to the string '144', use the built-in function str(). If you want a hexadecimal or octal representation, use the built-in functions hex() or oct().
  7. What's a negative index?
    Python sequences are indexed with positive numbers and negative numbers. For positive numbers 0 is the first index 1 is the second index and so forth. For negative indices -1 is the last index and -2 is the penultimate index and so forth. Think of seq[-n] as the same as seq[len(seq)-n].
    Using negative indices can be very convenient. For example S[:-1] is all of the string except for its last character, which is useful for removing the trailing newline from a string.
  8. What is a method?
    A method is a function on some object x that you normally call as Methods are defined as functions inside the class definition:
    class C:
    def meth (self, arg):
    return arg*2 + self.attribute
  9. What is self?
    Self is merely a conventional name for the first argument of a method. A method defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for some instance x of the class in which the definition occurs; the called method will think it is called as meth(x, a, b, c).
  10. How do I delete a file?
    Use os.remove(filename) or os.unlink(filename);
  11. How do I copy a file?
    The shutil module contains a copyfile() function.
  12. Can I create my own functions in C?
    Yes, you can create built-in modules containing functions, variables, exceptions and even new types in C.