0% found this document useful (0 votes)
6 views3 pages

Python Lists

This lesson covers Python lists, which are used to store multiple ordered items in a single variable, defined using square brackets. Key topics include indexing to access specific elements, list slicing to obtain a range of elements, and defining lists with different data types. The lesson also demonstrates practical examples using a Jupyter Notebook, including accessing and slicing lists of companies and their revenues.

Uploaded by

wannad413
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Python Lists

This lesson covers Python lists, which are used to store multiple ordered items in a single variable, defined using square brackets. Key topics include indexing to access specific elements, list slicing to obtain a range of elements, and defining lists with different data types. The lesson also demonstrates practical examples using a Jupyter Notebook, including accessing and slicing lists of companies and their revenues.

Uploaded by

wannad413
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Lists

[00:00:00.00] [AUDIO LOGO]

[00:00:08.41] RYAN AHMED: Hello, everyone, and welcome to this lesson on Python lists. In
Python, lists are used to store multiple items in a single variable. These items are ordered and
have a specific index. Python lists are powerful because you can contain a collection of items
with different data types in them. For example, in a single list object, you can have integers,
strings, and floating points.

[00:00:33.91] Lists are defined using square brackets and elements are separated by commas.
Here are the key learning objectives of this lesson. Define Python lists and understand their
benefits and use cases. Use indexing to access specific elements in Python lists. Perform lists
slicing to access a range of elements within the lists. So let's head over to our Jupyter Notebook
and get started.

[00:01:02.36] [AUDIO LOGO]

[00:01:09.59] All right, so right now, we are in the Jupyter Notebook titled Python Lists. As a
quick reminder, we use Jupyter Notebooks throughout the entire course, because we can include
everything we need in one place. This includes slides, code scripts, output generated from that
code. We can also include comments, practice opportunities, and the solution of those practice
opportunities as well. So you can share the notebook with other people or with your colleagues at
your company easily and efficiently.

[00:01:44.16] So let's go ahead and get started with our topic today. And that is going to be
Python lists. In Python, a list is a collection of items that are ordered and have a specific index.
Lists are defined using square brackets, and they are separated by commas.

[00:02:03.76] So let's go ahead and define a list containing five companies in US, Asia, Europe,
and Africa. And simply, that's how you define a list. You provide a name to that list. You say,
my_companies for example, equals to, you declare or define a Python list using these square
brackets that you see here, and then you list elements within that list separated by commas. For
example, here, I listed Apple, and then I listed Samsung afterwards, followed by Alibaba, Novo
Nordisk, and Naspers. Please note that Apple and Samsung-- they simply manufacture electronic
devices, such as phones and computers. Alibaba Group is a Chinese multinational company that
specializes in tech, e-commerce, retail, and internet. And Novo Nordisk is a Danish multinational
pharmaceutical company. And finally, Naspers is an African internet and tech company that is
headquartered in Cape Town, South Africa.

[00:03:13.86] So an important point when you define a list is that every element in a list has its
own index. So the first element in the list has an index of 0. So Apple has an index of 0.
Samsung has an index of 1. Alibaba, index of 2. And then Naspers at the end, has an index of 4.

[00:03:36.28] So the next topic that I wanted to show you is regarding list indexing. So if you
would like to access specific elements in a Python list, we access the elements using the index.
For example, if I would like to access, let's say, Apple, you say my_companies-- and that's the
name of the Python list-- you open a square bracket. And then you specify the index, or the
location of that element within the list.

[00:04:05.03] So for example, Apple has an index of 0. I know that might sound a little bit
confusing at first. But trust me. You will get used to it. You just say my_companies[0], of 0 and
then you would be able to access Apple, which is the first element in the list. The second
element, which is Samsung, has an index of 1. You say my_companies, square brackets, and then
you specify the index or the location. And then you are able to access the second element, which
is Samsung. If you want to access the last element-- so the last element here in the list has an
index of 4-- so you say my_companies[4], and then you will end up with Naspers.

[00:04:45.73] So the last topic that I wanted to cover before we head over to our code is lists
slicing. So in lists slicing is used to obtain more than one element from a Python list. The slicing
operator, if I say n, colon, m used to obtain elements that starts from index n up until, but not
including the index m. Again, I know that might sound a little bit confusing at first. But you will
get used to it. So let's assume that I wanted to access, or slice my list, which is my_companies,
and I wanted to obtain Samsung, Alibaba, and Novo Nordisk, for example.

