Lesson 1 of 8

Hello, Swift

Write and run your first Swift program and learn let, var and Swift's basic types. Swift has concise syntax with no semicolons, and it compiles to fast native code.

Swift
// hello.swift — run with: swift hello.swift
print("Hello, World!")

let name = "Ada"        // let = constant (use by default)
var age = 17            // var = mutable
age = 18
// name = "Bob"         // error: 'name' is a let constant

print("I'm \(name), age \(age)")     // string interpolation
print("Next year: \(age + 1)")

Types are inferred but static: let name = "Ada" is a forever. Explicit use a colon — let name: String = "Ada". As in Kotlin and Rust, prefer (let) and reach for var only when mutation is the point.

Swift
let int: Int = 42
let double: Double = 3.14
let bool: Bool = true
let text: String = "hello"

// No implicit conversions — be explicit:
let sum = Double(int) + double

// Multi-line strings:
let poem = """
    Roses are red,
    Swift compiles too.
    """

// Type check and conversion:
type(of: int)               // Int
let parsed = Int("123")     // Int? — might fail! (next lesson)
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.