From d03d7244fea91a821830dad4d936d55b8661293a Mon Sep 17 00:00:00 2001 From: Yesenya Date: Wed, 22 Nov 2023 23:56:55 +0300 Subject: [PATCH] add new info to sections --- docs/python/defolt_python.md | 6 +++++- docs/python/dictionaries.md | 20 ++++++++++++++++++- docs/python/lists.md | 6 ++++++ .../stepik/stepik_tasks/update_dictionary.py | 2 -- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/docs/python/defolt_python.md b/docs/python/defolt_python.md index ea74f7f..1364156 100644 --- a/docs/python/defolt_python.md +++ b/docs/python/defolt_python.md @@ -837,4 +837,8 @@ list(string) ИЛИ string.split() -``` \ No newline at end of file +``` +> Записать так, чтобы регистр всех букв при вводе был `lower(or upper)`, а каждое отдельное слово было отдельным элементом: + ```python + text = input().lower().split() + ``` \ No newline at end of file diff --git a/docs/python/dictionaries.md b/docs/python/dictionaries.md index fd5d6b1..6ccb343 100644 --- a/docs/python/dictionaries.md +++ b/docs/python/dictionaries.md @@ -16,9 +16,16 @@ dict[key] # венет значение по ключу dict.get(key) # вернет None, если не найдет значение del dict[key] # удаление всех эл-тов в ключе и самого ключа ``` +- Чтобы добавить значение в словарь, если он пустой: +```python +d = {} +d[key] = [value] # Добавит ключ с значением, вместо значения можно использовать [], если значение без скобок value, то меняться будет сам элемент (смотри ниже с += ) +d[key] += [value] # Добавит value в конец списка с [value], если не будет списка, то ошибка или изменит значение +``` +- удаление ключа ```python del.price['toyota'] #удаление переменной price -price.clear() #удаление ключей в перменной price +price.clear() #удаление ключей в переменной price ``` - Замена ключей в нескольких словарях ```python @@ -53,6 +60,12 @@ __`.items()`__ - и ключ и значения for items in dict.items(): print(item) -> ('key1','value1') ('key2','value2') + +OR + +for key, items in dict.items(): + print (key,items) -> key items + key items ``` __`.keys()`__ - только ключи ```python @@ -84,3 +97,8 @@ print (num_dict) -> {6:36,43:1849,-2:4...} num_dict = {number: ('positive' if number >0),(else 'negative' if number <0),(else'zero') for number in list} print (num_dict) -> {6:'positive', 43:'positive',-2:'negative'...} ``` +### Особенности +- Записать так, чтобы регистр всех букв при вводе был `lower(or upper)`, а каждое отдельное слово было отдельным элементом: + ```python + text = input().lower().split() + ``` diff --git a/docs/python/lists.md b/docs/python/lists.md index 04f2419..8a0642a 100644 --- a/docs/python/lists.md +++ b/docs/python/lists.md @@ -121,3 +121,9 @@ for inner_list in list: ``` - +## Особенности +- Для списков или картежей можно использовать `x.count()`: + ```python + text = [1,2,3,1,2] + print(text.count(1)) -> 2 # Элемент 1 встречается 2 раза + ``` \ No newline at end of file diff --git a/practica/stepik/stepik_tasks/update_dictionary.py b/practica/stepik/stepik_tasks/update_dictionary.py index 2552364..3d45fe9 100644 --- a/practica/stepik/stepik_tasks/update_dictionary.py +++ b/practica/stepik/stepik_tasks/update_dictionary.py @@ -9,5 +9,3 @@ def update_dictionary(d,key,value): d[new_key] += [value] else: d[key] += [value] - -