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 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 / 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.