Python Interview Questions and Answers For Freshers part-3
21.Define docstring in Python.
A string literal occurring as the first statement (like a comment) in any module, class, function or method is referred as docstring in Python. This kind of string becomes the _doc_ special attribute of the object and provides an easy way to document a particular code segment. Most modules do contain docstrings and thus, the functions and classes extracted from the module also consist of docstrings.
22. Name the optional clauses used in a ‘try-except’ statement in Python?
While Python exception handling is a bit different from Java, the former provides an option of using a try-except clause where the programmer receives a detailed error message without termination the program. Sometimes, along with the problem, this try-except statement offers a solution to deal with the error.
The language also provides try-except-finally and try-except-else blocks.
23. How to use PYTHOPATH?
PYTHONPATH is the environment variable consisting of directories. $PYTHONPATH is used for searching the actual list of folders for libraries.
24. Define ‘self’ in Python?
25. Define CGI?
Common Gateway Interface support in Python is an external gateway to interact with HTTP server and other information servers. It consists of a series of standards and instructions defining the exchange of information between a custom script and web server. The HTTP server puts all important and useful information concerning the request in the script environment and then run the script and sends it back in the form of output to the client.
26. What is PYTHONSTARTUP and how is it used?
PYTHONSTARTUP is yet another environment variable to test the Python file in the interpreter using interactive mode. The script file is executed even before the first prompt is seen. Additionally, it also allows reloading of the same script file after being modified in the external editor.
27. What is the return value of trunc() in Python?
truc() returns integer value. Uses the _trunc_ method
>>> import intellipaat
28. How to convert a string to an object in Python?
To convert string into object, Python provides a function eval(string). It allows the Python code to run in itself
29. Is there any function to change case of all letters in the string?
Yes, Python supports a function swapcase(), which swaps the current letter case of the string. This method returns a copy of the string with the string case swapped.
30.What is pickling and unpickling in Python?
The process of Pickling relates to the Pickle module. Pickle is a general module that acquires a python object and converts it into string. It further dumps that string object into a file by using dump () function.
Pickle comprises two methods:
Dump (): dumps an object to a file object
and Load (): loads an object from a file object
Unpickling is the reacquiring process to perform retrieval of the original Python object from the stored string for reuse.