Professional Documents
Culture Documents
1
00:00:04.000 --> 00:00:08.000
In this section, we're going to look at the built in, data structures in Python
2
00:00:08.000 --> 00:00:12.000
which are extremely important when building real applications. First,
3
00:00:12.000 --> 00:00:16.000
we're going to look at lists, and then we will look at couples, sets, and
4
00:00:16.000 --> 00:00:20.000
dictionaries. So earlier you have seen, that we have used the square brackets
5
00:00:20.000 --> 00:00:24.000
to define a list of a sequence of objects. In
6
00:00:24.000 --> 00:00:28.000
between these brackets we can have object of any type, so we can have list
7
00:00:28.000 --> 00:00:32.000
of strings, like this, and then assign it to a variable
8
00:00:32.000 --> 00:00:36.000
like letters. We can also have a list of
9
00:00:36.000 --> 00:00:40.000
numbers, booleans, or even a list of lists. Let me show you.
10
00:00:40.000 --> 00:00:44.000
So here we have a list. Each item in this list,
11
00:00:44.000 --> 00:00:48.000
will be a list itself. So here's the first item.
12
00:00:48.000 --> 00:00:52.000
Which is a list of two items, now let's add another
13
00:00:52.000 --> 00:00:56.000
item to our name or parent list, this item is also
14
00:00:56.000 --> 00:01:00.000
a list with two items, so now we have a
15
00:01:00.000 --> 00:01:04.000
matrix, which is a two dimensional list. Now
16
00:01:04.000 --> 00:01:08.000
let me show you some cool tricks. Let's say you want to have a list of 100
17
00:01:08.000 --> 00:01:12.000
0's. You don't want to manually create those like this, that's very
18
00:01:12.000 --> 00:01:16.000
ugly. Let me show you a better way. So we define a list
19
00:01:16.000 --> 00:01:20.000
of item, one zero, and then we can multiply it by 100,
20
00:01:20.000 --> 00:01:24.000
and the result will be this. Let me show you.
21
00:01:24.000 --> 00:01:28.000
Print zeros here
22
00:01:28.000 --> 00:01:32.000
it is. There it is, so using a star
23
00:01:32.000 --> 00:01:36.000
or an asterisk we can repeat the items in a list. Now,
24
00:01:36.000 --> 00:01:40.000
similarly we can use a plus to concatenate multiple lists.
25
00:01:40.000 --> 00:01:44.000
Let me show you. So first I'm going to change this to 5,
26
00:01:44.000 --> 00:01:48.000
now, let's define the variable combined,
27
00:01:48.000 --> 00:01:52.000
which is our zeros list, plus letters,
28
00:01:52.000 --> 00:01:56.000
let's see what happens. Print combined
29
00:01:56.000 --> 00:02:00.000
you know it, so we have 5 zeros, followed by an
30
00:02:00.000 --> 00:02:04.000
abc. Now as you can see in Python every object
31
00:02:04.000 --> 00:02:08.000
in the list can be of a different type, so they don't have to be from the same
type.
32
00:02:08.000 --> 00:02:12.000
We can combine a list of numbers with strings, and booleans, or even
33
00:02:12.000 --> 00:02:16.000
lists. Now let's say you want to have a list of numbers,
34
00:02:16.000 --> 00:02:20.000
like 0, 1, 2, 3, all the way up to 20. You don't want to type all
35
00:02:20.000 --> 00:02:24.000
of the by hand. There is a better way. So we have this list function
36
00:02:24.000 --> 00:02:28.000
as you can see this function takes an iterable.
37
00:02:28.000 --> 00:02:32.000
So we can pass any interval here, and convert it to a list.
38
00:02:32.000 --> 00:02:36.000
Earlier you learned about the range function, this function returns
39
00:02:36.000 --> 00:02:40.000
a range object which is iterable. Which means we can iterate or
40
00:02:40.000 --> 00:02:44.000
loop over it. So here we can call this function and pass
41
00:02:44.000 --> 00:02:48.000
20, and with this we can create a list of numbers from 0,
42
00:02:48.000 --> 00:02:52.000
to 20, let me show you, so let's store it
43
00:02:52.000 --> 00:02:56.000
in numbers, and then print it on the terminal,
44
00:02:56.000 --> 00:03:00.000
there you go. So zero up to 20, but note that
45
00:03:00.000 --> 00:03:04.000
20 itself is not included. As another
46
00:03:04.000 --> 00:03:08.000
example, let's call the list function, and pass
47
00:03:08.000 --> 00:03:12.000
a string. Earlier I told you that strings are also iterable,
48
00:03:12.000 --> 00:03:16.000
we can loop over them, so we can pass them to the list function,
49
00:03:16.000 --> 00:03:20.000
and see what we get. Let's print
50
00:03:20.000 --> 00:03:24.000
chars on the terminal. So you can see each character
51
00:03:24.000 --> 00:03:28.000
in our original string is an item
52
00:03:28.000 --> 00:03:32.000
in this list. So, these are a few different ways to create a list
53
00:03:32.000 --> 00:03:36.000
in Python. Now that we have a list, we can get the number of items in
54
00:03:36.000 --> 00:03:40.000
that list using the len function. So here we can print
55
00:03:40.000 --> 00:03:44.000
the len or length of chars. Let's
56
00:03:44.000 --> 00:03:48.000
take a look, so we have 11 items in this list. Over the
57
00:03:48.000 --> 00:03:52.000
next few lectures we'll look at various