Python Creating Modules

PythonPythonBeginner
Practice Now

Introduction

In this lab, we'll immerse ourselves in a space station scenario, where we take on the role of a space station commander. Our goal is to utilize our Python skills to create modules that will aid in the management and smooth operation of the space station. This engaging scenario will allow us to apply our Python knowledge in a practical and exciting setting.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python/ModulesandPackagesGroup -.-> python/creating_modules("`Creating Modules`") subgraph Lab Skills python/creating_modules -.-> lab-271537{{"`Python Creating Modules`"}} end

Creating a Utility Module

In this step, we will start by creating a utility module called space_util.py, which will contain functions for basic space station operations.

First, let's open the space_util.py file in the /home/labex/project directory with the following content:

## space_util.py

def oxygen_level():
    return 98

def temperature():
    return 25

print(oxygen_level())
print(temperature())

Run the script:

python space_util.py

The information below should be displayed on your terminal:

98
25

Using the Created Module

In this step, let's utilize the space_util module. Open a new file named /home/labex/project/station_status.py with the following code:

## station_status.py
import space_util

print("Oxygen Level: {}%".format(space_util.oxygen_level()))
print("Temperature: {}°C".format(space_util.temperature()))

Run the script:

python station_status.py

The information below should be displayed on your terminal:

Oxygen Level: 98%
Temperature: 25°C

Summary

In this lab, we created and utilized Python modules to simulate space station operations. By designing this scenario-based lab, we aimed to provide a practical application for you to reinforce their understanding of creating and using modules in Python. This lab helps you connect theoretical concepts with real-world applications and fosters a deeper understanding of Python module creation.

Other Python Tutorials you may like