Lijsten in Python

Introductie tot Python voor Finance

Adina Howe

Instructor

Lijsten - rechte haken [ ]

months = ['January', 'February', 'March', 'April', 'May', 'June']
Introductie tot Python voor Finance

Python telt vanaf nul

lijst_illustratie

Introductie tot Python voor Finance

Lijst-elementen selecteren

months = ['January', 'February', 'March', 'April', 'May', 'June']
months[0]
'January'
months[2]
'March'
Introductie tot Python voor Finance

Negatieve indexering van lijsten

months = ['January', 'February', 'March', 'April', 'May', 'June']
months[-1]
'June'
months[-2]
'May'
Introductie tot Python voor Finance

Meerdere lijst-elementen subsetten met slicing

Slicing-syntaxis

# Inclusief start en tot (maar exclusief) einde
mylist[startAt:endBefore] 

Voorbeeld

months = ['January', 'February', 'March', 'April', 'May', 'June']
months[2:5]
['March', 'April', 'May']
months[-4:-1]
['March', 'April', 'May']
Introductie tot Python voor Finance

Uitgebreid slicen met lijsten

months = ['January', 'February', 'March', 'April', 'May', 'June']
months[3:]
['April', 'May', 'June']
months[:3]
['January', 'February', 'March']
Introductie tot Python voor Finance

Slicen met stappen

# Inclusief start en tot (maar exclusief) einde
mylist[startAt:endBefore:step] 
months = ['January', 'February', 'March', 'April', 'May', 'June']
months[0:6:2]
['January', 'March', 'May']
months[0:6:3]
['January', 'April']
Introductie tot Python voor Finance

Laten we oefenen!

Introductie tot Python voor Finance

Preparing Video For Download...