Introduction to Python: Coding Tasks, Variables, and Expressions

Categories of Coding Tasks and Programming Logic

  • Definition of Coding: Programming is described as writing lines of variously leveled "broken English" to command a computer to perform specific tasks.

  • Three Primary Categories of Code Actions:

    • Assigning or Reassigning Values (Manipulation): Storing data in memory for later use.

      • Example: name = "Tony" — The text string "Tony" is stored in a discrete memory space labeled "name".

    • Background Tasks and Evaluation: Performing operations that do not necessarily store data but execute a process.

      • Example: print(math.sqrt(25)) — The computer calculates the square root of 2525 (which is 55) and outputs the result to the screen.

    • Setting Up Code Chunks: Establishing structures that handle more complex or repetitive tasks.

      • Example: for i in range(5): — This sets up a "for loop" to execute a block of code multiple times.

  • Programming Mindset and Decision-Making:

    • The Big Picture: Constant consideration of the ultimate goal the entire program must accomplish.

    • The Small Picture: Understanding the specific task delegated to a single line or chunk of code.

    • Efficiency vs. Utility: While written code may be inefficient, it is rarely "useless." The engineering process involves constant evaluation of goals and the tools used to achieve them.

Memory Manipulation and Variables

  • Memory Requirements: Electronic devices require memory (space) to store information, similar to saving data in a Google Doc or a physical notebook.

  • Required Information for Storage: To manipulate memory, most programming languages require two pieces of information:

    1. The specific data being stored.

    2. The name designated for that specific memory space.

  • Python’s Role in Memory Management: Python simplifies the storage process by handling the technical details of where in memory to store data and how much space to reserve. It abstracts the need to designate specific memory positions or amounts.

  • The Assignment Operator (==):

    • In Python, the equals sign (==) is the assignment operator, not a statement of equality.

    • It is used to assign a value to a label that points to a memory space.

    • Comparison vs. Assignment: True mathematical equality or comparison uses different symbols (operators); == is strictly for data storage.

  • The Concept of a Variable:

    • A variable is the formal name for the label assigned to a memory space.

    • Analogy: Think of a variable as a labeled jar. The data is placed inside the jar, and the label (the variable name) allows you to find and use that data later.

    • Lack of Automatic Output: Simply assigning a value (e.g., name = "Tony") does not produce a visible output. Python performs the instruction exactly as written (storing the data) and will only show output if an explicit command to do so is given.

Rules and Mechanics of Expressions

  • The Order of Assignment: In Python, order is strictly enforced. The variable name must be on the left-hand side of the assignment operator (==), and the value or expression must be on the right-hand side.

  • Value Persistence and Reassignment:

    • Variables are not permanent storage for a single value.

    • You can switch the value stored in a variable by assigning a new value to the same label.

    • Example Transition:

      1. name = "Tony" (The memory space labeled "name" contains "Tony").

      2. name = "Adolin" (The memory space labeled "name" now contains "Adolin").

    • When reassigned, the old value (e.g., "Tony") is discarded by background processes (garbage collection) because nothing else is claiming it.

  • Defining Expressions:

    • An expression is code that evaluates (simplifies) to a single value.

    • If an expression is left alone on a line without being assigned to a variable, it will evaluate to a value, but that value will immediately disappear because no code "claims" it.

    • Example: total = 1 + 2 — Here, 1+21 + 2 is the expression that evaluates to 33, which is then stored in the variable total.

  • Syntax Constraints:

    • Expressions cannot appear on the left-hand side of an assignment operator.

    • Invalid Syntax Example: 1 + 1 = 2 results in a SyntaxError: cannot assign to expression here. Python interprets the left side as a literal variable name, and since an expression is not a valid name, the code fails.

  • Using Variables within Expressions: Python can retrieve values from existing variables to use in new calculations.

    • Example:

      • a = 10

      • b = 20

      • total = a + b (The value of total becomes 3030).

The print() Function

  • Definition: print is a built-in Python function that allows the user to visually display values on an output terminal.

  • Syntax: The items to be displayed must be placed inside parentheses: print(...).

  • Usage Scenarios:

    • Printing Literals: print("Howdy, world!") outputs the exact text: Howdy, world!

    • Printing Expressions: print(2000 + 25) will first simplify the expression and then output the result: 20252025.

    • Printing Variables: When a variable is passed to the print function, the terminal displays the value currently stored in that variable.

      • Example: python mascot_name = "Miss Reveille" print(mascot_name)             

      • Output: Miss Reveille