1. What is the difference between Xrange and range?
A: Xrange returns the xrange object while range returns the list, and uses the same memory and no matter what the range size is.
2. What is the difference between list and tuple?
A: The difference between list and tuple is that list is mutable while tuple is not. Tuple can be hashed for e.g as a key for dictionaries.
3. How are arguments passed by value or by reference?
A: Everything in Python is an object and all variables hold references to the objects. The references values are according to the functions; as a result you cannot change the value of the references. However, you can change the objects if it is mutable.
4. What are Python decorators?
A: A Python decorator is a specific change that we make in Python syntax to alter functions easily.
5. What is lambda in Python?
A: It is a single expression anonymous function often used as inline function.
6. Why lambda forms in python does not have statements?
A: A lambda form in python does not have statements as it is used to make new function object and then return them at runtime.
7. What are local variables and global variables in Python?
A: Global Variables:
Variables declared outside a function or in global space are called global variables. These variables can be accessed by any function in the program.
Local Variables:
Any variable declared inside a function is known as a local variable. This variable is present in the local space and not in the global space.
8. What is self in Python?
A: Self is an instance or an object of a class. In Python, this is explicitly included as the first parameter. However, this is not the case in Java where it’s optional. It helps to differentiate between the methods and attributes of a class with local variables.
The self variable in the init method refers to the newly created object while in other methods, it refers to the object whose method was called.
9. What is pickling and unpickling?
A: Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.
10. How will you capitalize the first letter of string?
A: In Python, the capitalize() method capitalizes the first letter of a string. If the string already consists of a capital letter at the beginning, then, it returns the original string.