Introduction-to-Python-Chapter-1-2-Python-Lists
Introduction-to-Python-Chapter-1-2-Python-Lists
Introduction to Python
Python Data Types
• float - real numbers
• int - integer numbers
• str - string, text
• bool - True, False
height = 1.73
tall = True
height1 = 1.73
height2 = 1.68
height3 = 1.71
height4 = 1.89
• Inconvenient
Python List
• [a, b, c]
[1.73, 1.68, 1.71, 1.89]
fam
[1.73, 1.68, 1.71, 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]]
List type
type(fam)
list
type(fam2)
list
• Specific functionality
• Specific behavior
Questions?
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
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
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
List slicing
fam
fam[3:5]
[1.68, 'mom']
fam[1:4]
fam[:4]
fam[5:]
Hugo Bowne-Anderson
Data Scientist at DataCamp
List Manipulation
• Change list elements
• Add list elements
• Remove list elements
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]
del(fam[2])
fam
['lisa', 1.74, 1.68, 'mom', 1.71, 'dad', 1.86]
Behind the scenes (1)
x = ["a", "b", "c"]
y = x
y[1] = "z"
y
['a', 'z', 'c']
x
['a', 'z', 'c']
Behind the scenes (1)
x = ["a", "b", "c"]
Behind the scenes (1)
x = ["a", "b", "c"]
y = x
Behind the scenes (1)
x = ["a", "b", "c"]
y = x
y[1] = "z"
Behind the scenes (1)
x = ["a", "b", "c"]
y = x
y[1] = "z"
y
['a', 'z', 'c']
x
['a', 'z', 'c']
Behind the scenes (2)
x = ["a", "b", "c"]
Behind the scenes (2)
x = ["a", "b", "c"]
y = list(x) # or
y = x[:]
Behind the scenes (2)
x = ["a", "b", "c"]
y = list(x) # or
y = x[:]
y[1] = "z"
y
['a', 'z', 'c']
x
['a', 'b', 'c']
Let's practice!
INTRODUCTION TO PYTHON