Data, variables and types
Programs work with data: names, prices, dates, lists of products. To use data, we store it in variables and the language keeps track of its type.
Variables: labelled boxes
A variable is a name that refers to a value stored in memory.
=means "store this value", not "is equal to".score = score + 10reads: "take score, add 10, and store the result back in score".
Naming conventions
| Language | Convention | Example |
|---|---|---|
| JavaScript, Java, C# | camelCase | totalPrice |
| Python, Ruby, Rust | snake_case | total_price |
| Constants (most languages) | UPPER_SNAKE | MAX_USERS |
Good names describe what the value is: daysUntilDeadline beats d or x2.
The core data types
Almost every language has these:
| Type | Represents | JavaScript | Python |
|---|---|---|---|
| Integer | Whole numbers | 42 (number) |
42 (int) |
| Float | Decimals | 3.14 (number) |
3.14 (float) |
| String | Text | 'hello' |
"hello" |
| Boolean | True / false | true / false |
True / False |
| Nothing | "No value" | null, undefined |
None |
| List / array | An ordered collection | [1, 2, 3] |
[1, 2, 3] |
| Map / dictionary | Key → value pairs | { name: 'Asha' } |
{"name": "Asha"} |
Why types matter
The type decides what operations make sense. + adds numbers but joins strings:
This is a classic difference:
- Python is strongly typed: mixing types raises an error, so you find the bug early.
- JavaScript is weakly typed: it converts types automatically, which can hide bugs. (TypeScript adds type checking to JavaScript for this reason.)
Converting between types
Static vs dynamic typing
| Dynamic typing | Static typing | |
|---|---|---|
| Types are checked… | While the program runs | Before it runs (at compile time) |
| A variable can hold… | Any type, and change type | One declared type |
| Examples | Python, JavaScript, Ruby | Java, C#, Go, Rust, TypeScript |
// Static (TypeScript / Java style): the type is part of the declaration
let age: number = 28;
age = "twenty-eight"; // ❌ compile error, caught before runningPractice
Create variables describing a product: its name (string), price (float), quantity in stock (integer), whether it's on sale (boolean) and a list of tags. Print a sentence using all of them. Then convert the string "19.99" to a number and add it to the price.
1.In Python, what does "5" + 5 do?
2.Which type would you use to store whether a user is logged in?