Last saved 9 days ago
AH

Lecture 03/04: CSC1302

robot
knowt logo

Lecture 03/04: CSC1302

Python Programming Overview

  • Discussion revolves around writing and executing Python scripts.

  • Common IDEs:

    • PyCharm

    • Visual Studio Code

    • Command Line Interface (CLI)

Running Python Scripts

  • 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.

Command Line Execution

  • 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

Command Line Arguments

  • 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

Using 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)

Benefits of Command Line Arguments

  • Flexible input without a GUI.

  • Automation and testing advantages.

Writing a Python Program

  • 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))

File Types

  • 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.

Data Structures Overview

  • Introduction to data structures with focus on trees.

Trees as Hierarchical Data Structures

  • Tree Definition:

    • Root: Topmost node (only one)

    • Branches: Connect nodes in a hierarchy

    • Leaves: Nodes without children

Components of Trees

  • Root, children, parent nodes, leaf nodes, and edges.

  • Height of a Tree: Number of edges from root to the farthest leaf.

Types of Trees

  • General Tree: Nodes can have any number of children.

  • Binary Tree: Each node has at most two children.

Classifying Binary Trees

  • 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.

Example Problems

  • Homework: Identify types of binary trees based on given structures.

Implementing Trees in Python

  • 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.