Algorithms and Programming Python 7
Switching to Google Colab
Starting from this session, we will move away from the Programiz Online Python Compiler (used in Meetings 1–6) and use Google Colab instead. This change is necessary because this session covers File Handling, and Programiz — like most simple online compilers — doesn’t properly support persistent file read/write operations, making it impossible to properly practice opening, writing, reading, and appending files.
Google Colab (Colaboratory) is a free, cloud-based Jupyter Notebook environment provided by Google that runs Python directly in your browser, with no installation required. It also gives us the ability to save files to Google Drive, which is a much closer experience to real-world data science and Python development work — and it’s the same tool we’ll use later in this module for the Pandas/Data Science section.
Getting Started with Google Colab
- Open Colab: Go to colab.research.google.com and sign in with a Google account.
- Create a New Notebook: Click File > New notebook. A notebook is made up of “cells” that you can run one at a time.
- Run a Cell: Type Python code into a cell and press
Shift + Enter(or click the ▶ play button) to run it. The output appears directly below the cell. - Add More Cells: Use the + Code button to add new cells, so you can build a program step by step and re-run only the parts you need.
- File Handling in Colab: By default, files you create (e.g.,
Test.txt) are saved to a temporary session storage, which is enough for practicing file handling in this module. If you want files to persist permanently, you can mount your Google Drive with:After mounting, you can read/write files directly inside your Google Drive folder, e.g.1
2from google.colab import drive
drive.mount('/content/drive')open('/content/drive/MyDrive/Test.txt', 'w'). - Uploading Files: To upload a file from your computer for practice, use the folder icon on the left sidebar, or run:
1
2from google.colab import files
uploaded = files.upload()
From this point forward in the course, all exercises in this session (and the Data Science section that follows) should be done in Google Colab.
File Handling
File Handling refers to the management of files in programming. In the context of file handling, we can open, close, read, write, append, and copy files. Python treats different file types, whether binary or text, appropriately. To implement this in Python, the syntax is: file = open(‘filename’, ‘mode’). Python provides three types of modes for opening files:
1 | "r", for reading. |
Don’t forget that after performing operations on a file, you should call the method to close it. The method call is done using file.close().
Here’s a simple example:
1 | # Write File |
The with Statement
The examples above work, but manually calling open() and close() is considered outdated and risky in modern Python. If an error occurs between open() and close(), the close() line never runs, and the file may stay locked or unsaved (data loss risk). This is why professional Python code today always uses the with statement (a context manager), which automatically closes the file even if an error happens inside the block:
1 | # Write File (modern, preferred style) |
From this point forward, always prefer with open(...) as file: over manual open()/close() calls — this is the pattern you will see in real-world Python codebases and is what interviewers and code reviewers expect.
Exception Handling with Try/Except
This is one of the most important topics in any Python course, and it was missing until now. Programs constantly deal with things that can go wrong: a file that doesn’t exist, a user typing text instead of a number, dividing by zero, and so on. Instead of letting the program crash, Python provides try/except to catch and handle these errors gracefully.
1 | try: |
Applied to file handling specifically — a very common real-world scenario is trying to read a file that might not exist:
1 | try: |
You can also raise your own errors intentionally using raise, which is useful for validating input inside your own functions:
1 | def set_age(age: int): |
Key exception types you will encounter often: ValueError (wrong value type/format), TypeError (wrong data type used in an operation), ZeroDivisionError, FileNotFoundError, IndexError (invalid list index), and KeyError (invalid dictionary key).
Python Modules & PIP
Modules
A module is simply a .py file containing functions, classes, or variables that can be imported and reused in another file — this is how larger programs stay organized instead of living in one giant file. You’ve already used built-in modules like math; you can also create your own.
my_module.py:1
2
3
4def greet(name: str) -> str:
return f"Hello, {name}!"
PI = 3.14159
main.py (in the same folder):1
2
3
4
5
6
7
8import my_module
print(my_module.greet("Azhar"))
print(my_module.PI)
# Alternative import styles
from my_module import greet
print(greet("World"))
PIP & Virtual Environments
PIP is Python’s official package manager, used to install third-party libraries (like pandas, which you’ll use later in this module) that don’t come built into Python:
1 | pip install pandas |
A virtual environment is an isolated Python installation for a single project, so that each project’s dependencies don’t conflict with each other — this is standard practice in every real-world Python project:
1 | python -m venv myenv # create a virtual environment named "myenv" |
Introduction to Data Science
Data Science
Data Science is a discipline that focuses on studying data, particularly quantitative data, whether structured or unstructured. Many programming languages support data processing, including R, Python, SQL, and JavaScript, among others. Python is one of the languages that supports data processing and provides libraries for this purpose, one of which is the Pandas library. For data processing, Python recommends using the Integrated Development Environment (IDE) Jupyter Notebook.
Alongside Pandas, two other libraries are considered essential in the current data science industry and are worth knowing about at this stage:
- NumPy: the foundational library for fast numerical computing in Python; Pandas itself is built on top of NumPy.
- Matplotlib / Seaborn: the standard libraries for data visualization (charts, graphs) in Python.
Install them with: pip install pandas numpy matplotlib
Pandas Data Frame
The basic data structure in Pandas is called a DataFrame, which is a collection of ordered columns with names and types. It resembles a table similar to a database, where a single row represents a single example, and the columns represent specific attributes. A Pandas DataFrame can also be considered a dictionary of lists because its structure resembles a list with key-value identification for each piece of data.
Here’s a basic example of file handling in data processing:
1 | import pandas as pd |
Exercises
File Handling Exercise 1
Create a text file named Biodata.txt using file handling implementation with the following user input:
Name: Your Name
Age: Your Age
Address: Your Address
Email: Your Email
Include both write and read methods within functions to make the program more structured.
File Handling Exercise 2
Create a program that can create a file, read a file, and append text to a file, where the file name is obtained from user input and the data to be added to the file is also provided by user input. Implement the program into functions and also incorporate branching and looping so that the program continues running until the user chooses the “close” option.
Exception Handling Exercise
Modify your File Handling Exercise 2 program above so that it uses try/except/finally to handle the case where the user enters an invalid filename or the file cannot be opened, instead of letting the program crash.
Data Science Exercise 1
Create a program that reads a DataFrame from a CSV file, with at least 10 country data entries, and displays the Mean (Average) and Standard Deviation.
Data Science Exercise 2
Create a program that writes a CSV file from dummy data: use a dictionary, convert it to a DataFrame, and then write it to a CSV file using the pandas library.


