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.
- Division: Represented by the forward slash (
Expression Decoding Examples:
- Celsius to Fahrenheit Conversion:
- Base mathematical logic evaluates parentheses first: division of by , followed by multiplication by the Celsius temperature variable ().
- Given , the implicit multiplication between parentheses and variables must be decoded into explicit Python syntax using
*: - Translated Python syntax:
(9 / 5) * c.
- Alpha Expression Decoding:
- Mathematical formulation: Alpha equals times times plus squared ().
- Formula representation:
- Python translation:
alpha = 1 * 9 * 06 + 7 ** 2. - Explicit asterisks (
*) must separate , , and , and exponentiation of to the power of requires double asterisks (** 2).
- Celsius to Fahrenheit Conversion:
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)ornow_comes_drive = 15 (1.5)fail due to implicit multiplication. They must be corrected by inserting the multiplication operator:12 * (1.1)and15 * 1.5. - Variable Naming Conventions: Spaces inside variable names are invalid syntax. A variable written as
yearly cutscauses 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., referencingnumber_of_cups_differencewhen onlynumber_cups_week_differenceornumber_cups_day_differencewas 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., ), the variable name
jmust be retained in the formula. Replacing the variable name manually with its numeric literal 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.
- Missing Multiplication Operators: Statements such as
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
weatherconsists of 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.
- General method syntax follows the pattern:
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
.ipynbnotebook 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 datascienceto 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), orCommand(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 (
TrueorFalse) and the output returns a number (e.g., or ), 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.
- Method 1: Scrutinize the Question Requirements:
Word Problem Translation: Comic and Bubble Gum Expenditure Case Study
Scenario Parameters:
- Duration of semester: weeks.
- Comic purchases: comic books per week.
- Comic unit cost:
- Bubble gum purchases: 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 weeks or weekly unit quantities) guarantees an incorrect formula.
- Total comic expenditure is calculated as weekly comic quantity () multiplied by unit cost () multiplied by semester duration ( weeks):
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 (
TrueorFalse), 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 , should be substituted in place of in the code?
- Response: No. The variable should not be manually replaced with ; 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 + Ckeyboard shortcut.
- Response: Use the
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.