Skip to main content
    Courses/Swift/Introduction to Swift

    Lesson 1 • Beginner

    Introduction to Swift 🦅

    By the end of this lesson you'll know what Swift is and where it runs, you'll understand print() and comments, and you'll have written and run your very first Swift program.

    What You'll Learn in This Lesson

    • • What Swift is — Apple's modern, fast, safe language for iOS and macOS
    • • Where Swift runs: Xcode, Swift Playgrounds, and free online editors
    • • How to show output with print()
    • • How to leave notes in your code with single- and multi-line comments
    • • How to drop values into text with string interpolation \(name)
    • • How to write, read, and run a complete first Swift program

    1️⃣ What is Swift (and where does it run)?

    Swift is Apple's modern, fast, and safe programming language. Apple introduced it in 2014 to replace the older Objective-C, and it's now how almost every new iPhone, iPad, Mac, Apple Watch, and Apple TV app is built. "Safe" means the language is designed to catch whole classes of mistakes before your program runs, so your apps crash far less often.

    So where do you actually write Swift? You have three common homes for it: Xcode (Apple's free, full code editor on the Mac, used to build real apps), Swift Playgrounds (a friendly app on Mac and iPad for learning and experimenting), and free online editors such as swiftfiddle.com or Replit that run in any browser. For this course, an online editor is the quickest way to start — you don't need a Mac yet.

    2️⃣ Your first program: print() and comments

    Every Swift journey starts with output. The print() function takes whatever you put between its parentheses and shows it in the console. Text goes inside "double quotes"; numbers don't need quotes. Anything after // is a comment — a note for humans that Swift ignores. Read this worked example, then run it and confirm the output matches.

    Worked example: print() and comments
    // This is a comment — Swift ignores everything after the //.
    // Comments are notes for humans; they never run.
    
    /* You can also write a comment that
       spans several lines like this. */
    
    // print(...) shows a line of text in the console (the output panel).
    print("Hello, World!")          // shows: Hello, World!
    print("I'm learning Swift!")    // shows: I'm learning Swift!
    
    // You can print numbers too — no quotes needed for numbers.
    print(2 + 2)                    // shows: 4
    Output
    Hello, World!
    I'm learning Swift!
    4
    This is real code — run it for free atSwiftFiddleor in your own editor.

    Now a slightly bigger first program. A let creates a constant — a named value. The real magic is string interpolation: write \(name) inside a string and Swift swaps in the value of name. It reads far better than sticking pieces of text together by hand.

    Worked example: a friendly first program
    // A first real program: greet a person by name.
    
    // 'let' makes a constant — a named value that never changes.
    let name = "Sam"
    let year = 2014
    
    // \(...) is "string interpolation": Swift drops the value into the text.
    print("Hi \(name)! Welcome to Swift.")     // Hi Sam! Welcome to Swift.
    print("Swift launched in \(year).")        // Swift launched in 2014.
    
    // print can join values with commas — note the automatic spaces.
    print("Two", "plus", "two", "is", 2 + 2)   // Two plus two is 4
    Output
    Hi Sam! Welcome to Swift.
    Swift launched in 2014.
    Two plus two is 4
    This is real code — run it for free atSwiftFiddleor in your own editor.

    3️⃣ Your turn: print and comment

    Time to write some code. The program below is almost finished — just fill in the two blanks marked ___ using the // 👉 hints, then run it and check your output against the expected lines.

    🎯 Your turn: make print() talk
    // 🎯 YOUR TURN — replace each ___ then run it (try SwiftFiddle, link below).
    
    // 1) Print a friendly greeting of your choice
    print(___)                 // 👉 put your text inside "double quotes"
    
    // 2) Print the answer to 10 + 5  (numbers need no quotes)
    print(___)                 // 👉 replace ___ with  10 + 5
    
    // 3) Write a // comment above the next line saying what it does
    print("Comments are free — use them!")
    
    // ✅ Expected output (example):
    //    Hello there!
    //    15
    //    Comments are free — use them!
    Output
    Hello there!
    15
    Comments are free — use them!
    This is real code — run it for free atSwiftFiddleor in your own editor.

    4️⃣ Your turn: interpolate your own details

    One more guided exercise, this time with string interpolation. Fill in the name and age blanks, then run it — the print line already works once your constants exist.

    🎯 Your turn: greet yourself with interpolation
    // 🎯 YOUR TURN — fill in the blanks to greet YOU.
    
    // 1) Make a constant 'name' holding your name (text in "double quotes")
    let name = ___             // 👉 e.g. "Alex"
    
    // 2) Make a constant 'age' holding your age (a whole number, no quotes)
    let age = ___              // 👉 e.g. 21
    
    // This line already works once your constants exist.
    // \(name) and \(age) get replaced by their values.
    print("My name is \(name) and I am \(age).")
    
    // ✅ Expected output (example):
    //    My name is Alex and I am 21.
    Output
    My name is Alex and I am 21.
    This is real code — run it for free atSwiftFiddleor in your own editor.

    Common Errors (and the fix)

    • "Cannot find Xcode" / nothing happens when you click run: Xcode only exists on a Mac and is a big download. If you're on Windows, Linux, or just starting out, skip it — use swiftfiddle.com in your browser instead.
    • Nothing prints because you used return instead of print: return hands a value back inside a function but shows nothing on screen. To see output, use print(...).
    • "Unterminated string literal": you forgot a quote, like print("Hello). Swift text needs a matching pair of "double quotes": print("Hello").
    • "Cannot find 'name' in scope": you used a value before creating it. Declare it first with let name = "Sam", then use \(name).

    Pro Tips

    • 💡 Print often while learning: add a print(...) to check a value any time you're unsure what's happening.
    • 💡 Comment the "why": good comments explain why a line exists, not just what it obviously does.
    • 💡 No semicolons needed: Swift ends a statement at the end of the line, so leave the ; off.

    📋 Quick Reference — Swift Basics

    ConceptSwift Syntax
    Show outputprint("Hello, World!")
    Single-line comment// a note for humans
    Multi-line comment/* spans lines */
    Constant (never changes)let name = "Alice"
    String interpolationprint("Hi \(name)!")
    Print a numberprint(2 + 2)

    Frequently Asked Questions

    Q: Do I need a Mac to learn Swift?

    No. You can write and run Swift entirely in your browser using a free site like SwiftFiddle or Replit — no installation and no Apple device required. You only need macOS and Xcode later, when you want to build and ship actual iOS or macOS apps.

    Q: What is print() actually doing?

    print() writes a line of text to the console — the output area where your program's results appear. It is the simplest way to see what your code is doing, and you will use it constantly while learning to check that values are what you expect.

    Q: What does \(name) mean inside a string?

    That is string interpolation. Swift replaces \(name) with the current value of name. So if name is "Sam", then print("Hi \(name)") prints "Hi Sam". It is cleaner than gluing pieces of text together with the + sign.

    Q: Why does Swift not use semicolons at the end of lines?

    Swift treats the end of a line as the end of a statement, so semicolons are optional. Leaving them off keeps code clean and readable. You only need a semicolon if you want to put two statements on the same line.

    Mini-Challenge: About-Me Card

    No blanks this time — just a brief and an outline to keep you on track. Write it yourself, run it on SwiftFiddle, and check your output against the example in the comments.

    🎯 Mini-Challenge: build an About-Me card
    // 🎯 MINI-CHALLENGE: About-Me card
    // 1. Make three constants with 'let':
    //      name (your name), city (where you live), and favourite (a hobby).
    // 2. Print three lines using string interpolation \(...) :
    //      "Name: <name>", "City: <city>", "I love <favourite>."
    // 3. BONUS: add a // comment above each print saying what it shows.
    //
    // ✅ Example output:
    //    Name: Sam
    //    City: Leeds
    //    I love coding.
    
    // your code here
    This is real code — run it for free atSwiftFiddleor in your own editor.

    🎉 Lesson Complete!

    • ✅ Swift is Apple's modern, fast, safe language for iOS, macOS, and beyond
    • ✅ It runs in Xcode, Swift Playgrounds, and free online editors (no Mac needed to start)
    • print(...) shows output; // and /* */ add comments Swift ignores
    • \(value) interpolation drops a value straight into a string
    • Next lesson: Variables & Data Types — store text, numbers, and true/false values with let and var

    Sign up for free to track which lessons you've completed and get learning reminders.

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service

    Install LearnCodingFast

    Learn faster with the app on your home screen.