Python Constants (let), Variables (var) & Type Inference
Swift strongly encourages immutability. Learn when to use let (immutable constant) versus var (mutable variable), how type inference detects types automatically, explicit type annotations, and core primitives (Int, Double, Float, String, Bool).
"A constant (let) is like engraving your name in stone—once carved, it can never be altered. A variable (var) is like writing on a whiteboard with a dry-erase marker."
Deep Dive: How It Works
let vs var: Always default to let unless you specifically know the value must change.
Type Inference: Swift automatically determines variable types (e.g. let score = 100 infers Int).
Explicit Type Annotation: var rating: Double = 4.5 specifies the type explicitly when inference is ambiguous.
Type Safety: Swift will not implicitly convert types (you cannot add Int and Double without explicit Double(intVal) conversion).
Syntax Blueprint
let maxRetries = 3 // Immutable var currentAttempt = 0 // Mutable let timeout: Double = 15.5
Use let for constants and var for variables, optionally adding : Type annotations.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Trying to mutate a value declared with let (e.g. let count = 0; count += 1).Why it happens: let constants are read-only after initialization.
How to fix: Change let to var if the value legitimately needs to mutate over time.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Calculate Circle Area with Constants
Declare a constant let radius: Double = 5.0.
Declare a constant let pi: Double = 3.14159.
Calculate let area = pi * radius * radius.
Print "Area: " followed by area.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.