### Intro to Python

Course
Time
2 hours 57 minutes
Difficulty
Beginner
CEU/CPE
3

### Video Transcription

00:00
Hello, everyone. And welcome back to Cyberia on demand. I am your instructor, Joe Perry, and you're watching Intro Toe Python. This is less than eight where we're gonna be doing a deep dive on lists and in this lesson, we're gonna learn two things. The first is we're gonna learn some list methods as we talk about string. Method's gonna do the same thing with lists and we're going to start up
00:18
applying slicing toe list. So we're just gonna go over kind of the same things we did with strings,
00:22
But this time we're gonna apply them to lists.
00:25
So to do that, we're gonna generate our first list, which we're gonna call l one. And that's gonna consist of all the number three being one in 10.
00:33
***.
00:35
L one, as you can see exists, contains the numbers who wanted to exist.
00:39
Let's look at the possible functions we can call against that list.
00:43
So have upend clear copy count, extend, index, insert pop, remove reverse and sort those air. All of the built in methods for lists. We're gonna discuss most of them pretty quickly. So first, let's look at
00:55
fell one dot of pen
00:57
and we can do that
00:59
by examining it with
01:00
through this first
01:02
help.
01:06
See, we can see here that upend is a method of the built in list. Instance.
01:11
We do upended. Then we give it some object argument,
01:14
and it depends the object to the end of the list.
01:17
Fair enough. Let's see what that looks like practically will do. L one dot upend
01:22
the number 11.
01:26
It's not my commands here.
01:29
There we go.
01:34
01 again. And we can see here that the number 11 has been added to our list.
01:38
Fantastic. So I would know how to add things to the list.
01:41
What other functions did we have?
01:45
We have clear
01:47
l one dot clear. Let's see what that does.
01:51
Well, pretty straightforward. It clears the list out.
01:57
We're not gonna quite go, you know, item by item through this. But I am going to just demonstrate a few of these methods for your real fast
02:07
fix That
02:12
so we talked about upend the other one that we really want to talk about right away is pop.
02:15
Pop is a function that will take the last item off of the list and will print it to your screen will return it to your variable, depending on what you're doing. So if I were to do X equals
02:25
l one dot pop
02:29
and the print X
02:30
imprints one,
02:32
you'll see that the list no longer contains the number 10. It is now stored in the variable that X.
02:38
There's a really easy way when you use things like pop and upend. What you're actually doing is creating what we call a stack. The idea of a last in first out structure
02:47
when you're implementing stacks in Python, you actually will generally be using the penned and pop functions very, very useful tools.
02:59
Looking back on our list of options, we have two more that we really want to examine. First is reverse. Reverse is gonna do something pretty similar to what we've seen before.
03:07
But what's cool about it
03:09
is that it's not actually gonna return anything. That's why I wanted to address this is it was actually happening with. This list is happening inside the list without ever actually returning any code. You're not creating a new list. You are reversing the existing list in place, and then,
03:24
as a useful was useful about that, is that it lets us demonstrate the next function that I want to talk about real fast,
03:30
which is
03:32
sort.
03:35
The last list function. Sorting lists is a challenge in basically every programming language. It's something that you'll see in a lot of job interviews. The task you'll have pretty often in Python. You can very easily do it
03:46
just by running that sort command and real fast. Let's look at sort under helped as well.
03:55
You see, it has two arguments, key and reverse, stable sort in place. Basically, what it's saying is, if you have a key, you have some order in which you want some method it mechanism by which you want to start. You can give that as an argument, you know, also reverse sort it.
04:11
But we've sorted our list here, and we can see that L one is back to being 123456789 There's some other list methods, and in our lab you'll get to play with those a lot. I definitely recommend you check them out, but for now, now that we understand what the methods are and how to apply them and how to find them and find information about them. We're gonna move on to the second half of this lesson, which is a playing slicing two lists. Now, if you remember,
04:30
Waken have, for example, our string
04:35
and we could do slicing in that
04:39
of reversing the lists or reversing the string with negative one weaken Do X is
04:45
two through five.
04:48
You know, X is the entire list, but only every second letter,
04:53
all that slicing works exactly the same way with lists. And I actually I actually accidentally said list a couple of times in there. I think
05:00
because they're
05:01
this methodology is interchangeable.
05:04
So we can do, For example, we have our l one we could do l one, and we only want the fifth item.
05:15
This, by the way, is why I really should stop using l. Because I keep mixing up, Ellen one. But I'm gonna keep doing it because, well, there's no one here to tell me I can't. But here we see the index of five and six makes sense. The fifth number of the sixth number rather is 6012345
05:33
We could also do l one
05:38
reverse.
05:40
Now the thing that's interesting here and the reason why I wanted to address it why I brought reverse up earlier is that l one by itself, actually is still the same. So l one
05:51
with the negative one index step and l one dot reverse
05:56
are not actually the same thing reverses sorting that, reversing that list in place. It changes the list itself,
06:02
whereas just using the indexes is something that you can do to reverse it without ever actually modifying the list. Additionally, if you wanted to copy out one,
06:12
you wouldn't actually do something like l q equals out one. Because then you're using the same list what you would do there and it to demonstrate that we've got l two and one.
06:26
You can see that by modifying l two we modified out one because it's the same list. So that's one of the dangers of using lists the way you would copy a listen. First off, re sort no one.
06:36
Well, you would. Copulate is actually by using slicing like we just talked about.
06:41
You would just do
06:44
all of l one with no step.
06:47
And actually, you can get rid of the second Colin there because you're not using a step so all of l one
06:54
and then you would just assign that
06:58
l two.
06:59
So now we could do l two dot reversed
07:03
and we see that l one stays the same. So when you're trying to copy a list you want to use string lee string slicing or list slicing rather to do that
07:12
string slicing and list lacing. The reason why I keep using them interchangeably because they function in essentially the exact same way. So everything that we've done with strings, you'll also get to do with lists in the lab while you're working on those exercises which I highly recommend you do. Ah, that's gonna be all there is for this lesson again. In this lesson, we learned how to apply and use some list methods.
07:30
And then we took string slicing, and we turned it into a list slicing.
07:32
I hope you enjoyed it. I hope you found it informative. As always, I have been your instructor, Joe Perry, and thank you for watching intruder python on cyber eri on demand

### Intro to Python

This is an introductory course on Python for cyber security, giving students the ability to understand the basics of the language, solve problems with scripts and identify useful Python modules.

### Instructed By

Joe Perry
Senior Technical Instructor at FireEye, Inc
Instructor