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 . Parentheses override this: .
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 (), but it concatenates two strings ("1" + "1" = "11"). Mixing types carelessly causes subtle bugs.