Assignment 5

Python Modules and Numpy Operations

Objective

This assignment covers using Python modules (os, argparse, math, and numpy) for essential programming tasks. The tasks are designed to give you hands-on experience with common module functionalities, data processing, and mathematical operations.


Folder Structure

scripts: for storing your Python scripts.

documents: for storing outputs of your scripts as .txt files.

Important: Follow the folder structure exactly as described. This will allow for automated verification of your work.


Tasks

Task 1: Using the os Module

Instructions:

In the scripts folder, create a Python file named os_operations.py.

Write a script that uses the following os functions:

os.getcwd()
os.mkdir('test_dir')
os.listdir('.')
os.rename('test_dir', 'renamed_dir')
os.rmdir('renamed_dir')

The script should print each operation’s result in the following format:

Current working directory: Your current directory
Created directory 'test_dir'
Directory contents: ['test_dir', 'os_operations.py']
Renamed 'test_dir' to 'renamed_dir'
Deleted directory 'renamed_dir'

[Hint!]

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import __ 

# Print the current working directory
print("Current working directory:", os.getcwd())

# Create a directory named 'test_dir' and print a message
os.mkdir('test_dir')
print("Created directory 'test_dir'")

# List files and directories in the current directory and print them
print("Directory contents:", os.______('.'))

# Rename 'test_dir' to 'renamed_dir' and print a message
os.rename('test_dir', 'renamed_dir')
print("Renamed 'test_dir' to 'renamed_dir'")

# Delete the 'renamed_dir' directory
os._____('renamed_dir')
print("Deleted directory 'renamed_dir'")
python3 os_operations.py > ../documents/os_operations_output.txt

Task 2: Using the argparse Module

Instructions:

In the scripts folder, create a Python file named argparse_example.py.

Write a script that:

Accepts three arguments: --name, --age (as an integer), and --city.

Prints the following formatted output:

You are Alice was born in 1994. You are from Istanbul.

[Hint!]

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import _____ 
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--name", type=___, help="Enter your name")
parser.add_argument("-a", "--age", type=___, help="Enter your age")
parser.add_argument("-c", "--city", type=___, help="Enter your city")
args=parser.parse_args()

n=args.name
a=args.age
c=args.city

print("You are "+__+" was born in "+str(2024-___)+". You are from "+___+".")
python3 argparse_example.py --name Alice --age 30 --city Istanbul > ../documents/argparse_output.txt

Task 3: Using the math Module

Instructions:

Prints each result with a label, formatted as follows:

Square Root of 25: 5
Factorial of 5: 120
90 degrees in radians: 1.5707963267948966
π/2 radians in degrees: 90.0
Log of 100 (base 10): 2.0

[Hint!]

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import math

# Square root of 25
sqrt_result = ________
print(f"Square Root of 25: {sqrt_result}")

# Factorial of 5
factorial_result = ________
print(f"Factorial of 5: {factorial_result}")

# 90 degrees in radians
radians_result = ________
print(f"90 degrees in radians: {radians_result}")

# π/2 radians in degrees
degrees_result = math.degrees(math.pi / 2)
print(f"π/2 radians in degrees: {degrees_result}")

# Log of 100 (base 10)
log_result = math.log(100, 10) 
print(f"Log of 100 (base 10): ________")
python3 math_operations.py > ../documents/math_operations_output.txt

Check your output:

Open documents/math_operations_output.txt and verify that it contains each output line as specified above.


Task 4: Basic Numpy Operations

Instructions:

Addition: [5 7 9]
Dot Product: 32
Element-wise Multiplication: [ 4 10 18]

[Hint!]

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np

# Create two arrays
A1 = np.array([1, 2, 3]) 
A2 = np.array([4, 5, 6]) 

# Perform addition
addition_result = A1 + A2
print(f"Addition: {addition_result}")

# Perform dot product
dot_product_result = np.dot(A1, A2) 
print(f"Dot Product: {dot_product_result}")

# Perform element-wise multiplication
element_wise_multiplication_result = A1 * A2
print(f"Element-wise Multiplication: {element_wise_multiplication_result}")
python3 scripts/numpy_basics.py > documents/numpy_basics_output.txt

Check your output: Open documents/numpy_basics_output.txt and verify that it contains each output line as specified above.


Task 5: Advanced Numpy Array Operations

Instructions:

Prints each result with a label, formatted as follows:

Original Matrix:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
------------------------------------------------------------
Element in 2nd row, 3rd column: 6
------------------------------------------------------------
First two rows, last two columns:
 [[2 3]
 [5 6]]
------------------------------------------------------------
Diagonal Elements: [1 5 9]
------------------------------------------------------------

[Hint!]

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np

# Create a 3x3 matrix with values from 1 to 9
M = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(f"Original Matrix:\n {M}")

print("------------------------------------------------------------")

# Extract specific elements and submatrices
element = M[__, __]
submatrix = M[__, __]
diagonal = np.diag(M)

print(f"Element in 2nd row, 3rd column: {element}")

print("------------------------------------------------------------")

print(f"First two rows, last two columns:\n {submatrix}")

print("------------------------------------------------------------")

print(f"Diagonal Elements: {diagonal}")

print("------------------------------------------------------------")
python3 numpy_advanced.py > ../documents/numpy_advanced_output.txt

Check your output: Open documents/numpy_advanced_output.txt and verify that it contains each output line as specified above.


Final Steps

# Assignment 5: Python Modules and Numpy Operations

## Objective

This assignment focuses on learning Python modules and fundamental operations in `numpy`, including file system operations, command-line argument parsing, mathematical functions, and matrix manipulations. The project is divided into five tasks, each with scripts and documentation.

There are four components (3 `folders` and 1 `.md` file) here:

1. **`scripts`**

This folder contains 6 Python scripts:

   - `os_operations.py`: Demonstrates file system operations using the `os` module.
   - `argparse_example.py`: Parses command-line arguments using `argparse`.
   - `math_operations.py`: Performs mathematical operations using the `math` module.
   - `numpy_basics.py`: Basic `numpy` operations including addition, dot product, and element-wise multiplication.
   - `numpy_advanced.py`: Advanced `numpy` operations, including reshaping, stacking arrays, and extracting specific elements.

2. **`documents`**

This folder contains output files for each script:

   - `os_operations_output.txt`: Output of `os_operations.py`.
   - `argparse_output.txt`: Output of `argparse_example.py`.
   - `math_operations_output.txt`: Output of `math_operations.py`.
   - `numpy_basics_output.txt`: Output of `numpy_basics.py`.
   - `numpy_advanced_output.txt`: Output of `numpy_advanced.py`.

3. **`README.md`**

   This `README.md` file provides an overview of the Assignment 5 and its components.

Save and exit the README.md file. (:wq)

Submit your Assignment 5:

Before running the submitAssignment.sh file, make sure you are in the "Assignment 5" folder. Running this script in any other folder or subfolder may result in missing files being uploaded. When executed in the correct folder, all files in the "Assignment 5" directory will be included in the submission.

submitAssignment.sh