This chapter explores the building blocks of ShellLite: storing data, using different types of information, and talking to the user.
Variables are like labeled boxes where you can store information. You create one simply by giving it a name and using = to assign a value.
name = "Alice"
age = 30
is_happy = yesYou don't need to tell ShellLite what "type" of data it is; it figures it out automatically (dynamic typing).
- Must start with a letter or underscore (
_) - Can contain letters, numbers, and underscores
- Case-sensitive (
Nameandnameare different) - Cannot be a reserved keyword
Sometimes you want a value that never changes. Use const for this. By convention, we often use ALL CAPS for constants to distinguish them from regular variables.
const PI = 3.14159
const MAX_USERS = 100
const APP_NAME = "ShellLite v0.05"Note: If you try to change a const later in your code, ShellLite will throw an error.
ShellLite is dynamically typed, meaning variables can hold any type of value. Here are the core types:
| Type | Description | Example |
|---|---|---|
| Number | Integers and floating-point numbers | 42, 3.14, -10 |
| String | Text enclosed in quotes | "Hello", 'World' |
| Boolean | Logical yes/no values | yes, no, true, false |
| List | Ordered collection of items | [1, 2, 3] |
| Dictionary | Key-value pairs | {"name": "Alice"} |
| Function | Callable code blocks | fn x => x * 2 |
| Object | Instance of a class/structure | new Car |
| None/Null | Represents absence of value | (empty result) |
Integers (whole numbers) and Floats (decimals).
count = 10
price = 19.99
negative = -5Arithmetic Operators:
| Operator | Description | Example |
|---|---|---|
+ |
Addition | 5 + 3 → 8 |
- |
Subtraction | 10 - 4 → 6 |
* |
Multiplication | 3 * 4 → 12 |
/ |
Division | 15 / 3 → 5 |
% |
Modulo (remainder) | 17 % 5 → 2 |
Compound Assignment:
x = 10
x += 5 # x is now 15
x -= 3 # x is now 12
x *= 2 # x is now 24
x /= 4 # x is now 6
x %= 4 # x is now 2Natural Language Math:
increment x # x = x + 1
decrement x # x = x - 1
multiply x by 3 # x = x * 3
divide x by 2 # x = x / 2Text is enclosed in either double quotes " or single quotes '.
greeting = "Hello World"
quote = 'Keep it simple.'Escape Sequences:
| Sequence | Meaning |
|---|---|
\n |
Newline |
\t |
Tab |
\r |
Carriage return |
\" |
Double quote |
\' |
Single quote |
Combining Strings:
You can join strings using +.
full_name = "John" + " " + "Doe"String Interpolation:
You can inject variables directly into strings using {}. This is often cleaner than using +.
name = "Shrey"
score = 100
say "Player {name} has a score of {score}!"String Methods:
text = "Hello World"
say text.upper() # "HELLO WORLD"
say text.lower() # "hello world"
say len(text) # 11Instead of true and false, ShellLite uses yes and no (though true/false work too).
active = yes
sleepy = no
is_ready = trueTruthiness and Falsiness:
In ShellLite, certain values are considered "falsy" (evaluate to no in conditions):
no/false0- Empty string
"" - Empty list
[] - Empty dictionary
{}
Everything else is "truthy" (evaluates to yes).
| Operator | Description | Example |
|---|---|---|
and |
Both must be true | yes and yes → yes |
or |
At least one must be true | yes or no → yes |
not |
Negates the value | not yes → no |
To display text on the screen, use the say command. You can also use print or show if you prefer.
say "Welcome!"
print "Processing..."
show "Done."You can make your output pop with colors!
say in red "Error!"
say in green "Success!"
say bold blue "Info"Supported colors: red, green, blue, yellow, cyan, magenta, white, black
Supported styles: bold
To get information from the user via the terminal, use the ask command (or input).
# Simple input
name = ask "What is your name? "
say "Hello " + name
# Using the input in logic
age = ask "How old are you? "
if age > 18
say "Access Granted"The ask command pauses the program, prints the prompt (if provided), and waits for the user to type something and press Enter.
Operators are evaluated in the following order (highest to lowest):
- Parentheses
() - Unary operators:
not,-(negation) - Multiplication, Division, Modulo:
*,/,% - Addition, Subtraction:
+,- - Comparison:
>,<,>=,<=,==,!= - Logical AND:
and - Logical OR:
or
# Example
result = 2 + 3 * 4 # result is 14, not 20
result = (2 + 3) * 4 # result is 20ShellLite tries to ignore "filler words" to make code read like English. You can use words like the freely.
if the score is more than 10
say "Win"In this example, the is completely ignored by the computer - it's just there for you.
| Symbol | English Phrase |
|---|---|
== |
is, is exactly, equals |
!= |
is not |
>= |
is at least |
<= |
is at most |
> |
is more than, is greater than |
< |
is less than |