0% found this document useful (0 votes)
10 views9 pages

CPE 103 Laboratory Activity 5 Functions Modules and Packages

This laboratory activity introduces students to Python's modules and packages, emphasizing the use of built-in functions and user-defined functions. Students will create programs that utilize these concepts, including file handling and function creation, and explore various Python packages for scientific computing and web development. The activity also includes supplementary tasks such as creating a word filter and solving physics problems using functions.

Uploaded by

rheymonoy
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)
10 views9 pages

CPE 103 Laboratory Activity 5 Functions Modules and Packages

This laboratory activity introduces students to Python's modules and packages, emphasizing the use of built-in functions and user-defined functions. Students will create programs that utilize these concepts, including file handling and function creation, and explore various Python packages for scientific computing and web development. The activity also includes supplementary tasks such as creating a word filter and solving physics problems using functions.

Uploaded by

rheymonoy
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/ 9

Laboratory Activity No.

5
Functions, Modules, and Packages
Course Code: CPE103 Program: BSCPE
Course Title: Object-Oriented Programming Date Performed:
Section: Date Submitted:
Name: Instructor:
1. Objective(s):
This activity aims to introduce students to the concept of modules and packages in Python.
2. Intended Learning Outcomes (ILOs):
The students should be able to:
2.1 Use the different Python built-in functions, modules, and packages.
2.2 Create a program with user-defined functions
2.3 Create a program that will import or load a user-created module and import its functions.
2.4 Create a program that will import or load a user-created package and import its modules.
3. Discussion:
As the program gets larger and more complex, it will be unavoidable for a programmer to split related and
repeated codes into separate files, this in Python is referred to as Modules and Packages.

A module contains variables and functions that can be imported or used again in another program (Python
file) without the programmer having to code the same variables and functions again. A python program (.py
file) is considered to be a module. A package is simply a group of related modules or .py files combined
together. A package commonly is considered to be a library which contains an enormous amount of
functions and modules.

Python comes with built-in modules and packages such as the math module, statistics module, random module.
For scientific computing, the following packages is mentioned here. The NumPy package is a general-purpose
array-processing package. It provides a high-performance multidimensional array object. The NumPy package is
used for scientific computing. Another well-known Python package is Scikit-learn which is an open source
machine learning library that supports supervised and unsupervised learning for implementing Artificial
Intelligence related tasks. For Software Development, the Tkinter and the PyQt5 are the most commonly used.
For Web Development, Flask and Django are the most widely used framework(composed of packages) for
building websites with Python.

Functions
To implement a simple module or package, we need to refamiliarize ourselves with the concept of
functions. Recall that a function is composed of a block of codes stored together that when called it executes
those codes stored inside it. Functions help programmers organize related code into modular pieces that can be
called upon to perform repetitive tasks. The Python interpreter has a number of functions and types built into it
that are always available but programmers can add their own custom functions to implement more customized
logic. The image below shows an example of a function written in Python.

The def is a special keyword used to indicate a function definition or creation. The function_name is the name
of the function
to be created. Recall that a function is also declared using parenthesis. Inside the parenthesis are inputs or
arguments that can be accepted in the function through the declaration of parameters. Parameters are
temporary variables or placeholder that can

be used inside the function. In Python, a return value is optional since the interpreter automatically makes the
decision if it will be a void datatype function, an int datatype function, string datatype function, boolean
datatype function and adjusts memory allocation accordingly.

Functions can be built-in or user-defined, for instance the Python print() is an example of a built-in function. The
len() is also a built-in function. Other examples of built-in functions are: int(), str(), float(), bool(), list(), tuple(),
dict(), open(). Functions will be used in the activity to create modules and packages. More built-in functions can
be found in the official Python documentation.

Source: https://docs.python.org/3/library/functions.html

In this activity, you will be exploring the various built-in Python functions, modules, and packages, and how to
create functions in Python, put those functions in a module, call functions from a module to another program,
and create your own package of modules.

For more information you may also visit the official python documentation:
https://docs.python.org/3/tutorial/modules.html
https://docs.python.org/3/tutorial/modules.html#packages
4. Materials and Equipment:

Desktop Computer with Anaconda Python or Google


Colab Windows Operating System

5. Procedure:
For this activity we will be creating Python programs using the Spyder IDE.
Functions
Exploring built-in functions
1. Create a folder named builtinfunctions
2. Create a Python file inside the functions1 folder named evaluator.py and copy the code shown below:

Note: and, or, not must be small cases.


3. Run the program and observe the output. Try to analyze the purpose of the built-in functions used in
the program (the keywords in color violet).
4. You may modify the code in order to study it as it will be used later in the next sections of the activity.

