Lesson 2 of 17
Variables and Types
Let Bindings
In Gleam, you create variables with let:
let name = "Gleam"
let age = 1
let pi = 3.14
let is_fun = True
All variables in Gleam are immutable. Once you bind a value to a name, it cannot be changed. You can, however, rebind a name to a new value:
let x = 1
let x = x + 1 // This creates a new binding, x is now 2
Basic Types
Gleam has these fundamental types:
| Type | Examples | Description |
|---|---|---|
Int | 42, -7, 1_000_000 | Arbitrary precision integers |
Float | 3.14, -0.5, 1.0e10 | 64-bit floating point |
String | "hello" | UTF-8 strings |
Bool | True, False | Boolean values |
Type Annotations
Gleam can infer types, but you can add annotations for clarity:
let name: String = "Gleam"
let age: Int = 1
Int and Float Are Separate
Gleam strictly separates integers and floats. You cannot mix them in arithmetic:
let x = 1 + 2 // Int arithmetic: +, -, *, /, %
let y = 1.0 +. 2.0 // Float arithmetic: +., -., *., /.
The . suffix on operators indicates float operations. This is different from most languages but prevents subtle precision bugs.
Converting Between Types
Use int.to_string and float.to_string to convert numbers to strings:
import gleam/int
import gleam/float
let age_str = int.to_string(42) // "42"
let pi_str = float.to_string(3.14) // "3.14"
Your Task
Create variables for your name and your favorite number. Print them on separate lines in the format:
Name: Lucy
Number: 7Gleam runtime loading...
Loading...
Click "Run" to execute your code.