Introduction to Python Computational Problem Solving, Syntax Debugging, and Google Colab

Fundamental Principles of Computational Problem Solving

  • Role of the Computer vs. User:

    • The user must allow the computer to solve the computational problem rather than computing and manually writing the numerical answer.
    • Writing down the final numeric answer directly is incorrect; the user must write only the equation or expression.
    • Computational logic must rely strictly on information explicitly provided. Making up values or variables is prohibited.
    • The computer possesses no baseline knowledge or assumptions unless explicitly declared or previously defined in prior execution steps.
  • Sequential Dependency and State Preservation:

    • Computational tasks build sequentially upon one another.
    • Defining variables and equations correctly in an initial step (such as Question 1) establishes the environment state required for subsequent questions.
    • If the computer is properly informed in earlier steps, subsequent dependent steps (such as Question 2) execute correctly.
    • Failing to define necessary variables or structures upstream causes failure downstream.

Python Mathematical Syntax and Arithmetic Operators

  • Operator Translations:

    • Division: Represented by the forward slash (/).
    • Multiplication: Represented by an asterisk or star (*). Implicit mathematical multiplication through juxtaposition (e.g., writing numbers next to parentheses without an operator) does not exist in Python; the asterisk must always be explicitly stated.
    • Exponentiation / Powers: Represented by two consecutive asterisks or stars (**).
    • Precedence and Grouping: Parentheses ( ) determine the order of evaluation, ensuring specific operations are evaluated first.
  • Expression Decoding Examples:

    • Celsius to Fahrenheit Conversion:
      • Base mathematical logic evaluates parentheses first: division of 99 by 55, followed by multiplication by the Celsius temperature variable (cc).
      • Given c=17c = 17, the implicit multiplication between parentheses and variables must be decoded into explicit Python syntax using *:
      • (95)×c\left(\frac{9}{5}\right) \times c
      • Translated Python syntax: (9 / 5) * c.
    • Alpha Expression Decoding:
      • Mathematical formulation: Alpha equals 11 times 99 times 0606 plus 77 squared (727^2).
      • Formula representation: alpha=1×9×06+72\text{alpha} = 1 \times 9 \times 06 + 7^2
      • Python translation: alpha = 1 * 9 * 06 + 7 ** 2.
      • Explicit asterisks (*) must separate 11, 99, and 0606, and exponentiation of 77 to the power of 22 requires double asterisks (** 2).

Code Debugging, Syntax Rules, and Table Manipulation

  • Syntax Debugging and Error Indicators:

    • Development environments flag syntax errors visually with red coloring and red squiggly underlines underneath problematic code.
    • Debugging procedure requires examining erroneous code, copying and pasting it into the cell, and methodically repairing syntax errors one by one.
  • Specific Syntax Errors and Corrections:

    • Missing Multiplication Operators: Statements such as number_of_cups += 12 (1.1) or now_comes_drive = 15 (1.5) fail due to implicit multiplication. They must be corrected by inserting the multiplication operator: 12 * (1.1) and 15 * 1.5.
    • Variable Naming Conventions: Spaces inside variable names are invalid syntax. A variable written as yearly cuts causes an error and must be joined using underscores (yearly_cuts) or removing whitespace.
    • Variable Definition Errors (NameError): Attempting to compute operations with undefined variables (e.g., referencing number_of_cups_difference when only number_cups_week_difference or number_cups_day_difference was defined) results in execution failure. Names must match defined identifiers exactly.
    • Output Display: If code executes but outputs no visible value, the variable of interest must be explicitly evaluated or re-entered on the final line of the code cell.
    • Variable Substitution Rule: When a variable is defined by the environment (e.g., j=26j = 26), the variable name j must be retained in the formula. Replacing the variable name manually with its numeric literal 2626 is incorrect.
    • String Enclosure: Column names, table references, and string parameters must have matching quotation marks or apostrophes at both the beginning and the end inside parentheses.
  • Table Operations Syntax:

    • General method syntax follows the pattern: table_name.operation(...).
    • .select(): Specifies and qualifies the columns or attributes to extract from the table.
    • .sort(): Sorts table data according to the parameter passed inside the parentheses (e.g., alphabetical order or reverse alphabetical order).
    • .where(): Filters rows based on a specified condition (e.g., filtering a student table specifically for rows where student hair is natural or silk pressed).
    • Weather Table Example: A dataset named weather consists of 88 rows corresponding to days from June 21 through June 30, containing Celsius temperature readings and student attendance metrics. Operations on this table are chained via dot notation: weather. followed by the required manipulation.

