Question 25: What is difference between help() and dir() ?
help () : As we know Python has a lot of modules, classes , functions and methods . It is not possible to remember these.
So if you are in middle of code and got stuck due to you don't remember the module , method and keyword. You just have to pass the keyword (module, function and any keyword) in help() function and you will get the documentation of the related keyword (passed keyword).
Note that if you don't pass any keyword, then during program execution help() will ask you to pass one . And if keyword is not related to python, then it will return 'no documentation for the keyword'.
Let's see with an example.
|#help() with keyword|
Note: It's not possible to show whole output here , it's just a small snapshot.
dir(): dir() also return the information of passed keyword/object but unlike help() (help return documentation) it just return the valid attributes and methods of the passed keyword.
With no argument passed in dir() , it return the attributes and methods in current scope.
|#dir() with keyword|
Question 26: What is decorators in python ?
Answer: Decorators: In simple terms decorators in python add or enhance the functionality of other function (on which decorators applied ) without changing the structure of function itself.
Decorator itself is a function used on another function to enhance the function functionality without changing its structure.
To understand decorators you must know functions and function's properties very well.
Let's understand the concept of decorators with an example-
For example we have a simple question as mentioned below
#defining a simple function
Now we will defined a new function which takes function as an argument.
#defining decorators in pyhton
print("executing dec_fun1 function ")
Note: As you can see we defined a function (dec_fun) which takes function as an argument and a nested function (dec_fun1) in which first we passed a print statement after that we called the argument function (passed in dec_fun) and then again a print statement.
Now we want to use the functionality of dec_fun() function by any_function(). Or we can say that we want to use any_function() in dec_fun().
#enhance the functionality of dec_fun
obj = dec_fun(any_function)
Note: You can see that we just passed the any_function to dec_fun as an argument and the functionality of dec_fun is enhanced by any_function Or we can say that we used an existing function and functionality by another function. This process is actually known as decorator where we can use a function and its functionality by other function.
We can do the same concept in another way, How ?
# @ in decorators
Note: It is simple if you want to enhance a function's functionality you just have to write @ function_name_you_want _to _use.
meaning @dec_function = dec_fun(any_funciton).
Question 27: What is PEP 8 in python ?
Answer: PEP in python stand for - Python Enhancement Proposal. PEP is actually a designed official python documentation with set of rules to defined the format of python code writing to the python community and also new features and process for python.
pep-8 is specially important as it written by Guido van Rossum and its major task is to improve the readability and consistency of Python code.
We have pep 0 ,pep 1,.......indexes you can visit official website here -click here
Question 28 : Named some commonly used python modules ? (OS and SYS module )
Answer: As we know python modules are collection python files (codes) of function and objects. We can use modules directly by importing them.
Here are some in built python modules - date time , JSON , os , sys , random, math etc...
OS module in python
The os (operating system) module in python used to create , remove and change directories.
Let's see the methods/ functions in os module.
#methods and function of os
Note: It's not full output but just a snapshot , Try it yourself . Now from the output you can see that os module has functions/methods such as mkdir (to make a new directory , folder), open (to open a file in current directory), rename (to change a file name in current directory) and many more.
Note that if you change a directory to another, that means the changed directory now is your current working directory. And you can only modify or update the current working directory.
Let's display current working directory.
#os module in pyhton
Note: The output shows the current working in which I am doing work, when you will run this program in your computer , it will display the current working in which you are working. You can change current working directory to other by using chdir() function.
You can also check list of all files and directories in the specified directory. You can use listdir() function for this purpose. See the example given below.
#display files and directories
Sys Module in Python
Sys module in python provide functions and variables used to manipulate python runtime environment. And access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
Sys module basically used for system specific parameters and functions.
We will see some examples to understand sys module.
#exit() in sys
print("After exiting system ")
#sys in python
Note: The second output display the current version of python interpreter. And the first output terminate the program where we want it to stop.
Official Documentation of Sys module