text
stringlengths
0
897
```
p[1:-1]
```
This gets the slice starting at element 1 (i.e., the one with 1 to its left), and going up to but not including the last one.
## Attack of the Mutant Lists
One important difference between strings and lists is that strings are immutable, while lists are mutable. "Mutable" means "able to be mutated," and "mutated" is just a fancy word for "changed."
mutable
: describes an object with data that can be changed: in MiniScript, lists and maps
immutable
: describes an object that can't be changed, but only replaced: strings and numbers
You've been mutating lists since you first saw them (i.e. yesterday), with methods like `.push`. Here's another way to mutate a list: replace any element with a new value.
```
p[3] = 42
```
Try that, and then examine `p` to see what it contains. It looks the same as before, except element 3 (the one with 3 items to its left) has been replaced with 42. However, it's important to note that you can *not* replace a slice this way.
{caption:"This doesn't work."}
```miniscript
p[3:6] = [1,2,3]
```
Try it. At the time of this writing, at least, it doesn't work.
D> It's possible that this feature will be added to MiniScript someday. It's a sensible thing to want, and wouldn't break any existing code.
And you also can't mutate a string.
{caption:"This also doesn't work (with strings)."}
```miniscript
s[2] = "x"
```
Of course you can always assign a *new* string to a string variable. When you do something like `s = s.upper`, that is exactly what you're doing. That's not mutating the string; it's just replacing it with a different one. It's the same with numbers, for that matter; there is no way to *change* a number, but you can...
Because lists are mutable, you have to be a little careful sometimes. When you do something like `q = p`, it is not creating a new list; it is simply making `q` refer to the same list that `p` already referred to. The list itself lives off in the computer's memory somewhere, and has variables like `p` and `q` that re...
```
q = p
q[0] = "hey"
p
p.push "ho!"
q
```
{pageBreak}
The results will not be surprising, as long as you remember that `p` and `q` do not *contain* a list; they only *refer* to a list. Assignment copies the reference, but does not make a new list.
{width:"50%"}
![](listRefs.svg)
So what do you do when you need to make a copy of a list? For example, suppose you want to keep your original list, but also make a copy that is slightly different. Here slicing comes to the rescue again. It turns out that slicing *always* gets you a new list, even if the range of the slice covers the entirety of th...
```
q = p[0:p.len]
```
...but, as you learned above, you can omit the first index when it is 0 and omit the last one when it is the length of the list or string. So the standard solution looks like this:
```
q = p[:]
q.push "I'm unique!"
p
q
```
{gap:20}
{width:"62.37%"}
![](listCopy.svg)
Think of `p[:]` as an idiom — a standard way of phrasing a concept in a language. The concept in this case is making a complete copy. You might even read this out loud as "copy of p", though if you forget and need to work it out at first, "p (from the beginning to the end)" is fine too.
## Programs as Text Files
So far in this book, we've written MiniScript code in two contexts: in the Try-It! page on the web, and in the REPL of command-line MiniScript. Both of these are great for short bits of code, but not so great for larger programs. The Try-It! page has a code limit (currently about 2000 characters), and of course typin...
So it's time to learn to code the way professional programmers do: using a separate code editor! In our case, any text editor will do. But this being the modern world, let's make sure we are clear on the difference between a text editor and a word processor.
text editor
: a program that edits plain text (.txt) files. Examples on Windows include Notepad, Notepad++, and GEdit; examples on Mac include TextEdit and BBEdit
word processor
: a program that edits formatted documents. Microsoft Word is the most widely known word processor, but Apple Pages is another example
While both kinds of applications are superficially similar in how they look and work, what they do is fundamentally different. Text files are fairly universal, but word processors all have their own proprietary format, and their documents are generally not usable in any other program. So, when programming, you will n...
A few examples of text editors are given above. If you're on Windows, Microsoft Notepad is a simple text editor that comes with the operating system, and is perfectly adequate. Many people prefer Notepad++, which is a free replacement that has some more powerful features. The situation on Mac is similar: the built-i...
Pick one, and create a new file. Enter the following text.
{caption:"Our first MiniScript source file!"}
```miniscript