Programming Notes 2
Programming Notes 2
Example:
```bash
Detailed Explanation: random Module conda install package_name
The random module is useful for generating random numbers: ```
Example:
- To install numpy:
```python
```bash
import random
pip install numpy
my_list = [1, 2, 3, 4, 5]
```
random.shuffle(my_list)
- Importing numpy:
print(my_list) # Output: Shuffled list
```python
print(random.randint(10, 20)) # Output: Random integer between 10 and 20
import numpy as np
```
```
numpy arrays are collections of elements of the same type in n dimensions. They
improve performance over traditional
Python lists by storing data more efficiently.
1. External Packages
Example:
Packages are collections of modules that provide tools for performing complex ```python
programming tasks. Python has an extensive set of external packages that can be used import numpy as np
for tasks such as data analysis, machine learning, and scientific computing. These array_2d = np.array([[1, 2], [3, 4]])
packages are typically found in the Python Package Index (PyPI) and can be installed print(array_2d)
using package managers like pip or conda. ```
2. Python Package Repository and Managers
5. ndarray Class
To manage external packages, Python uses package managers like pip and conda.
The ndarray class is used to represent numpy arrays. Some of its attributes include:
- **pip**: The most common Python package manager used to install and manage
packages from PyPI. - **ndim**: Number of dimensions (axes).
Example: - **shape**: Size of the array in each dimension.
```bash - **size**: Total number of elements.
pip install package_name --user - **dtype**: Data type of the elements.
``` - **itemsize**: Size of each element in bytes.
- **conda**: A modern package manager that can install and manage packages across Example:
```python series_example = pd.Series([1, 2, 3, 4])
numpy_array = np.array([1, 2, 3, 4]) dataframe_example = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
print(numpy_array.shape) # Output: (4,) ```
```
9. Exploring DataFrames
6. Creating numpy Arrays
Once a DataFrame is created, it can be explored using various functions:
There are several ways to create numpy arrays:
- **head()**: View the first few rows.
- **np.array()**: Converts a Python list into an array. - **tail()**: View the last few rows.
- **np.zeros()**: Creates an array filled with zeros. - **describe()**: Get summary statistics.
- **np.ones()**: Creates an array filled with ones. - **columns**: List the column names.
- **np.arange()**: Creates an array with a range of values.
- **np.linspace()**: Creates an array with evenly spaced values. Example:
```python
Example: df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
```python print(df.head())
array_zeros = np.zeros((2, 3)) print(df.describe())
array_range = np.arange(0, 10, 2) ```
```
Function:
4. Data Type Conversion - concat(): joins DataFrames along a particular axis
Data types are often converted for compatibility using the .astype() function. - merge(): SQL-style joins on common columns
Function: the_df[col].str.function()
5. Mapping Functions - Functions: len(), lower(), upper(), replace()
Apply functions row-wise, column-wise, or element-wise to modify data.
Example:
Functions: the_df["Name"] = the_df["Name"].str.upper() # converts names to uppercase
- apply(): row/column-wise
- applymap(): element-wise across DataFrame
- map(): element-wise across Series 10. Time Series Data
Time-based data is handled with the datetime64 data type and accessor functions.
Examples:
the_df.apply(lambda x: x * 2) # doubles each element row/column-wise Function:
the_df['Col'].map(lambda x: x.lower()) # converts strings to lowercase in a specific - pd.to_datetime(): converts strings to datetime objects
column - dt accessor: extracts components (e.g., year, month, day)
Example:
6. Renaming and Filtering Columns the_df["date"] = pd.to_datetime(the_df["date"]) # converts string to datetime
Renaming and filtering are useful for clarity and data reduction. the_df["year"] = the_df["date"].dt.year # extracts the year component
Example:
the_df = the_df.rename(columns={"old_col": "new_col"}) # renames a column
the_df = the_df[["col1", "col2"]] # filters the DataFrame to keep only certain columns
Week 10: Detailed Code for Data
fig = plt.figure(figsize=(3.5, 2.5)) # Create figure
ax = fig.subplots(2, 1, sharex=True) # Create two subplots sharing the x-axis
# Add legend
fig = plt.figure(figsize=(5, 5))
ax.legend()
ax = fig.subplots()
plt.tight_layout()
7. Pandas Plotting Example
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
5. Pie Chart
# Create a DataFrame
import matplotlib.pyplot as plt df = pd.DataFrame({
'length': [1.5, 0.5, 1.2, 0.9, 3],
x = [2, 4, 10, 15] 'width': [0.7, 0.2, 0.15, 0.2, 1.1]
labels = ["x1", "x2", "x3", "x4"] }, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])
import pandas as pd
plt.tight_layout()
import matplotlib.pyplot as plt
plt.show()
# Create a Series
6. Histogram srs = pd.Series([1, 2, 3, 3])
3. File Object Methods Explanation: This example demonstrates writing to a file. Escape sequences like '\n' are
Python provides several methods to operate on files: used for newlines, and '\t' for tabs.
• the_ptr = open(filename, 'mode'): Opens the file with the specified mode (read, write,
etc.). 7. Binary Files – Access Modes
Binary files are accessed in the same way as text files, but with 'b' added to the mode:
• the_ptr.read(size): Reads a specified number of bytes from the file.
• 'rb': Opens a binary file for reading.
• the_ptr.readline(): Reads the next line from the file.
• 'wb': Opens a binary file for writing (overwriting content).
• the_ptr.readlines(): Reads the entire file line-by-line as a list.
• 'ab': Opens a binary file for appending.
• the_ptr.write(line_str): Writes the string to the file.
Explanation: This code converts a JSON string into a Python dictionary using
11. CSV Writing Example `json.loads()`. The dictionary is then modified and written back to a JSON file using
import csv `json.dump()`.
Explanation: This example reads an Excel file into a Pandas DataFrame using
`pandas.read_excel()` and writes it back to another Excel file using `to_excel()`. 2. Key Features of OOP
OOP introduces several key features that make it a powerful paradigm for complex
problem-solving:
16. CSV DictReader Example
The `csv.DictReader` function reads a CSV file and maps each row into a Python • **Encapsulation**: Hiding internal details by exposing only a public interface
dictionary where the keys are taken from the first row (header). This is especially useful (methods) for interacting with an object's data (attributes).
when working with CSV files that have column headers, allowing easy access to values • **Inheritance**: Allows a new class (subclass) to inherit properties and behaviors
by their field names. from an existing class (superclass). This promotes code reuse and reduces
redundancy.
import csv
• **Polymorphism**: The ability of an object to take on different forms, either by
method overloading (same method name with different parameters) or subclassing
# Open the CSV file in read mode
(different implementation in child classes).
file_ptr = open("example.csv", "r")
csv_reader = csv.DictReader(file_ptr) • **Abstraction**: Simplifies complex logic by hiding unnecessary details and showing
only essential features.
# Iterate over each row and print it as a dictionary
for each_row in csv_reader:
print(each_row) 3. Creating a Class
A class serves as a blueprint for creating objects. It defines the structure (attributes and
file_ptr.close() methods) that will be shared by all objects created from that class.
Example:
Explanation: In this example, the `csv.DictReader()` reads the CSV file row by row. Each class Pet:
row is returned as an OrderedDict, where the keys correspond to the column headers. """A simple class to represent a pet."""
The `for` loop iterates over each row, allowing you to access the data by field names
(headers). This method is useful for structured CSV files where each column represents def __init__(self, species, breed):
a specific attribute, such as 'Name', 'Age', and 'Score'. self.species = species
self.breed = breed
Key points:
• The first row of the CSV file must contain the headers (field names). Explanation: In this example, we define a `Pet` class with two attributes: `species` and
• The values in the dictionary can be accessed by their respective header names (e.g., `breed`. The `__init__` method is called when an object is instantiated and is used to
`each_row['Name']`). initialize the object's attributes.
• This method simplifies accessing specific fields in CSV rows without needing to
manage index positions manually.
4. Instantiating an Object
Week 12-Oriented Programming Once a class is defined, you can create instances (objects) of that class by calling it like a
function.
Example:
1. Introduction to Object Orientation my_pet = Pet("Dog", "Pug")
Object-oriented programming (OOP) is a programming paradigm that focuses on
modeling real-world entities as 'objects'. Each object is an instance of a class, which
Explanation: Here, `my_pet` is an instance of the `Pet` class, and it represents a dog of the
breed 'Pug'.
5. Object Methods def greeting():
Methods are functions defined within a class that operate on instances of the class. They return "Hello, world!"
can access and modify an object's attributes.
Example: Explanation: The `greeting()` method is defined as a static method using the
class Student: `@staticmethod` decorator. It does not access any instance-specific data.
def __init__(self, first_name, last_name, student_number):
self.first_name = first_name
9. Inheritance
self.last_name = last_name
Inheritance allows a class to inherit attributes and methods from another class. This
self.student_number = student_number
promotes code reuse by creating specialized subclasses from more general classes.
def full_name(self): Example:
return f"{self.first_name} {self.last_name}" class Programmer(Student):
def __init__(self, first_name, last_name, student_number, language):
super().__init__(first_name, last_name, student_number)
Explanation: In this example, the `full_name()` method combines the `first_name` and
self.language = language
`last_name` attributes and returns the full name of the student.
def __str__(self):
6. Accessor Methods (Getters) return f"{self.first_name} {self.last_name} ({self.language})"
Accessor methods, or getters, are used to retrieve the value of an attribute while
maintaining encapsulation. Explanation: In this example, `Programmer` is a subclass of `Student`. It inherits the
Example: attributes and methods from `Student` and adds a new attribute, `language`.
def get_first_name(self):
return self.first_name
10. Aggregation of Classes
Aggregation occurs when an object of one class contains objects from another class as
Explanation: This accessor method returns the value of the `first_name` attribute. part of its attributes.
Example:
7. Mutator Methods (Setters) class Lecture:
Mutator methods, or setters, are used to modify the value of an attribute. They help def __init__(self, students, lecture_name):
enforce encapsulation by controlling how attributes are modified. self.students = students
self.lecture_name = lecture_name
Example:
def set_first_name(self, first_name): def number_of_students(self):
self.first_name = first_name return len(self.students)
Explanation: This mutator method allows the `first_name` attribute to be updated. Explanation: In this example, the `Lecture` class contains a list of `Student` objects as one
of its attributes, demonstrating aggregation.
8. Static Methods
Static methods do not operate on class attributes or require an instance of the class.
They are typically used as utility or helper functions within a class.
Example:
@staticmethod