[00:05:27.79] To do that, you specify the name of the list. You say my_companies. You open
square brackets. You specify the index of the first element you would like to access. And
Samsung here has an index of 1. That's why you say 1. And then you say colon. And then
afterwards, you add the last index you are interested in. So if you say 4, you essentially get
elements starting from index 1 up until, but not including 4. So you get elements with index 1, 2,
and 3. And that's why here, I got Samsung, Alibaba, and Novo Nordisk afterwards.

[00:06:06.52] OK. All right, so let's go ahead and jump into our code and show you all of these
concepts in details. All right, so let's go ahead and define our list. So I'm going to say
my_companies equals to, I opened square brackets, and then here I specify Apple, Samsung,
Alibaba, Novo Nordisk, Naspers. And then to run or execute this, you just press Shift and Enter
at the same time on your keyboard. And then you will end up with the list here. OK?

[00:06:37.96] Next, if I would like to check out the data type for my companies, if I say type,
and you open parentheses, and then you say my_companies, and then you run the SEM, then you
will get the type. And that is going to be lists. Please note that up until this moment, we know
about integers data types. We also learned about notes. We covered strings. And right now, we
introduced a list.

[00:07:08.09] Next, I can also define a list that contains multiple integers and floating points as
well. So let's assume that I wanted to obtain a list that contains all the revenues from the
previously mentioned companies in 2021. And I want to list them as well, in billions of dollars.
So for example, I wanted to get the annual revenue for Apple in 2021, and then the annual
revenue for Samsung in 2021, Alibaba, and so on. To do that, I'm going to define another list. I'm
going to call it my_companies_revenues equals to-- I open square brackets. And then I listed the
revenue for Apple. And that was $365 billion.
[00:07:56.63] And please note that these numbers are just an approximated numbers. Samsung is
around $240 billion. Alibaba is $109 billion. And then Novo Nordisk is $22 billion. Naspers is
$5.9 billion. So to run or execute the SEM, you just press Shift and Enter. And you should get
the other list that is going to be my company's revenues. OK.

[00:08:18.17] Now, let's go ahead and show you how we can access elements within a specific
list. So for example, if I have again, my_companies, which is listing all the companies I'm
interested in-- if I say my_companies and open a square bracket, and then I obtain 0, and you run
the SEM, you will get the element in the index of 0, which is Apple.

[00:08:41.57] I want you to go ahead and change that number and maybe put let's say, number 2
for example, and press Shift-Enter. And then you end up with Alibaba. And that's the element in
the third location, because we start with index 0, 1, and then 2.

[00:08:59.00] Let me show you list slicing. As I mentioned before, we use the column operator
to slice specific elements from a Python list. So if I say my_companies, and then I open square
brackets, and then I say 1 colon 4, and you run the SEM, basically what you get, you will get
elements starting from index 1 up until, but not including index 4. So you get 1, 2, and 3. And
that's it. And that's why here I got Samsung, Alibaba, and then Novo Nordisk afterwards.

[00:09:32.51] Well, what if we wanted maybe to obtain elements starting from index 2 up until
the end of the list? The syntax to do that is, you say my_companies, square brackets, you specify
the first index, which is 2, and then you add colon. And that's it. So basically when you say
colon, that means everything. Just get me elements starting from index 2 and everything
onwards. Just get me the rest of the list.

[00:10:02.09] You press Shift and Enter. Here we go. You got Alibaba, and then you got
everything afterwards. Here, is index 0, 1, 2. So I got Alibaba and then I got everything
afterwards. OK? All right.

[00:10:16.10] And then finally, if I wanted to obtain or print out all the elements from start to
end, you just say my_companies, of colon, and again, if you press Shift-Enter, you will get all
the elements in the list. And if you would like to obtain the length of the elements in my list, like
how many elements in total, you can just say len, which stands for length, of my_companies.
And you press Shift-Enter. And then you will end up with five, because I have five elements
contained in my Python list.

[00:10:46.61] And that's it. That's all I have for this lesson. I hope you enjoyed it. In the next
lesson, I'm going to show you the practice opportunity along with the solution of the practice
opportunities as well. Please stay tuned. Best of luck. And I'll see you in the next lesson.

[00:11:00.41] [AUDIO LOGO]

You might also like