Google Colab Environment and File Operations

  • Platform Accessibility:

    • Google Colaboratory operates across multiple hardware devices, including desktop computers, laptops, tablets, and smartphones.
  • Notebook and File Setup:

    • Assignment notebooks and datasets are distributed via Canvas inbox.
    • Both the assignment notebook and data files must be downloaded locally to the downloads directory.
    • Google Colab requires uploading the .ipynb notebook file to launch the session.
  • Cell Execution and Status Indicators:

    • Execution is triggered by clicking the play button located on the left-hand side of each code cell.
    • Play Button Function: The play button tests code syntax and executes the statements; it does not grade mathematical or logical accuracy.
    • Green Checkmark Meaning: A green checkmark confirms that code executed without syntax errors or runtime exceptions. It does not signify that the computed answer is mathematically correct.
    • Altering parentheses or logic changes the calculation outcome while still returning a valid execution checkmark.
    • System Commands: Cells designated with instructions such as "just run this cell" must be executed directly as provided (e.g., running pip install datascience to install required dependencies).
  • Data File Ingestion:

    • Running the initialization cell prompts an interactive file upload interface for datasets (CSV or Excel files).
    • Files are located in the local downloads folder.
    • Files can be ingested via drag-and-drop or browsing.
    • Interface Variations: PC systems display a "Browse" button, whereas macOS systems display a "Choose File" button.
    • Multi-File Selection: If an assignment requires multiple CSV files, select multiple files simultaneously by holding Shift, Control (PC), or Command (Mac).
  • Session Termination and Saving:

    • To prevent loss of completed work, sessions must be saved prior to closing.
    • Navigation path: File -> Save a copy in Drive.
    • Users must verify within Google Drive that the copy has been successfully created and saved.

Verification Strategies and Error Detection

  • Three Verification Methods:
    • Method 1: Scrutinize the Question Requirements:
      • The prompt specifies expected output types, ranges, and formats.
      • If the question asks for a boolean (True or False) and the output returns a number (e.g., 6565 or 345345), the solution is incorrect.
      • If a prompt specifies an absolute value, the output must be strictly positive; any negative result is incorrect.
    • Method 2: Collaborative Peer Review:
      • Compare code implementation, mathematical logic, and outputs with an adjacent peer to identify discrepancies.
    • Method 3: Instructor Verification:
      • Raise a hand to have code verified, directly asking if the logic and output are correct before advancing.

Word Problem Translation: Comic and Bubble Gum Expenditure Case Study

  • Scenario Parameters:

    • Duration of semester: 1515 weeks.
    • Comic purchases: 44 comic books per week.
    • Comic unit cost: 6.756.75
    • Bubble gum purchases: 33 packs per week.
  • Parameter Inclusion Principle:

    • Every numerical value explicitly stated in a word problem must appear within the constructed formula.
    • Omitting given parameters (such as the duration of 1515 weeks or weekly unit quantities) guarantees an incorrect formula.
    • Total comic expenditure is calculated as weekly comic quantity (44) multiplied by unit cost (6.756.75) multiplied by semester duration (1515 weeks):
      • 4×6.75×154 \times 6.75 \times 15
  • Output Type Verification:

    • The problem specifically tasks the user with determining whether expenditure on comics exceeded bubble gum over the semester.
    • Because the question asks a comparative "whether" question, the expected output is a boolean condition (True or False), rather than an aggregate dollar amount.

Questions & Discussion

  • Question / Dialogue: What was the next step after opening Google Drive, and should a personal drive or school drive be used?

    • Response: A personal Google Drive account is used. Navigate to the website, select upload file or upload notebook, and download the assigned files sent via the Canvas inbox.
  • Question / Dialogue: Should parentheses be kept, or should the last zero be removed from the code?

    • Response: The parentheses can be retained. Removing them is optional, but retaining them does not cause an error.
  • Question / Dialogue: If the prompt states that j=26j = 26, should 2626 be substituted in place of jj in the code?

    • Response: No. The variable jj should not be manually replaced with 2626; the code must utilize the assigned variable directly.
  • Question / Dialogue: How do you copy and paste code over when standard shortcuts fail?

    • Response: Use the Control + C keyboard shortcut.
  • Question / Dialogue: Why does code fail to output an answer even when it executes without syntax errors?

    • Response: The final variable must be typed on the very last line of the cell so the notebook environment outputs and displays its evaluated value.
  • Question / Dialogue: What causes an error when formatting table manipulation operations inside parentheses?

    • Response: Ensure that missing strings have appropriate opening and closing apostrophes or quotation marks, and that operations inside the parentheses contain valid syntax without stray decimal points.