0% found this document useful (0 votes)
17 views10 pages

Python Presentation

Uploaded by

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

Python Presentation

Uploaded by

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

List

Data Type
Lists: Data type in
Python
Definition:
Built-in Data type that store
values
(Sequence of elements)
Versatile data type as it can
contains elements of different
data types
Dynamic length (lists are
mutable)
Creation of a Lists

First, let’s have a look at some
examples and then, let’s come up
with a definition:
prices = [1.20, 0.75, 4.50]
somePrimes = [1, 3, 5, 7, 11, 13]
underTheBed = [3, "old socks“]
Accessing element from a list

List indexing: positive indexing


For example:
courses = [“Biology”, “Physics”,
“Chemistry”,”Mathmatics”, “Computer”]

The 1st element of the list, we use the index 0


The 2nd element of the list, we use the index 1
etc…
Index Error ??
“list index out of range” error
occur
List indexing: negative
indexing
There is another way we can use to access one
list element at a time: negative indexing:

The 1st element of the list, we use the index -8


The last element of the list, we use the index -1
etc…
Careful: Negative index starts at -1, not 0
List slicing (using positive
indices)
use indices to indicate the list slice
For example:

Syntax: <list>[start : stop : step ]


Sub_list[1:3] = Physics, Chemistry
Sub-list[ :3] = Biology, Physics,
Chemistry
Sub-list[0:4:2] =biology, Chemistry
List slicing (using positive
indices)
Inclusive -> element at index 0 is
included in the list slice
We use index 2 to indicate the end
of the list slice
Non-inclusive -> element at index 2
is ***not*** included

When we slice out of bound, the


result is an empty list
Lists are mutable!
Definition of mutable: can change
Can we modify a list?
For example, let’s try:

sub-list[2]=“English”
Thank You

You might also like