Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Introduction-to-Python-Chapter-1-2-Python-Lists

The document provides an introduction to Python lists, explaining data types and the advantages of using lists for storing multiple values. It covers list creation, subsetting, slicing, and manipulation techniques, including changing, adding, and removing elements. Additionally, it illustrates how lists behave in memory when assigned to new variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction-to-Python-Chapter-1-2-Python-Lists

The document provides an introduction to Python lists, explaining data types and the advantages of using lists for storing multiple values. It covers list creation, subsetting, slicing, and manipulation techniques, including changing, adding, and removing elements. Additionally, it illustrates how lists behave in memory when assigned to new variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

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

• Each variable represents single value


Problem
• Data Science: many data points
• Height of entire family

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]

[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


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]]
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

['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]


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]


Questions?
Manipulating Lists
INTRODUCTION TO PYTHON

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]

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


fam
['lisa', 1.74, 'emma', 1.68, 'mom', 1.71, 'dad', 1.86]
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]


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

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

You might also like