Listes en Python

Introduction à Python pour la finance

Adina Howe

Professor

Listes – crochets [ ]

months = ['January', 'February', 'March', 'April', 'May', 'June']
Introduction à Python pour la finance

Python compte à partir de zéro

list_figure

Introduction à Python pour la finance

Extraire des éléments d'une liste

months = ['January', 'February', 'March', 'April', 'May', 'June']
months[0]
'January'
months[2]
'March'
Introduction à Python pour la finance

Indexation négative des listes

months = ['January', 'February', 'March', 'April', 'May', 'June']
months[-1]
'June'
months[-2]
'May'
Introduction à Python pour la finance

Extraire plusieurs éléments avec le tranchage

Syntaxe de tranchage

# Inclut le début et va jusqu'à (sans inclure) la fin
mylist[startAt:endBefore] 

Exemple

months = ['January', 'February', 'March', 'April', 'May', 'June']
months[2:5]
['March', 'April', 'May']
months[-4:-1]
['March', 'April', 'May']
Introduction à Python pour la finance

Tranchage avancé des listes

months = ['January', 'February', 'March', 'April', 'May', 'June']
months[3:]
['April', 'May', 'June']
months[:3]
['January', 'February', 'March']
Introduction à Python pour la finance

Tranchage avec pas

# Inclut le début et va jusqu'à (sans inclure) la fin
mylist[startAt:endBefore:step] 
months = ['January', 'February', 'March', 'April', 'May', 'June']
months[0:6:2]
['January', 'March', 'May']
months[0:6:3]
['January', 'April']
Introduction à Python pour la finance

Passons à la pratique !

Introduction à Python pour la finance

Preparing Video For Download...