Skip to content
My Compiler
LoginRegister

Code Examples

Short, runnable examples for the basics. Choose a language and open any example in the compiler.

  1. Hello, World!

    Every language starts here: print a message to the screen to check that everything works.

    print("Hello, World!")
    Open in compiler
  2. Variables

    Variables store values such as text, whole numbers and decimals so you can reuse them.

    name = "Ada"
    age = 21
    height = 1.75
    print(name, age, height)
    Open in compiler
  3. Conditions

    if / else lets your program choose a path depending on a value.

    age = 18
    if age >= 18:
        print("Adult")
    elif age >= 13:
        print("Teenager")
    else:
        print("Child")
    Open in compiler
  4. Loops

    Loops repeat a block of code: use for when you know the count and while when you wait for a condition.

    for i in range(1, 6):
        print(i)
    
    n = 3
    while n > 0:
        print("Countdown", n)
        n -= 1
    Open in compiler
  5. Functions

    A function packs code into a reusable block that takes inputs and returns a result.

    def add(a, b):
        return a + b
    
    print(add(2, 3))
    Open in compiler
  6. Fibonacci sequence

    Each number is the sum of the two before it. A classic exercise that combines variables, loops and arithmetic.

    def fib(n):
        a, b = 0, 1
        for _ in range(n):
            print(a, end=" ")
            a, b = b, a + b
    
    fib(10)
    Open in compiler
Code Examples | My Compiler