Discussion revolves around writing and executing Python scripts.
Common IDEs:
PyCharm
Visual Studio Code
Command Line Interface (CLI)
Typical execution: Write code and run from the UI.
Not all computers have a GUI (e.g., servers).
Command Line Arguments: Alternative input method for Python scripts.
Example: Execute a Python script from the command line.
Navigate to the directory containing the script:
Command: cd /path/to/directory
Run script: python test.py
Provide additional parameters when running scripts.
Example of running a script with arguments:
Command: python script.py arg1 arg2
Accessing the arguments in the script via:
import sys
Use sys.argv
, where:
sys.argv[0]
: script name
sys.argv[1]
: first argument
sys.argv[2]
: second argument
sys.argv
Arguments are stored as strings, requiring conversion for mathematical operations.
Example usage:
distance = int(sys.argv[1])
time = int(sys.argv[2])
speed = distance / time
print(speed)
Flexible input without a GUI.
Automation and testing advantages.
Task: Create a program that counts lines in a text file.
Usage:
Command: python script.py example.txt
Inside the script, access the text file via sys.argv[1]
.
Open the file and count lines using:
with open(filename, 'r') as f:
lines = f.readlines()
print(len(lines))
Differences between text files and CSV (Comma Separated Values) files.
CSV files:
Read/write using csv
module in Python:
csv.reader
for reading CSV files.
csv.writer
for writing to CSV files.
Introduction to data structures with focus on trees.
Tree Definition:
Root: Topmost node (only one)
Branches: Connect nodes in a hierarchy
Leaves: Nodes without children
Root, children, parent nodes, leaf nodes, and edges.
Height of a Tree: Number of edges from root to the farthest leaf.
General Tree: Nodes can have any number of children.
Binary Tree: Each node has at most two children.
Full Binary Tree: Every node has either zero or two children.
Complete Binary Tree: All levels filled except possibly the last, aligned to the left.
Balanced Binary Tree: The height difference between left and right subtrees is at most one.
Homework: Identify types of binary trees based on given structures.
Binary trees can be represented using arrays.
Array Representation: Root at index 0.
Left child at 2i + 1
and right child at 2i + 2
.
Array Binary Tree Class:
Structure includes methods for setting child nodes and accessing tree data via indices.
Lecture 03/04: CSC1302
Discussion revolves around writing and executing Python scripts.
Common IDEs:
PyCharm
Visual Studio Code
Command Line Interface (CLI)
Typical execution: Write code and run from the UI.
Not all computers have a GUI (e.g., servers).
Command Line Arguments: Alternative input method for Python scripts.
Example: Execute a Python script from the command line.
Navigate to the directory containing the script:
Command: cd /path/to/directory
Run script: python test.py
Provide additional parameters when running scripts.
Example of running a script with arguments:
Command: python script.py arg1 arg2
Accessing the arguments in the script via:
import sys
Use sys.argv
, where:
sys.argv[0]
: script name
sys.argv[1]
: first argument
sys.argv[2]
: second argument
sys.argv
Arguments are stored as strings, requiring conversion for mathematical operations.
Example usage:
distance = int(sys.argv[1])
time = int(sys.argv[2])
speed = distance / time
print(speed)
Flexible input without a GUI.
Automation and testing advantages.
Task: Create a program that counts lines in a text file.
Usage:
Command: python script.py example.txt
Inside the script, access the text file via sys.argv[1]
.
Open the file and count lines using:
with open(filename, 'r') as f:
lines = f.readlines()
print(len(lines))
Differences between text files and CSV (Comma Separated Values) files.
CSV files:
Read/write using csv
module in Python:
csv.reader
for reading CSV files.
csv.writer
for writing to CSV files.
Introduction to data structures with focus on trees.
Tree Definition:
Root: Topmost node (only one)
Branches: Connect nodes in a hierarchy
Leaves: Nodes without children
Root, children, parent nodes, leaf nodes, and edges.
Height of a Tree: Number of edges from root to the farthest leaf.
General Tree: Nodes can have any number of children.
Binary Tree: Each node has at most two children.
Full Binary Tree: Every node has either zero or two children.
Complete Binary Tree: All levels filled except possibly the last, aligned to the left.
Balanced Binary Tree: The height difference between left and right subtrees is at most one.
Homework: Identify types of binary trees based on given structures.
Binary trees can be represented using arrays.
Array Representation: Root at index 0.
Left child at 2i + 1
and right child at 2i + 2
.
Array Binary Tree Class:
Structure includes methods for setting child nodes and accessing tree data via indices.