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

Chapter 2

The document discusses Python lists, including how they can be used to store multiple values, their functionality, and how to manipulate lists through operations like subsetting, slicing, changing elements, and adding/removing elements. It also discusses how lists are represented behind the scenes.

Uploaded by

mohamed farag
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)
15 views26 pages

Chapter 2

The document discusses Python lists, including how they can be used to store multiple values, their functionality, and how to manipulate lists through operations like subsetting, slicing, changing elements, and adding/removing elements. It also discusses how lists are represented behind the scenes.

Uploaded by

mohamed farag
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
You are on page 1/ 26

Python Lists

INTRODUCTION TO PYTHON

Hugo Bowne-Anderson
Data Scientist at DataCamp
Python Data Types
float - 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 different 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

Specific functionality

Specific behavior

INTRODUCTION TO PYTHON
Let's practice!
INTRODUCTION TO PYTHON
Subsetting Lists
INTRODUCTION TO PYTHON

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!
INTRODUCTION TO PYTHON
Manipulating Lists
INTRODUCTION TO PYTHON

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!
INTRODUCTION TO PYTHON

You might also like