
Play Store Application link – Java to Python in 17 Steps – App on Google Play
Github project link – https://github.com/kuldeep101990/Python_step7
Modules and packages are fundamental for structuring and organizing code in Python. If you’re familiar with Java packages, you’ll find many similarities, with Python’s approach being more dynamic and flexible.
Importing Modules (import
vs. Java import
)
In Java, we use import
to include predefined classes from packages, such as java.util.ArrayList
. Similarly, Python uses import
to include external or user-defined modules.
Java Example:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Hello");
System.out.println(list);
}
}
Python Equivalent:
import math
# Using the imported module
print(math.sqrt(16))
- In Python, you can also import specific functions or classes from a module:
from math import sqrt
print(sqrt(16))
Standard Python Libraries
Python comes with an extensive standard library, equivalent to Java’s standard library. Here are some commonly used modules:
os
: Interact with the operating system.sys
: Access system-specific parameters and functions.math
: Perform mathematical operations.datetime
: Handle dates and times.
Example Usage:
import os
print("Current working directory:", os.getcwd())
import sys
print("Python version:", sys.version)
import math
print("Pi value:", math.pi)
from datetime import datetime
print("Current date and time:", datetime.now())
Creating and Using Packages
Packages in Python are directories containing a special __init__.py
file (this file can be empty). They help organize modules into namespaces.
Creating a Package in Python:
- Create a folder named
mypackage
. - Inside it, create:
__init__.py
: Marks the directory as a package.module1.py
: Contains Python functions or classes.
Folder Structure:
mypackage/
__init__.py
module1.py
Example Content of module1.py
:
def greet(name):
return f"Hello, {name}!"
Using the Package:
from mypackage import module1
print(module1.greet("Alice"))
The __name__ == "__main__"
Construct
In Java, the main
method is the entry point for execution. In Python, we use the __name__ == "__main__"
construct to determine if a script is being run directly or imported as a module.
Java Example:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Python Equivalent:
def main():
print("Hello, World!")
if __name__ == "__main__":
main()
When a Python file is run directly, its __name__
is set to "__main__"
. If the file is imported, __name__
is set to the module’s name.
Complete Python Program
Here’s a program that demonstrates modules, packages, and the __name__ == "__main__"
construct:
Folder Structure:
mypackage/
__init__.py
calculator.py
main.py
Content of calculator.py
:
def add(a, b):
return a + b
def subtract(a, b):
return a - b
Content of main.py
:
from mypackage.calculator import add, subtract
def main():
print("Addition:", add(5, 3))
print("Subtraction:", subtract(5, 3))
if __name__ == "__main__":
main()
How to Run:
- Save the files as per the folder structure.
- Run the
main.py
file:python main.py
Sample Output:
Addition: 8
Subtraction: 2
By understanding and leveraging modules and packages, you can create modular, maintainable, and reusable Python code. Try these examples to deepen your knowledge!