Python Interview Questions FAQ - 1
Please click on the questions to see the answers
Python actually got its name from a BBC comedy series from the seventies "Monty Python's Flying Circus". The designer needed a name that was short, unique, and slightly mysterious. Since he was a fan of the show he thought this name was great, so he decided to call the language Python.
There are various web frameworks provided by Python. Some popular frameworks are :
- Cubic Web
Pep8 is a coding standard and Style Guide for readability and long-term maintainability. It was designed to help python developers write more readable code. It's not a requirement for your code to work, just a good coding practice you should follow.
There are various IDE for Python development. Some popular IDEs are :
- Pycharm IDE
- Pydev IDE
- Wing IDE
- Eric Python IDE
- Vim IDE
- IPython Notebook
Python is a platform independent language, it works for all Operating Systems like, Windows, Unix, Linus, MacOS etc.
Yes, Python a case sensitive language Case sensitive means that x is different from X. The variable of John is different from the variable of john.
Setting the PYTHONPATH environment variable is an easy way to make Python modules available for import from any directory. When you import modules in python, python searches for the module in the directories in PYTHONPATH, in addition to some other directories.
Documentation is important to understand what the code does. Docstrings are for people who are going to be using your code without needing or wanting to know how it works. Docstrings can be turned into actual documentation. While, comments are meant to give specific information on blocks or lines, #TODO is used to remind you what you want to do in future, definition of variables and so on.
Importing * from a module, you will import all the functions and classes in your own namespace, which may clash with the functions you define yourself. Also you don't know exactly what is imported and can't find place from what module certain thing was imported easily (readability).
In Python 2.x print is actually a special statement, while in Python 3.x the print statement has been replaced with a print() function. It means that we have to wrap the object that we want to print in parentheses. Python 2 doesn't have a problem with additional parentheses, but in contrast, Python 3 would raise a Syntax Error if we called the print function without the parentheses.
The input()method in Python return strings. Convert the result to integer explicitly with int()/float().
Python's data types are built in the core of the language. Sticking to the hierarchy scheme used in the official Python documentation these are numeric types, sequences, sets and mappings.
Numeric types: int, long, float, complex.
Sequences: String, bytes, byte array, list, tuple.
Sets: set, frozen set.
A decorator is a function that takes another function and extends the behaviour of the latter function without explicitly modifying it. This supports more readable applications of the DecoratorPattern but also other uses as well.
No. There is no switch or case in Python.
On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user's home directory.
"//" is for flooring division
The "in" operator you can use in this case.
Above statement validate... x=1 or x=2 or x=3 or x=4 or x=5.
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. The subprocess.call() method run the command described by args. Wait for command to complete, then return the returncode attribute.
_abc: weak "internal use" indicator. E.g. from X import * does not import objects whose name starts with single underscore.
__abc: the interpreter replaces this name with _classname__abc as a way to ensure that the name will not overlap with a similar name in another class.
Python does not have that restriction of single quotes for chars and double quotes for strings.
Both are equal and what you use is entirely your preference.
In Python 2, raw_input() takes exactly what the user typed and passes it back as a string.
In Python 3, raw_input() was renamed to input() so now input() returns the exact string and Old input() was removed.
In python 2.x range() returns a list and xrange() returns an xrange object, which is kind of like an iterator and generates the numbers on demand. In Python 3, there is no xrange() , but the range() function behaves like xrange in Python 2. If you want to write code that will run on both Python 2 and Python 3, you should use range().
To leave the interactive shell and go back to the console (the system shell), press Ctrl-Z and then Enter on Windows, or Ctrl-D on OS X or Linux. Alternatively, you could also run the python command exit()!
In python we can Both positive and negative index . Negative index is used in python to index starting from the last element of the list, tuple or any other container class which supports indexing.
-1 refers to the last index, -2 refers to the second last index and so on.
Yes. Python is an object-oriented language, in which the program is built around objects which combine data and functionality. Python classes provide all the standard features of Object Oriented Programming. It is a mixture of the class mechanisms found in C++ and Modula-3.
Python is an object-oriented language but not pure. Python doesn't support strong encapsulation, which is only one of many features associated with the term "object-oriented".
Python Object is simply a collection of data (variables) and methods (functions) that act on those data. And, class is a blueprint for the object.
Here is a simple class definition.
Python int() method is the standard built-in function to convert a string into an integer value. You call it with a string containing a number as the argument, and it returns the number converted to an actual integer.
It will print 'Hello World!' two times.
The .title() method of a string (either ASCII or Unicode is fine) will capitalize the first letter of each word in a string.
The method isalnum() checks whether the string consists of alphanumeric characters.
Python str.isdigit() method return true if all characters in the string are digits and there is at least one character, false otherwise.
You can use the strip() to trim whitespace in Python.
- Python Interview Questions FAQ - 2
- What is python used for?
- Is Python interpreted, or compiled, or both?
- Explain how python is interpreted
- How do I install pip on Windows?
- How do you protect Python source code?
- What are the disadvantages of the Python?
- How would you achieve web scraping in Python?
- How to Python Script executable on Unix
- What is the difference between .py and .pyc files?
- What is __init__.py used for in Python?
- What does __name__=='__main__' in Python mean?
- What is docstring in Python?
- What is the difference between runtime and compile time?
- How to use *args and **kwargs in Python
- Purpose of "/" and "//" operator in python?
- What is the purpose pass statement in python?
- Why isn't there a switch or case statement in Python?
- How does the ternary operator work in Python?
- What is the purpose of "self" in Python
- How do you debug a program in Python?
- What are literals in python?
- What is Python's parameter passing mechanism?
- What is the process of compilation and Loading in python?
- How to share a global variable across python modules
- Is there a tool to help find bugs or perform static analysis?
- What does the 'yield' keyword do in Python?
- Comparison Operators != is not equal to in Python
- What is the difference between 'is' and '==' in python
- What is the difference between = and == in Python?
- How are the functions help() and dir() different?
- What is the python keyword "with" used for?
- Is all the memory freed when Python exits?
- Difference between Mutable and Immutable in Python
- Explain split() methods of "re" module in Python
- Accessor and Mutator methods in Python
- How to Implement an 'enum' in Python
- Important characteristics of Python Objects
- How to determine the type of instance and inheritance in Python
- How would you implement inheritance in Python?
- How is Inheritance and Overriding methods are related?
- How can you create a copy of an object in Python?
- How to avoid having class data shared among instances in Python?
- Static class variables in Python
- Difference between @staticmethod and @classmethod in Python
- How to Get a List of Class Attributes in Python
- Does Python supports interfaces like in Java or C#?
- What is used to create Unicode string in Python?
- Difference between lists and tuples in Python?
- What are differences between List and Dictionary in Python
- Different file processing modes supported by Python
- How do you append to a file in Python?
- What are the differences between the threading and multiprocessing?
- Is there any way to kill a Thread in Python?
- What is the use of lambda in Python?
- What is map, filter and reduce in python?
- Is monkey patching considered good programming practice?