# Variables, Expressions, and Types

Computer Science I · Programming I · https://tryals.app/learn/computer-science-i/variables-expressions-and-types

## Naming Values and Expressions

A **variable** is a named box holding a value your program can read and change. **Assignment** puts a value into a variable, written as `x = 5`, read as "let x become 5." Assignment is directional: the right side evaluates first, then stores into the left.

An **expression** is any combination of values, variables, and operators evaluating to a single value. **Operator precedence** dictates that multiplication binds tighter than addition, so $3 + 4 \times 2 = 11$. Parentheses override this: $(3 + 4) \times 2 = 14$.

**Common pitfall:** Never read `x = x + 1` as a mathematical equation. It is a command: read current x, add 1, and store the result back into x.

## Types and Operations

Every value has a **type** dictating what it holds and which operations are valid. Operations depend heavily on types, especially with overloaded operators like `+`.

| Type | Holds | Example |
| --- | --- | --- |
| Integer (int) | Whole numbers | `42`, `-7` |
| Float | Numbers with decimals | `3.14` |
| Boolean (bool) | `true` or `false` | `true` |
| String | Text | `"hello"` |

**Distinction:** The `+` operator adds two integers ($1 + 1 = 2$), but it _concatenates_ two strings (`"1" + "1" = "11"`). Mixing types carelessly causes subtle bugs.

## Practice questions

4 of this lesson's 10 practice questions, with answers. The full set is in the app.

### 1. The operator + adds two numbers but joins two strings. Sort each expression by what it evaluates to.

**Answer:**

- Evaluates to a number: 3 + 4, 10 - 3, 2 * 5
- Evaluates to a string: "3" + "4", "ab" + "cd", "1" + "1"

**Why:** With numeric operands + adds; with string operands it concatenates. So "3" + "4" is "34" and "1" + "1" is "11", not 7 or 2. The operator is the same symbol; the operand types decide what it means, mixing them carelessly is a frequent source of bugs.

Page: https://tryals.app/practice/computer-science-i/variables-expressions-and-types/the-operator-adds-two-numbers-but-joins-two-strings-sort-each

### 2. In Python, `a = [1,2,3]` then `b = a` then `b.append(4)` leaves `a` with three elements.

**Answer:** False

**Why:** False. `b = a` binds a second name to the same list, so `a` has four elements afterwards. Only an explicit copy — `a[:]`, `list(a)`, `copy.deepcopy` for nested data — makes an independent object. This is the single most common source of surprise in Python and the reason `is` differs from `==`.

Page: https://tryals.app/practice/computer-science-i/variables-expressions-and-types/in-python-a-1-2-3-then-b-a-then-b-append-4-leaves-a-with

### 3. In Python, `a = [1,2,3]; b = a; b.append(4)` leaves `a` with four elements, but `a = 5; b = a; b += 1` leaves `a` at 5. Why do the two behave differently?

A. Because lists are global by default and integers are local
B. Because append mutates the shared object while += rebinds a local name
C. Because the assignment operator behaves differently for container types
D. Because integers are passed by value and lists by reference

**Answer:** B. Because append mutates the shared object while += rebinds a local name

**Why:** The mechanism is identical, a reference is copied both times, and the objects differ: lists are mutable, integers are not. Rebinding touches your name only; mutating touches the object everyone shares. Python calls this call-by-sharing, and confusing it with by-value or by-reference is where the surprise comes from.

Page: https://tryals.app/practice/computer-science-i/variables-expressions-and-types/in-python-a-1-2-3-b-a-b-append-4-leaves-a-with-four

### 4. Order what the machine does when it runs the single line x = x + 1, given that x currently holds 7.

**Answer:**

1. Read the current value of x, which is 7
2. Add 1 to that value, giving 8
3. Store 8 back into x, overwriting the old value
4. From now on the name x refers to 8

**Why:** Read, compute, store, then the name carries the new value. Because the read happens before the write, x = x + 1 is a command and not a false equation, the very point this lesson insists on.

Page: https://tryals.app/practice/computer-science-i/variables-expressions-and-types/order-what-the-machine-does-when-it-runs-the-single-line-x-x-1
