0% found this document useful (0 votes)
7 views14 pages

M3 Creating Python Package IBM

This document outlines a hands-on lab for creating a Python package named 'mymath' that includes two modules: 'basic' and 'stats', each containing two functions. It provides step-by-step instructions for creating the package structure, writing the module code, and verifying the package functionality. Additionally, it includes a practice exercise to extend the package with a new module named 'geometry'.

Uploaded by

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

M3 Creating Python Package IBM

This document outlines a hands-on lab for creating a Python package named 'mymath' that includes two modules: 'basic' and 'stats', each containing two functions. It provides step-by-step instructions for creating the package structure, writing the module code, and verifying the package functionality. Additionally, it includes a practice exercise to extend the package with a new module named 'geometry'.

Uploaded by

Lorena Paciello
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Hands-on Lab: Creating a

Python Package

Creating a Python Package

Estimated time needed: 30 minutes

Objectives

In this lab you will :

Create a module named basic

Add two functions to the module basic

Create a module named stats

Add two functions to the module stats

Create a python package named mymath

Verify that the package is working


Lab
Create Package

On the window to the right, click on the View menu and


select Explorer option, as shown in the image below.

Your IDE now should look like the image below.

On the window to the right, click on the File menu and


select New Folder option, as shown in the image below.
Enter mymath and click OK as shown in the image below.
Create the first module
Create a python module named basic

Create a file named [Link].

Copy and paste the below code into [Link]

plaintext

def square(number):
"""
This function returns the square of a given number
"""
return number ** 2

def double(number):
"""
This function returns twice the value of a given number
"""
return number * 2

def add(a, b):


"""
This function returns the sum of given numbers
"""
return a + b

You should see a screen like this now.


Save the file [Link]
Create the second module
Create a module named stats

Create a file named [Link].

Copy and paste the below code into [Link]


plaintext

def mean(numbers):
"""
This function returns the mean of the given list of numbers.
The mean is calculated as the sum of all numbers divided by the count of
"""
# Calculate the mean by summing all the numbers and dividing by the leng
# 'sum(numbers)' computes the total of all numbers in the list.
# 'len(numbers)' gives the count of numbers in the list.
# The mean (or average) is the total sum divided by the number of elemen
return sum(numbers) / len(numbers) # Return the mean value.

def median(numbers):
"""
This function returns the median of the given list of numbers.
The median is the middle value when the numbers are sorted.
If there is an even number of observations, it returns the average of th
"""
# Sort the list of numbers in ascending order.
# Sorting is necessary to find the median, as the median depends on the
[Link]()

# Check if the number of elements in the list is even.


# len(numbers) % 2 == 0 evaluates to True if the list has an even number
if len(numbers) % 2 == 0:
# If the list has an even number of elements, find the two middle nu
# 'len(numbers) // 2' computes the index of the second middle elemen
# 'len(numbers) // 2 - 1' computes the index of the first middle ele
median1 = numbers[len(numbers) // 2] # The higher index of the two
median2 = numbers[len(numbers) // 2 - 1] # The lower index of the t

# Calculate the median by taking the average of the two middle numbe
# This is done by adding the two middle values and dividing by 2.
mymedian = (median1 + median2) / 2 # Average of the two middle valu
else:
# If the list has an odd number of elements, return the middle numbe
# 'len(numbers) // 2' computes the index of the middle element.
mymedian = numbers[len(numbers) // 2] # The middle value for lists

# Return the calculated median value.


return mymedian

You should see a screen like this now.


Save the file [Link]
Create __init__.py
Create the file `__init__.py`

Copy and paste the below code into `__init__.py`

plaintext

from . import basic


from . import stats

Save the file `__init__.py`

Now your directory structure should look like

plaintext

mymath
mymath/__init__.py
mymath/[Link]
mymath/[Link]

You are done creating a package


Verify the package
On the window to the right, click on the Terminal menu
and select New Terminal option, as shown in the image
below.

You will see a terminal open up on the bottom of the


screen like the one in the image below.

At the terminal type python3 to invoke python interpreter.

Once the python interpreter is loaded.

At the python prompt type import mymath

If the above command runs without errors, it is an


indication that the mymath package is successfully
loaded.

At the python prompt type [Link](3,4)

You should see an output 7 on the screen.


At the python prompt type [Link]([3,4,5])

You should see an output 4.0 on the screen.

Type exit() to quit python interpreter.


Practice Exercise
Create a new module named geometry and add
to the mymath package.

Create a module name geometry

Add a function named `area_of_rectangle` that takes


length and breadth as input and returns the area of a
rectangle.

Add a function named `area_of_circle` that takes


radius as input and returns the area of a circle.

Modify the `__init__.py` to include this module.

Import and test the function `area_of_circle` from


python terminal.

Authors

Ramesh Sannareddy

Other Contributors

Rav Ahuja

© IBM Corporation. All rights reserved.

This notebook and its source code are released


under the terms of the MIT License.
Lab
Create Package

On the window to the right, click on the View menu and


select Explorer option, as shown in the image below.

Your IDE now should look like the image below.

On the window to the right, click on the File menu and


select New Folder option, as shown in the image below.
Enter mymath and click OK as shown in the image below.

Hands-on Lab: Creating a… Create the first module

The content of this lab is licensed under Apache 2.0

You might also like