Lessons · The language · 13 of 36
Data: lists and objects
Variables, lists, objects and the list helpers.
A variable holds a value: a number, text in quotes, true / false, a list […] or an object
{ … }.
score = 0
player = "Ada"
playing = true
scores = [12, 40, 7]
user = { name: "Sam", age: 31 }
main show_data():
scores.add(25)
print(player, "has", scores.count, "scores")
print("first", scores.first, "last", scores.last)
print("has 40?", scores.has(40))
user.age = user.age + 1
print(user.name, "is now", user.age)
scores.remove(7)
print(scores)
scores.clear()
print("left:", scores.count)
| On a list | Does |
|---|---|
items.add(x) |
adds x at the end |
items.remove(x) |
removes x |
items.clear() |
removes everything |
items.count |
how many there are |
items.first, items.last |
the first and the last |
items.has(x) |
true when x is in the list |
items.index(x) |
where x is (from 0) |
items[0] |
the item at a place, from 0 |
Objects group named values: user.name reads one, user.age = 32 changes it. A list of objects is
the usual way to hold records: tasks = [{ name: "Milk", done: false }].
Every example here runs in Vi Studio: open it in your browser (phones too) and press Start learning on the welcome page. Each code block there has a Try it button that runs it in the phone preview.
More in The language
- Comments and names# comments, <# blocks #> and snake_case names.
- ContainersEverything on screen is a container; children one per line.
- Size and spacesize out of 100, and space_inside / between / around.
- Arrange and placeDirection, spot and spread; floating with place.
- Texttext and text_style.
- Colour, shape and imagesColours and shades, shape, shadow, see_through, outline, @images.
- Buttons, inputs and other kindsbutton, input, search, icon, link and scrolling lists.
- Eventson_tap, on_hold, on_touch, on_swipe, on_change, on_submit, on_hit.
- FunctionsYour own functions and the built-in ones.
- Conditions and loopsif / else, the inline if, and, or, not, for and while.
- Programs without a screenmain functions and print().
About Vi. Vi is a free, open-source programming language for building real mobile apps. You describe each screen in plain words, and Vi compiles it to React Native (Expo) for Android, iOS and the web. Its editor, Vi Studio, runs on Windows, in any browser and on your phone, with the app running live as you type. Read more about Vi.