Variables & Basic Data Types
🔢 Variables & Basic Data Types
type() and casting.
✏️ Declaring Variables — No Keyword Needed
Python has no let, var, or val — just a name, =, and a value:
Declaring a Value: Go vs Kotlin vs Python
| Language | Syntax |
|---|---|
| Go | age := 30 / var age int = 30 |
| Kotlin | val age = 30 / var age = 30 |
| Python | age = 30 |
🔄 Dynamic Typing: Types Belong to Values, Not Variables
This is a genuinely meaningful difference from Go and Kotlin, both already covered — in Python, a variable can be reassigned to a completely different type at any point:
In Kotlin, val age = 30 locks age to Int forever — assigning a String to it later is a compile error. In Python, the type belongs to the value currently stored, not to the variable name itself; the same name can point at an int one moment and a str the next.
Dynamic typing means a typo or a logic mistake that assigns the wrong type to a variable often won't be caught until the program actually runs and fails — unlike Go or Kotlin, where the compiler catches this before the program ever executes. Python's answer to this trade-off is type hints (Course 3's own dedicated chapter) — an opt-in way to declare intended types that tools like mypy can check, without changing Python's actual runtime behavior.
🧱 The Basic Data Types
int
Whole numbers, positive or negative, no fixed size limit (unlike Go's int32/int64).
float
Decimal numbers. Any number written with a decimal point is automatically a float.
str
Text, in single or double quotes — Python treats 'text' and "text" identically.
bool
True or False — capitalized, unlike Go/Kotlin's lowercase true/false.
🔍 type() — Checking a Value's Type
🔁 Type Casting — Converting Between Types
int("3.14") raises a ValueError, not 3 — int() can't parse a decimal point directly from a string. The fix is going through float first: int(float("3.14")) → 3. This trips up nearly everyone the first time they try to convert user input that happens to contain a decimal.
✅ Truthiness — What Counts as False
Every value in Python has an implicit boolean meaning, checked with bool(): 0, 0.0, "" (empty string), None, and empty collections are all falsy; everything else is truthy. This becomes directly useful once if statements arrive in the next chapter.
Static Typing (Go/Kotlin) vs Dynamic Typing (Python)
| Go / Kotlin | Python | |
|---|---|---|
| Type belongs to | The variable, fixed at declaration | The current value, can change |
| Type mismatch caught | At compile time | At runtime (if at all) |
| Type hints | Required (Go) / default (Kotlin) | Optional, opt-in (Course 3) |
💻 Coding Challenges
Challenge 1: Four Types, One Script
Declare one variable of each basic type (int, float, str, bool), then print each one alongside its type using type().
Goal: Get comfortable with all four basic types and confirming them with type().
Challenge 2: The int("3.14") Trap
Write code that safely converts the string "3.99" into an integer, working around the fact that int() can't parse a decimal point directly.
Goal: Practice the float-then-int casting workaround from this chapter's warn-box.
Challenge 3: Dynamic Typing in Action
Write code that assigns an int to a variable, prints its type, then reassigns the same variable name to a str, and prints its type again — proving the type genuinely changed.
Goal: Build a concrete, working example of dynamic typing rather than just reading about it.
🎯 What's Next
Next chapter: Operators & Control Flow — arithmetic/comparison/logical operators, if/elif/else, and the walrus operator.