Lesson · 40 min · Free
Python Modules: Basics
Python Modules: Basics body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2c3e50; } pre { background-color: #ecf0f1; padding: 15px; border-radius: 5px; overflow-x: auto; } code { font-fami
Python Modules: Basics
Welcome to the "Python for Pharmaceutical Research" course! In this lesson, we'll delve into the fundamental concept of Python modules. As you progress in your pharmaceutical research, you'll encounter increasingly complex tasks that require organizing your code efficiently. Modules are Python's way of allowing you to logically organize your Python code, making it reusable and easier to manage. Think of a module as a single file containing Python definitions and statements. This file can define functions, classes, and variables. When you want to use these definitions in another Python script, you simply "import" the module. This mechanism promotes code reusability, reduces redundancy, and makes your projects more maintainable, which is crucial in scientific computing where reproducibility and clarity are paramount. For instance, imagine you've written a set of functions to calculate molecular weights or to perform specific statistical analyses on drug efficacy data. Instead of copying these functions into every new script, you can put them into a module and import them as needed. This not only saves time but also ensures consistency across your analytical workflows.
Importing Modules
There are several ways to import modules in Python. The most common and straightforward method is using the import statement, followed by the module name. This makes all the functions, classes, and variables defined within that module available in your current script, prefixed by the module name. Let's consider a simple example. Python's built-in math module provides access to mathematical functions. If you want to calculate the square root of a number, you would typically use the sqrt() function from the math module. # Example 1: Basic Module Import import math # Calculate the square root of 64 result = math.sqrt(64) print(f"The square root of 64 is: {result}") # Calculate the value of pi pi_value = math.pi print(f"The value of pi is: {pi_value}") Another way to import specific components from a module is using the from ... import ... statement. This allows you to import only the functions, classes, or variables you need, directly into your current namespace, without having to prefix them with the module name. While convenient, be mindful of potential naming conflicts if you import many items this way. # Example 2: Importing Specific Items from math import sqrt, pi # Calculate the square root of 100 result_specific = sqrt(100) print(f"The square root of 100 is: {result_specific}") # Use the value of pi directly print(f"Pi directly accessed: {pi}") # You can also import all names using '*' (not generally recommended for larger projects) # from math import * # print(f"Cosine of 0: {cos(0)}") # cos is now directly available Understanding modules is a foundational step in writing robust and scalable Python code for pharmaceutical research. As your projects grow, you'll find yourself creating your own modules to encapsulate domain-specific functions, such as those for pharmacokinetic modeling, drug-target interaction analysis, or data preprocessing pipelines.
Key Takeaways
Modules are Python files ( .py ) that contain Python code (functions, classes, variables). They help organize code, promote reusability, and improve maintainability. The import module_name statement imports the entire module. The from module_name import item1, item2 statement imports specific items from a module. Using modules is essential for building complex and well-structured applications in scientific computing.
Practice Exercise: Creating and Using Your Own Module
Create a new Python file named pharma_utils.py . Inside this file, define a function called calculate_molecular_weight(formula) that takes a string representing a simple chemical formula (e.g., "H2O", "C6H12O6") and returns its approximate molecular weight. For simplicity, use the following atomic weights: H=1.008, C=12.011, O=15.999. You can hardcode these values in a dictionary within your function. Then, in a separate Python script, import your pharma_utils module and use your calculate_molecular_weight function to find the molecular weight of "C8H9NO2" (Acetaminophen).
Watch the full lesson — free
This topic is part of Python for Pharmaceutical Research, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →