CS 150B Comprehensive Study Guide: Computer Science Fundamentals, Python Syntax, and Computing History

Computer Science Vocabulary & Key Terminology

  • Variable: A named container used for storing values in memory.

  • Casting: Converting a value from one data type to another (e.g., converting a string to an integer).

  • Interpreter: Translates human-readable code to machine code line-by-line as the program executes.

  • Operator: Performs operations such as addition or multiplication on values.

  • Function: A reusable block of code, created with the def keyword, that runs when called.

  • Parameter: The placeholder variable listed in a function's definition.

  • Return: Ends a function call and sends a result back to the caller.

  • Name Error (Unassigned Variable Handling): The error raised when a program attempts to use a variable that has not been defined.

Data Representation & Number Systems

  • Data Types and Typing:

    • Evaluating type("5") produces <class 'str'> because "5" is enclosed in double quotation marks, identifying it as a string data type.

    • Floating-point numbers (float) represent real numbers containing decimal points. For example, 3.0 is a float, whereas 3 is an integer, "3.0" is a string, and three is treated as a variable name or identifier.

  • Binary and Decimal Conversions:

    • Decimal to Binary Conversion (371037_{10}):

    • Deconstruct 3737 into powers of 22: 37=32+4+1=(1×25)+(0×24)+(0×23)+(1×22)+(0×21)+(1×20)37 = 32 + 4 + 1 = (1 \times 2^5) + (0 \times 2^4) + (0 \times 2^3) + (1 \times 2^2) + (0 \times 2^1) + (1 \times 2^0).

    • Result in binary representation: 1001012100101_2.

    • Binary to Decimal Conversion (1011012101101_2):

    • Expand binary positions: (1×25)+(0×24)+(1×23)+(1×22)+(0×21)+(1×20)=32+0+8+4+0+1=4510(1 \times 2^5) + (0 \times 2^4) + (1 \times 2^3) + (1 \times 2^2) + (0 \times 2^1) + (1 \times 2^0) = 32 + 0 + 8 + 4 + 0 + 1 = 45_{10}.

    • Bit Capacity:

    • To represent the decimal number 99 in binary (100121001_2), exactly 44 bits are required, as 23≤9<242^3 \le 9 < 2^4.

  • Character Encoding Systems:

    • ASCII (American Standard Code for Information Interchange): Utilizes 77 bits per character, enabling representation of 27=1282^7 = 128 unique characters.

    • Unicode: Developed to replace ASCII due to ASCII's inability to encode global non-English scripts, international characters, and modern symbolic sets such as emoji.

Python Operators, Expressions, and Variable Evaluation

  • Compound Assignment Operators:   

    Compound assignment operators code snippet
    • Given code sequence: python x = 5 x *= 3 x -= 5 x %= 7 print(x) &nbsp;&nbsp;&nbsp;&nbsp;

    • Step 1: Initialize x=5x = 5.

    • Step 2: Multiplication assignment x *= 3 evaluates to x=5×3=15x = 5 \times 3 = 15.

    • Step 3: Subtraction assignment x -= 5 evaluates to x=15−5=10x = 15 - 5 = 10.

    • Step 4: Modulo assignment x %= 7 computes remainder of division 10÷710 \div 7, giving x=10(mod7)=3x = 10 \pmod 7 = 3.

    • Step 5: print(x) outputs 3$.\n\n- **Operator Precedence and Floor Division**:\n - Evaluating the expression `print(2 ** 3 // 3)`:\n - Exponentiation operator (`**`) has higher precedence than integer floor division (`//`).\n - Step 1: Compute 2^3 = 8.\n - Step 2: Compute floor division 8 // 3 = 2.\n - Output printed is 2$.

  • Formatted String Literals (f-strings):   

    Formatted string evaluation snippet
    • Given code sequence: python score = 7 print(f"Next: {score + 3 * 2}") &nbsp;&nbsp;&nbsp;&nbsp;

    • Expression evaluated inside interpolation braces {score + 3 * 2}:

    • Operator precedence performs multiplication before addition: 3 \times 2 = 6$.\n - Addition evaluates as score + 6 = 7 + 6 = 13$.

    • Output printed is Next: 13.

  • Name Resolution and Errors:   

    Undefined variable error snippet
    • Given code sequence: python total = price * 2 print(total) &nbsp;&nbsp;&nbsp;&nbsp;

    • Execution fails on the first line because price has not been assigned a value or defined in scope, raising a NameError.

