Variables & Basic Data Types

Python Fundamentals — Variables & Basic Data Types
Python Fundamentals
Course 1 · Chapter 2 · Variables & Basic Data Types

🔢 Variables & Basic Data Types

Chapter 1 got scripts and the REPL running. This chapter covers how Python actually stores values — no declaration keyword at all, dynamic typing that lets a variable hold any type at any time, the four basic data types, and converting between them with 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

LanguageSyntax
Goage := 30  /  var age int = 30
Kotlinval age = 30  /  var age = 30
Pythonage = 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.

⚠ Convenient, but a Real Source of Subtle Bugs

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") Fails — A Genuine Common Mistake

int("3.14") raises a ValueError, not 3int() 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 / KotlinPython
Type belongs toThe variable, fixed at declarationThe current value, can change
Type mismatch caughtAt compile timeAt runtime (if at all)
Type hintsRequired (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().

→ Solution

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.

→ Solution

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.

→ Solution

🎯 What's Next

Next chapter: Operators & Control Flow — arithmetic/comparison/logical operators, if/elif/else, and the walrus operator.