0% found this document useful (0 votes)
352 views3 pages

Important Questions of 11th Class

Teek hai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
352 views3 pages

Important Questions of 11th Class

Teek hai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Important Questions of [Introduction of python modules]

1. What is a Python Module?


Answer:
A module in Python is simply a file containing Python code. It can contain functions, classes,
and variables, as well as runnable code. The purpose of modules is to organize and reuse
code. For example, Python’s built-in math module provides mathematical functions, while os
provides functions to interact with the operating system.
Example: A Python file mymodule.py is a module.
2. What is the Difference Between a Module and a Package?
Answer:
 A module is a single Python file (with a .py extension) that contains Python code.
 A package is a collection of Python modules in a directory. A directory is considered
a package if it contains a special __init__.py file.
Example:
 math.py is a module.
 numpy is a package, which contains several modules like numpy.core,
numpy.linalg, etc.
3. How to Create a Python Module?
Answer:
To create a Python module, simply write Python code in a file and save it with a .py
extension.
Example:
# mymodule.py
def greet(name):
print(f"Hello, {name}!")
You can then import and use this module in another Python file:
import mymodule
mymodule.greet('Alice')
4. What are Built-in Python Modules?
Answer:
Python comes with a set of built-in modules that provide standard functionality. These
include modules for file handling, operating system interaction, networking, data
manipulation, and more.
Some common built-in modules include:
 math for mathematical functions.
 datetime for handling dates and times.
 sys for interacting with the interpreter.
 os for interacting with the operating system.
Example:
import math
print(math.sqrt(16)) # Outputs: 4.0
5. How to Import a Module in Python?
Answer:
There are several ways to import a module in Python:
 Basic import:
 import module_name
This imports the whole module.
 Import specific functions or classes:
 from module_name import function_name
This imports only the specified function or class.
 Import with alias:
 import module_name as alias
This imports the module with an alias for easier reference.
Example:
import math
print(math.pi)

from math import sqrt


print(sqrt(25))

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.

You might also like