Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Jordi Riera
● Software developer @ Rodeo FX
● Founder @ cgstudiomap.org
● 8 ans à écrire du python
● kender.jr@gmail.com
●...
How To Train Your Python
Les bases sur les iterables
Iterables
+ de 10 types d’iterables
liste, et pas que
● List & deque
● Tuple & namedtuple
● String
● Set & frozenset
● Dict, Ordereddict, ChainMap, Counter & ...
Lequel choisir?
Lequel choisir?
● List, deque, tuple, string, generators, Ordereddict: ordonnés
● Tuple, frozenset: immuables (well... kin...
for loop
languages = [‘php’, ‘ruby’, ‘python’]
frameworks = [‘symfony’, ‘ruby on rails’, ‘django’]
for i in range(len(lang...
for loop
languages = [‘php’, ‘ruby’, ‘python’]
frameworks = [‘symfony’, ‘ruby on rails’, ‘django’]
for i, language in enum...
Set
random_numbers = [ 3, 4, 4, 1, 2, 3, 1]
set(random_numbers)
>>> {1, 2, 3, 4}
frozenset(random_numbers)
>>> frozenset({...
Set
Sets acceptent les
opérations mathématiques:
● Union
● Intersection
● Difference
● Et d’autres opérations
plus chelou,...
dict, feel the powa!
mapping = dict() # ou {}
for language, framework in zip(languages, frameworks):
if not language in languages:
mapping[lang...
mapping = defaultdict(list)
for language, framework in zip(languages, frameworks):
mapping[language].append(framework)
map...
dict
mapping = {‘php’:[‘symfony’], ‘ruby’:[’ruby on rails’],
‘python’:[’django’]}
for language in mapping:
print(language)...
dict
mapping = {‘php’:[‘symfony’], ‘ruby’:[’ruby on rails’],
‘python’:[’django’]}
print(mapping[‘python’])
del mapping[‘py...
muabilité
# Les listes sont muables. Elles peuvent être mis à jour:
list1 = [1,]
list1.append(2)
list1 == [1, 2]
Et d’autr...
muabilité
# Cool mais...
list2 = list1
list1.append(3)
list2 == [1, 2, 3]
# list2 “pointe” vers list1.
# list1 et list2 so...
muabilité
# Solution
list2 = list1[:]
# list2 est une liste avec
tous les éléments de list1
# you’re welcome ;)
# Les tuples sont immuables,
# ils ne peuvent pas être mis à jour:
tuple1 = (1,)
tuple1.append(2)
Raise AttributeError
imm...
# enfin...
list1 = [1,]
tuple1 = (1, list1)
list1.append(2)
tuple1 == (1, [1, 2])
# pas cool bro!
immuabilité
Compréhension à la portée de tous!
list1 = [x for x in z if not x == ‘foo’]
gen = (x for x in z if not x == ‘foo’)
# Nope ...
Questions?
● kender.jr@gmail.com
● @jordiriera_cg
● https://www.linkedin.com/in/jordirieracg/
● https://github.com/foutouc...
Upcoming SlideShare
Loading in …5
×

How to train your python: iterables (FR)

320 views

Published on

Les iterables de python3 101: Slides axées sur les itérables (list, tuple, dict, etc.) décrivant de bonnes pratiques pour développeurs python débutants

Published in: Software
  • Be the first to comment

  • Be the first to like this

How to train your python: iterables (FR)

  1. 1. Jordi Riera ● Software developer @ Rodeo FX ● Founder @ cgstudiomap.org ● 8 ans à écrire du python ● kender.jr@gmail.com ● @jordiriera_cg ● https://www.linkedin.com/in/jordirieracg/ ● https://github.com/foutoucour/
  2. 2. How To Train Your Python Les bases sur les iterables
  3. 3. Iterables + de 10 types d’iterables
  4. 4. liste, et pas que ● List & deque ● Tuple & namedtuple ● String ● Set & frozenset ● Dict, Ordereddict, ChainMap, Counter & defaultdict ● Generators ● Range, zip, map, file object, et autres.
  5. 5. Lequel choisir?
  6. 6. Lequel choisir? ● List, deque, tuple, string, generators, Ordereddict: ordonnés ● Tuple, frozenset: immuables (well... kind of... :( ) ● Set & frozenset: caractère unique ● Dict, Ordereddict, ChainMap, Counter & defaultdict: mapping ● String: ... ben... string quoi... ● Generators, range, zip, map, etc: optimisation, consommation
  7. 7. for loop languages = [‘php’, ‘ruby’, ‘python’] frameworks = [‘symfony’, ‘ruby on rails’, ‘django’] for i in range(len(languages)): print(languages[i] + ‘: ’ + frameworks[i])
  8. 8. for loop languages = [‘php’, ‘ruby’, ‘python’] frameworks = [‘symfony’, ‘ruby on rails’, ‘django’] for i, language in enumerate(languages): print(‘: ’.join([language, frameworks[i]])) for language, framework in zip(languages, frameworks): print(‘: ’.join([language, framework]))
  9. 9. Set random_numbers = [ 3, 4, 4, 1, 2, 3, 1] set(random_numbers) >>> {1, 2, 3, 4} frozenset(random_numbers) >>> frozenset({1, 2, 3, 4})
  10. 10. Set Sets acceptent les opérations mathématiques: ● Union ● Intersection ● Difference ● Et d’autres opérations plus chelou, mais ça fait de jolies figures
  11. 11. dict, feel the powa!
  12. 12. mapping = dict() # ou {} for language, framework in zip(languages, frameworks): if not language in languages: mapping[language] = [] mapping[language].append(framework) dict
  13. 13. mapping = defaultdict(list) for language, framework in zip(languages, frameworks): mapping[language].append(framework) mapping = {} for l, framework in zip(languages, frameworks): mapping.setdefault(l, []).append(framework) dict
  14. 14. dict mapping = {‘php’:[‘symfony’], ‘ruby’:[’ruby on rails’], ‘python’:[’django’]} for language in mapping: print(language) for language, frameworks in mapping.items(): print(language, frameworks)
  15. 15. dict mapping = {‘php’:[‘symfony’], ‘ruby’:[’ruby on rails’], ‘python’:[’django’]} print(mapping[‘python’]) del mapping[‘python’] print(mapping.get(‘python’, ‘flask’)) Mapping[‘python’] = ‘pyramid’
  16. 16. muabilité # Les listes sont muables. Elles peuvent être mis à jour: list1 = [1,] list1.append(2) list1 == [1, 2] Et d’autres méthodes... list2 = [1,] list2.insert(0, 2) list2 == [2, 1]
  17. 17. muabilité # Cool mais... list2 = list1 list1.append(3) list2 == [1, 2, 3] # list2 “pointe” vers list1. # list1 et list2 sont la même liste en fait...
  18. 18. muabilité # Solution list2 = list1[:] # list2 est une liste avec tous les éléments de list1 # you’re welcome ;)
  19. 19. # Les tuples sont immuables, # ils ne peuvent pas être mis à jour: tuple1 = (1,) tuple1.append(2) Raise AttributeError immuabilité
  20. 20. # enfin... list1 = [1,] tuple1 = (1, list1) list1.append(2) tuple1 == (1, [1, 2]) # pas cool bro! immuabilité
  21. 21. Compréhension à la portée de tous! list1 = [x for x in z if not x == ‘foo’] gen = (x for x in z if not x == ‘foo’) # Nope c’est pas un tuple! Mais un générateur. set1 = {x for x in z if not x == ‘foo’} dic1 = {x: x.bar for x in z if not x == ‘foo’}
  22. 22. Questions? ● kender.jr@gmail.com ● @jordiriera_cg ● https://www.linkedin.com/in/jordirieracg/ ● https://github.com/foutoucour/

×