Python Interview Questions (Part 2)
Why was the language called as Python?
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.Name few Python Web Frameworks for developing web applications?
There are various web frameworks provided by Python. Some popular frameworks are :
- Django
- TurboGears
- Web2py
- Flask
- Pyramid
- Cubic Web
What is PEP 8?
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.
What IDE to use for Python?
There are various IDE for Python development. Some popular IDEs are :
- Pycharm IDE
- Pydev IDE
- Wing IDE
- Eric Python IDE
- Vim IDE
- IPython Notebook
What are all the operating system that Python can run on?
Python is a platform independent language, it works for all Operating Systems like, Windows, Unix, Linus, MacOS etc.
Is python a case sensitive language?
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.
What is the purpose of PYTHONPATH environment variable?
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.Docstrings vs Comments
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.What's wrong with import all?
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).
How is print statement represented in Python 3 (v/s Python2)?
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.How can I read inputs as integers/float?
The input()method in Python return strings. Convert the result to integer explicitly with int()/float().
What are the supported data types in Python?
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.
Mappings: dict.
What are Python decorators?
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.Is there a switch..case statement in Python?
No. There is no switch or case in Python.
How can we get home directory using '~' in Python?
On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user's home directory.
or
What is the purpose of the "//" operator in python?
"//" is for flooring division
How do I test one variable against multiple values?
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.
How to call an external command in Python
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. Windows exampleMeaning of a single and double underscore before an object name?
- _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 read a single character from the user?
Use of double quotes and single quote in Python
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.
What's the difference between raw_input() and input() in python?
- 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.
What is the difference between Xrange and range?
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().How to exit python script in command prompt?
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()!What is negative index in Python?
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.
Is Python object oriented?
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.Why is Python not fully object-oriented?
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".What is a Class? How do you create it in Python?
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.
How to convert strings into integers in Python?
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.What is the output of print str * 2 if str = 'Hello World!'?
It will print 'Hello World!' two times.
How to capitalize the first letter of each word in a string (Python)?
The .title() method of a string (either ASCII or Unicode is fine) will capitalize the first letter of each word in a string.
How to check the string consists of alphanumeric characters ?
The method isalnum() checks whether the string consists of alphanumeric characters.How will you check in a string that all characters are digits?
Python str.isdigit() method return true if all characters in the string are digits and there is at least one character, false otherwise.How will you remove all leading and trailing whitespace in string?
You can use the strip() to trim whitespace in Python.- Python Interview Questions (Part 3)
- 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?
- Global and Local Variables in Python
- 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?
- What is "typeerror: 'module' object is not callable"
- Python: TypeError: unhashable type: 'list'
- How to convert bytes to string in Python?
- What are metaclasses in Python?