Using the open() function for file handling


1. Create a folder named filehandling outside of builtinfunctions folder
2. Create a Python file inside the filehandling folder named filewriter.py
3. Open the filehandler.py using Spyder IDE and type the code as shown below:
Note: You may use help(open) either in a Python program or in the shell for more information about the
built-in function.
4. Run the program and observe the output.
5. Modify the program to create a file called newfile2.txt and print the message (excluding the “ “)
“This message was created using Python!”
6. Create another Python file inside the filehandling folder named filereader.py and type the code as shown
below:

7. It should display an error. Identify and resolve the cause of the error based on the message given by
Python. The file that should be read is newfile1.
8. After fixing the error, run the program again and observe the output.
9. Modify the filereader.py program so that the message of newfile2.txt is displayed.
10. Modify again the program with the following code below:

11. Create a new Python file still inside the oop1 folder called fileappender.py and copy the code
shown below:

12. Run the program and observe the output.

User-defined Functions
1. Create a new folder called userfunctions outside of filehandling folder
2. In the userfunctions folder create a program called truthtablegenerator.py and copy the code below:
3. Run the program and observe the output.
4. Modify the program by changing print(generate_truthtable(3)) to print(generate_truthtable()) then run the
program
5. An error should occur, modify the program according to the code shown below:

Note: The code modifications are underlined in red.


6. In the same file/program, create a new function with the name evaluate_propositional_logic(). The
parameter is c_list(combinations list) and the code can be found under the # main program
comment in the first program made earlier.
7. After successfully placing the code in the function, call the function using this code
evaluate_propositional_logic(generate_truthtable(3))
8. Analyze why in the generate_truthtable function we needed to print the function whereas in
the evaluate_propositional_logic function, it prints the values on its own.
9. Compare the program truthtablegenerator.py with evaluator.py. Identify the advantages of placing
code within functions against the sequential code done in the first.

Modules
Built-in Modules
math module
1. Create a new folder called modules1 outside of the userfunctions
2. Create a new Python file called mathmodule.py and copy the following codes as shown below:
3. Run the program and observe the output. You may switch between the two sets of values.
4. Create a new Python file called mathmodule2.py and copy the following codes as shown below:

,
5. To view additional functions in the module. Type and run help(math) while it is imported.
time and datetime module
1. Create a new file in the folder named dateandtime.py
2. Copy and run the code as show below:

3. Observe the output.


4. In the same file, copy and add the code to the file as shown below:

5. Run the program, and observe the output.


6. In the same file, copy and add the code to the file as shown below:

7. Run the program, and observe the output.


User-defined Modules
1. The previously created dateandtime.py is considered to be a module that you can import.
2. In the same modules1 folder, create a new file called main.py and copy the following code:

3. The program will not run as expected, and you will need to remove the following codes in the
dateandtime.py which are underlined in red.
4. Run the main.py program again and you will now see the correct output which is the current time.
5. Modify the main.py to also display the current date using the current_date() in dateandtime.py
6. To remove the need to constantly indicate the module name dateandtime. in each function, modify
the code as shown below:

6. Supplementary Activity:
Tasks
Simple Word Filter
1. Create a function that would accept two inputs: a sentence(string), and a list containing bad words that
the user would like to censor but not remove. The function should return the newly filtered
sentence wherein the bad words are replaced with asterisks equal to the length of the censored
word.

2. Given a certain Physics problem create a function(projectilemotion_solver) that would take in the
following inputs below and return the needed information when the function is called. Name the
program containing the function projectilemotion.py then create another program main_program.py
and import projectilemotion.py
“A long jumper leaves the ground at an angle of 20.0° above the horizontal and at a speed of 11.0 m/s.

(a) How far does he jump in the horizontal direction?
(b)What is the maximum height reached?
Given a projectile motion problem like this where the angle and speed are given, the range or

𝑣𝑖2𝑠𝑖𝑛2𝜃𝑖
distance travelled in the horizontal direction can be determined by using the formula:
𝑅=
𝑔

𝑣𝑖2𝑠𝑖𝑛2𝜃𝑖
The maximum height can be determined using the formula:
ℎ=
2𝑔
Reference: Serway, Jewet (2019), Physics for Scientists and Engineers 9e
3. Create a quadratic equation solver module that would write the inputs of the user and the
corresponding output into text files.

Questions
1. Why do built-in functions exist?

2. What is the advantages/disadvantages of placing code inside functions vs sequential codes.

3. What is the difference between a function and a module?

4. What is the difference between a module and a package?

7. Conclusion:

8. Assessment Rubric:

You might also like