###### top ## Python. Заметки на полях [Официальная документация](https://docs.python.org/3/) *** [Базовые понятия](#part1) - [List comprehension](#chapter1) - [lambda](#chapter2) - [Ad-hoc команды](#chapter3) [Ansible playbook](#part2) - [Простой playbook](#chapter4) - [Переменные](#chapter5) - [Отладка](#chapter6) - [Блоки и обработка ошибок](#chapter7) - [Асинхронные задачи](#chapter8) - [](#chapter9) - [](#chapter10) - [](#chapter11) - [](#chapter12) *** ###### part1 ## Базовые понятия [вверх](#top) Интрерпретируемый язык ###### Chapter1 ### List comprehension Представление списков, списковые включения https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions ```python list_square = [x ** 2 for x in range(5)] print(list_square) [0, 1, 4, 9, 16] ``` ```python vlans = [f'vlan {num}' for num in range(10, 16)] print(vlans) ['vlan 10', 'vlan 11', 'vlan 12', 'vlan 13', 'vlan 14', 'vlan 15'] ``` ```python prices = [98, 123, 13, 45, 67, 43, 87] less = [x for x in prices if x < 100] print(less) [98, 13, 45, 67, 43, 87] ``` ```python vlans = [100, 110, 150, 200] names = ['mngmt', 'voice', 'video', 'dmz'] result = ['vlan {}\n name {}'.format(vlan, name) for vlan, name in zip(vlans, names)] print(result) ['vlan 100\n name mngmt', 'vlan 110\n name voice', 'vlan 150\n name video', 'vlan 200\n name dmz'] ``` ```python users = [{'name': 'Иван', 'age': 29}, {'name': 'Андрей', 'age': 31}, {'name': 'Настя', 'age': 22}, {'name': 'Артём', 'age': 40}] users_list = [user['name'] for user in users if user['age'] > 30] print(users_list) ['Андрей', 'Артём'] ``` ```python vlans = [[10, 21, 35], [101, 115, 150], [111, 40, 50]] result = [vlan for vlan_list in vlans for vlan in vlan_list] print(result) [10, 21, 35, 101, 115, 150, 111, 40, 50] ``` [вверх](#top) *** ### lambda ```python list_square = [x ** 2 for x in range(5)] print(list_square) [0, 1, 4, 9, 16] ``` [вверх](#top) ***