0% found this document useful (0 votes)
9 views26 pages

Python Lists by Hugo Bowne-Anderson

The document introduces Python lists. It explains that lists are used to store multiple values in a single variable. Lists can contain any data type and different data types. Values in lists can be accessed using their integer index or sliced to access subsets. Lists support useful methods like adding and removing elements anywhere in the list as well as changing existing values. The document provides examples of creating, accessing, and manipulating list elements.

Uploaded by

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

Python Lists by Hugo Bowne-Anderson

The document introduces Python lists. It explains that lists are used to store multiple values in a single variable. Lists can contain any data type and different data types. Values in lists can be accessed using their integer index or sliced to access subsets. Lists support useful methods like adding and removing elements anywhere in the list as well as changing existing values. The document provides examples of creating, accessing, and manipulating list elements.

Uploaded by

Sonia REZK
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Lists

IN TR OD U C TION TO P YTH ON

Hugo Bowne-Anderson
Data Scientist at DataCamp
Python Data Types
oat - real numbers

int - integer numbers

str - string, text

bool - True, False

height = 1.73
tall = True

Each variable represents single value

INTRODUCTION TO PYTHON
Problem
Data Science: many data points

Height of entire family

height1 = 1.73
height2 = 1.68
height3 = 1.71
height4 = 1.89

Inconvenient

INTRODUCTION TO PYTHON
Python List
[a, b, c]

[1.73, 1.68, 1.71, 1.89]

[1.73, 1.68, 1.71, 1.89]

fam = [1.73, 1.68, 1.71, 1.89]


fam

[1.73, 1.68, 1.71, 1.89]

Name a collection of values

Contain any type

Contain di erent types

INTRODUCTION TO PYTHON
Python List
[a, b, c]

fam = ["liz", 1.73, "emma", 1.68, "mom", 1.71, "dad", 1.89]


fam

['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]

fam2 = [["liz", 1.73],


["emma", 1.68],
["mom", 1.71],
["dad", 1.89]]
fam2

[['liz', 1.73], ['emma', 1.68], ['mom', 1.71], ['dad', 1.89]]

INTRODUCTION TO PYTHON
List type
type(fam)

list

type(fam2)

list

Speci c functionality

Speci c behavior

INTRODUCTION TO PYTHON
Let's practice!
IN TR OD U C TION TO P YTH ON
Subsetting Lists
IN TR OD U C TION TO P YTH ON

Hugo Bowne-Anderson
Data Scientist at DataCamp
Subsetting lists
fam = ["liz", 1.73, "emma", 1.68, "mom", 1.71, "dad", 1.89]
fam

['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]

fam[3]

1.68

INTRODUCTION TO PYTHON
Subsetting lists
['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]

fam[6]

'dad'

fam[-1]

1.89

fam[7]

1.89

INTRODUCTION TO PYTHON
Subsetting lists
['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]

fam[6]

'dad'

fam[-1] # <-

1.89

fam[7] # <-

1.89

INTRODUCTION TO PYTHON
List slicing
fam

['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]

fam[3:5]

[1.68, 'mom']

fam[1:4]

[1.73, 'emma', 1.68]

INTRODUCTION TO PYTHON
List slicing
fam

['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]

fam[:4]

['liz', 1.73, 'emma', 1.68]

fam[5:]

[1.71, 'dad', 1.89]

INTRODUCTION TO PYTHON
Let's practice!
IN TR OD U C TION TO P YTH ON
Manipulating Lists
IN TR OD U C TION TO P YTH ON

Hugo Bowne-Anderson
Data Scientist at DataCamp
List Manipulation
Change list elements

Add list elements

Remove list elements

INTRODUCTION TO PYTHON
Changing list elements
fam = ["liz", 1.73, "emma", 1.68, "mom", 1.71, "dad", 1.89]
fam

['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]

fam[7] = 1.86
fam

['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.86]

fam[0:2] = ["lisa", 1.74]


fam

['lisa', 1.74, 'emma', 1.68, 'mom', 1.71, 'dad', 1.86]

INTRODUCTION TO PYTHON
Adding and removing elements
fam + ["me", 1.79]

['lisa', 1.74,'emma', 1.68, 'mom', 1.71, 'dad', 1.86, 'me', 1.79]

fam_ext = fam + ["me", 1.79]


del(fam[2])
fam

['lisa', 1.74, 1.68, 'mom', 1.71, 'dad', 1.86]

INTRODUCTION TO PYTHON
Behind the scenes (1)
x = ["a", "b", "c"]

INTRODUCTION TO PYTHON
Behind the scenes (1)
x = ["a", "b", "c"]
y = x
y[1] = "z"
y

['a', 'z', 'c']

['a', 'z', 'c']

INTRODUCTION TO PYTHON
Behind the scenes (1)
x = ["a", "b", "c"]
y = x
y[1] = "z"
y

['a', 'z', 'c']

['a', 'z', 'c']

INTRODUCTION TO PYTHON
Behind the scenes (1)
x = ["a", "b", "c"]
y = x
y[1] = "z"
y

['a', 'z', 'c']

['a', 'z', 'c']

INTRODUCTION TO PYTHON
Behind the scenes (2)
x = ["a", "b", "c"]

INTRODUCTION TO PYTHON
Behind the scenes (2)
x = ["a", "b", "c"]
y = list(x)
y = x[:]

INTRODUCTION TO PYTHON
Behind the scenes (2)
x = ["a", "b", "c"]
y = list(x)
y = x[:]
y[1] = "z"
x

['a', 'b', 'c']

INTRODUCTION TO PYTHON
Let's practice!
IN TR OD U C TION TO P YTH ON

You might also like