GitHub

Usage & Examples

Stable Command-line usage is stable in Levython 1.0.

This page provides a comprehensive overview of Levython usage patterns and practical examples.

Command Line Usage#

terminal
# Run a Levython file
levython script.levy

# Run REPL (interactive mode)
levython

# Execute inline code
levython -e 'say("Hello, World!")'

# Show version
levython --version

# Get help
levython --help

Hello World#

The simplest Levython program:

hello.levy
say("Hello, World!")

Working with Variables#

variables.levy
# Numbers
age <- 25
price <- 19.99

# Strings
name <- "Levython"
message <- "Fast as C!"

# Booleans
is_fast <- true
is_slow <- false

# Lists
numbers <- [1, 2, 3, 4, 5]
mixed <- ["text", 42, true]

# Display values
say("Name: " + name)
say("Age: " + str(age))
say("Fast? " + str(is_fast))

Functions#

functions.levy
# Simple function
act greet(name) {
    say("Hello, " + name + "!")
}

# Function with return value
act add(a, b) {
    -> a + b
}

# Recursive function
act factorial(n) {
    if n <= 1 {
        -> 1
    }
    -> n * factorial(n - 1)
}

# Call functions
greet("World")
result <- add(10, 20)
say("10 + 20 = " + str(result))

fact <- factorial(5)
say("5! = " + str(fact))

Data Structures#

lists.levy
# Create a list
fruits <- ["apple", "banana", "cherry"]

# Access elements
first <- fruits[0]
say("First fruit: " + first)

# Add elements
append(fruits, "date")
append(fruits, "elderberry")

# Iterate over list
say("All fruits:")
for fruit in fruits {
    say("  - " + fruit)
}

# List operations
numbers <- [5, 2, 8, 1, 9]
say("Length: " + str(len(numbers)))
say("Sum: " + str(sum(numbers)))
say("Min: " + str(min(numbers)))
say("Max: " + str(max(numbers)))
say("Sorted: " + str(sorted(numbers)))

File I/O#

file_io.levy
# Write to file
content <- "Hello from Levython!\nFast and powerful."
write_file("output.txt", content)
say("File written successfully")

# Read from file
data <- read_file("output.txt")
say("File contents:")
say(data)

# Check if file exists
if file_exists("output.txt") {
    say("File exists!")
} else {
    say("File not found")
}