Hello and welcome to Pluralsight. I'm Dr. Chris Brown, and this course is Python Quick Start for Linux
System Administrators. This lesson is called Interacting with the Linux System.
We'll begin the lesson with a look at a really neat language feature called slicing.
Then we'll see how to process command line arguments and how to access environments.
We'll talk about Python's file-like objects and see how to write a classic filter program in Python.
Finally, we'll learn how to handle signals in a Python program and suggest some useful things you might do with them.
So before diving into the real business of this lesson, I want to introduce you to a very handy language
feature called slicing.
Slicing extracts part of a sequence to create a new sequence. It does not modify the original sequence.
Here's an example. X is some sort of sequence. It could be a string, a list, or a tuple.
The stuff inside the square brackets specifies the slicing. The first number is the index of the start of the slice.
If you leave it out, the slice will start at the beginning, that is at index 0.
The second number is the index of the end of the slice or, rather, the index one beyond the end of the slice.
So in this example, the highest index included in the slice will be 14.
You can leave this out too, in which case the slice runs to the end.
Finally, there's an optional step size. If you omit this, it defaults to 1.
Any of these numbers can be negative, meaning count backwards from the end.
Let's look at some examples.
Here data is some sort of sequence. The slice 2:9 generates this slice containing elements 2 through 8.
Omitting the 2 means that the slice starts at the beginning. Omitting the 9, on the other hand,
means that the slice runs to the end. One really useful feature is that the numbers can be negative
meaning, as I said, count backwards from the end. So here the slice ends three elements before the end of
the original. And here with an increment of 2, we're plucking out alternate elements.
Now in my experience, there are a few rather degenerate-looking cases that tend to be the most useful.
Let's look at some of those. This removes the first element of a sequence.
This removes the last element. This gets you just the last element. And this reverses the sequence.
Finally, this generates a deep copy. Let me repeat a couple of points.
Slicing operates on any sequence type. You can slice a string and get a shorter string.
You can slice a list and get a shorter list. And you can slice a tuple and get a shorter tuple.
In all cases, you get a new sequence. Slicing does not modify the original.
Now I used the term deep copy a moment ago. What's that all about?
Well suppose we create a simple list called a. A then refers to an object that looks like this.
The assignment b = a just generates a new name that refers to the same object.
We refer to it as a shallow copy. Using slicing like this, though, makes c refer to a completely new copy
of the list. We call this a deep copy. Why do we care? Well suppose we modify an element of the list a.
The b reference will also see the changed value, but c will be unaffected.
Let me quickly demonstrate that to you at the REPL. Here's the original list.
Here's the shallow copy. Here's the deep copy. All of them, of course, look identical at the moment.
But if we modify an element in a, then a sees the new value. So does b. But c does not.