Important Questions of 11th Class
Important Questions of 11th Class
import math as m
print(m.pow(2, 3))
6. What is the __name__ variable in Python?
Answer:
The __name__ variable is a special built-in variable in Python. When a Python script is run
directly, __name__ is set to '__main__'. If the script is imported as a module, __name__ is
set to the name of the module.
Example:
# mymodule.py
def greet(name):
print(f"Hello, {name}!")
if __name__ == "__main__":
greet("Alice")
If you run mymodule.py directly, it will call the greet function. If you import it into another
script, the greet function will not run automatically.
7. What is the Purpose of the import Statement?
Answer:
The import statement is used to bring in modules or specific functions, classes, or variables
from a module into the current Python script, allowing you to reuse the code without having
to rewrite it.
Example:
import random
print(random.randint(1, 10)) # Generates a random number between 1 and 10
8. What is the sys.path?
Answer:
The sys.path is a list in Python that contains the directories where Python looks for
modules. When you import a module, Python checks the directories listed in sys.path to
find the module.
You can view and modify sys.path to add custom directories for module search.
Example:
import sys
print(sys.path)
9. What is the __init__.py File in a Package?
Answer:
The __init__.py file is a special file in a directory that indicates the directory is a Python
package. This file can be empty, or it can execute initialization code for the package when it
is imported.
Example:
mypackage/
├── __init__.py
├── module1.py
└── module2.py
You can import from mypackage like this:
from mypackage import module1
10. How to Install External Python Modules?
Answer:
You can install external Python modules using pip, which is Python’s package manager.
Example: To install the requests module:
pip install requests
After installation, you can import and use the module in your code:
import requests
response = requests.get('https://example.com')
print(response.status_code)
11. What is the Role of dir() Function in Modules?
Answer:
The dir() function is used to list all the attributes and methods available in a module. This
helps to inspect the contents of a module.
Example:
import math
print(dir(math)) # Lists all the attributes and functions in the math
module
12. How to Use help() Function to Get Information About a Module?
Answer:
The help() function can be used to get detailed documentation about a module or its
functions.
Example:
import math
help(math)
This will display the documentation for the math module, listing its functions, classes, and
description.