String Manipulation, Slicing, and Length Determination

  • Substring Slicing Mechanics:

    • Slicing notation string[start:stop] extracts characters starting at index start up to, but not including, index stop.

    • Basic Slicing:     

      String slicing example
    phrase = "data science"
    print(phrase[5:12])
    &nbsp;&nbsp;&nbsp;&nbsp;```
    - Index sequence for `"data science"`:
      - `d`(0), `a`(1), `t`(2), `a`(3), ` `(4), `s`(5), `c`(6), `i`(7), `e`(8), `n`(9), `c`(10), `e`(11).
    - Slice `[5:12]` retrieves characters from index 5 through 11, resulting in `"science"`.
    
    - **Extended Slicing (String Reversal)**:
    &nbsp;&nbsp;&nbsp;&nbsp;![String reversal slicing snippet](https://assets.knowt.com/pdf-flow-prod/bbc8707c-b444-4aa5-a3e6-9259f1a54de0-figures/1.png)
    

    python code = "12345" print(code[::-1])     ```

    • Specifying a step of -1 iterates backwards through the string, producing "54321".

  • Length Evaluation of Sliced Substrings:   

    Substring length calculation snippet
    • Given code sequence: python text = "programming" part = text[3:9] print(len(part)) &nbsp;&nbsp;&nbsp;&nbsp;

    • Slice text[3:9] extracts indices 3 through 8, yielding "grammi".

    • Substring length formula for [start:stop] is stop−start=9−3=6\text{stop} - \text{start} = 9 - 3 = 6.

    • len(part) prints 66.\n\n\n# Functions, Modular Design, and Program Execution Flow\n\n- **Function Definitions and String Operations**:\n  ![Function string indexing and concatenation](https://assets.knowt.com/pdf-flow-prod/bbc8707c-b444-4aa5-a3e6-9259f1a54de0-figures/6.png)\n - Given code sequence:\n ```python\n def initial(name):\n return name[0]\n\n print(initial("Ada") + initial("Babbage"))\n    ```\n - `initial("Ada")` returns index 0: `"A"`.\n - `initial("Babbage")` returns index 0: `"B"`.\n - String concatenation `"A" + "B"` evaluates to `"AB"`.\n\n- **Multi-Function Composition and Execution Hierarchy**:\n  ![Multi-function execution example](https://assets.knowt.com/pdf-flow-prod/bbc8707c-b444-4aa5-a3e6-9259f1a54de0-figures/7.png)\n - Given code sequence:\n ```python\n def get_hours(days):\n return days * 24\n\n def get_pay(hours, rate):\n return hours * rate\n\n def summary(days, rate):\n hours = get_hours(days)\n pay = get_pay(hours, rate)\n return f"{hours} hours = {pay}"\n\n print(summary(2, 15))\n    ```\n - **Execution Tracing**:\n - Function definitions are loaded into memory first.\n - The first function to actively execute is `summary()`, initiated by the outer `print(summary(2, 15))` statement.\n - `summary(2, 15)` calls `get_hours(2)`, returning 2 \times 24 = 48.\n - `summary` assigns `hours = 48` and calls `get_pay(48, 15)`, returning 48 \times 15 = 720$$.

    • summary assigns pay = 720 and returns formatted string "48 hours = $720".

    • Final printed output is 48 hours = $720.

Computing Principles & AI Concepts

  • DRY (Don't Repeat Yourself) Principle:

    • Core software architecture standard discouraging redundancy.

    • Violation: Manually duplicating or copy-pasting an identical 5-line logic sequence across multiple code sections instead of encapsulating that logic within a single, reusable function.

  • Input-Process-Output (IPO) Model:

    • Framework describing basic computational processing steps:

    • Input: Gathering user input (e.g., executing input()).

    • Process: Manipulating, transforming, or combining data (e.g., building a personalized greeting string from an input name).

    • Output: Presenting transformed results to the user (e.g., executing print()).

  • Artificial Intelligence Concepts and Ethics:

    • Turing Test: Conceptual benchmark created by Alan Turing to evaluate whether a machine can exhibit conversational behavior indistinguishable from a human. A chatbot convincing human evaluators of its humanity in text communication exemplifies passing the Turing Test.

    • Skill Acquisition and Tool Dependency: Complete reliance on automated AI tools to solve programming exercises deprives students of active problem-solving practice necessary to build underlying conceptual models and coding proficiency.

Computing Pioneers & Historical Foundations

  • Ada Lovelace:

    • Authored the world's first computer algorithm intended for machine implementation.

    • Recognized as the first computer programmer.

  • Charles Babbage:

    • Conceptualized and designed the Analytical Engine, laying mechanical hardware foundations for modern computing architecture.

  • Alan Turing:

    • Developed foundational concepts of theoretical computer science and artificial intelligence.

    • Formulated the Turing Test and the theoretical Turing Machine model.

  • Grace Hopper:

    • Developed the first computer compiler, transforming symbolic programming instructions into machine code.

    • Championed machine-independent programming languages, laying the groundwork for English-like syntax in modern programming languages.