Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

2. Values

Korrin has ten kinds of value. This chapter covers the everyday ones.

Numbers

int (whole numbers) and float (with a decimal point or exponent) are separate types:

print(type(3))
print(type(3.0))
print(10 + 5)
print(10 / 4)
print(10 % 3)
# => int
# => float
# => 15
# => 2.5
# => 1

/ always gives a float, even for 10 / 2 (which is 5.0). If you want a whole number, convert: int(10 / 4) is 2.

Integer overflow is an error, not a silent wraparound — a program that overflows int is almost always buggy, so Korrin tells you.

Strings

Double quotes only. Join them with +, measure them with .len(), index them by character:

first = "Ada"
last = "Lovelace"
full = first + " " + last
print(full)
print(full.len())
print(full[0])
# => Ada Lovelace
# => 12
# => A

The escapes are \n, \t, \r, \\, and \".

Strings are immutable, so their methods return a new string. They chain:

print("  Grace Hopper  ".trim().upper())
print("a,b,c".split(","))
print("ha".repeat(3) + "!")
# => GRACE HOPPER
# => ["a", "b", "c"]
# => hahaha!

The full list — upper, lower, trim, split, replace, find, contains, starts_with, ends_with, slice, repeat — is in specification §8.

Interpolation

Any { } in a string is a hole: the expression inside is evaluated and its value dropped in. Every string works this way — there is no special prefix.

name = "Ada"
score = 90
print("{name} scored {score}%")
print("next year: {score + 1}")
# => Ada scored 90%
# => next year: 91

A hole can hold any expression — a call, an index, another string:

row = {"user": "grace", "wins": 12}
print("{row["user"]} has {row["wins"]} win{"s"}")
# => grace has 12 wins

For a literal brace, double it: "{{" prints {. This replaces the old "count: " + str(n) style — you almost never need + str(...) any more.

Booleans, nil, and truthiness

true, false, and nil (the "no value"). In a condition, only nil and false are false0, "", and [] are all true:

if 0:
    print("zero is truthy")
if "":
    print("so is the empty string")
# => zero is truthy
# => so is the empty string

and and or return one of their operands, which makes or a neat way to supply a default:

name = nil
print(name or "anonymous")
# => anonymous

Lists

Ordered, mutable, written with [ ]. Operations are methods: push, pop, sort, reverse, insert, remove_at, index_of, contains, slice, join, len.

xs = [3, 1, 2]
xs.push(4)
print(xs)
print(xs[0])
print(xs.len())
last = xs.pop()
print(last)
# => [3, 1, 2, 4]
# => 3
# => 4
# => 4

push, insert, remove_at, reverse, and sort change the list in place:

xs = [3, 1, 2]
xs.sort()
print(xs)
# => [1, 2, 3]

Lists are shared, not copied. If two names refer to the same list, a change through one is seen through the other:

a = [1, 2]
b = a
b.push(3)
print(a)
# => [1, 2, 3]

Maps

Key–value pairs, written with { }, keys kept in insertion order. Keys must be nil, a bool, an int, or a str:

ages = {"ada": 36, "alan": 41}
ages["grace"] = 45
print(ages["ada"])
print(ages.has("alan"))
print(ages.get("linus", 0))
print(ages.keys())
# => 36
# => true
# => 0
# => ["ada", "alan", "grace"]

m[key] on a missing key is an error; m.get(key, default) is the safe version. The other map methods are values, remove, pairs, and len.