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:
name = "Ada" age = 30
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:
x = 5 # x is currently an int print(type(x)) # <class 'int'> x = "hello" # perfectly valid โ x is now a str print(type(x)) # <class 'str'>
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
age = 30 # int โ whole numbers price = 19.99 # float โ decimal numbers name = "Philip" # str โ text is_ready = True # bool โ True or False (capitalized!)
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
print(type(30)) # <class 'int'> print(type(19.99)) # <class 'float'> print(type("hi")) # <class 'str'> print(type(True)) # <class 'bool'>
๐ Type Casting โ Converting Between Types
int("42") # 42 โ str to int str(42) # "42" โ int to str float("3.14") # 3.14 โ str to float int(3.99) # 3 โ float to int (truncates, doesn't round!)
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.