# Стандарты написания кода

* В каждом языке программирования есть свой стандарт написания кода — набор правил, который рекомендовано соблюдать
* Стандарт, принятый в Python, называется [**PEP 8** ](https://pep8.org/)

Посмотрите на *примеры кода.* Попробуйте предположить, что делает функция:

<details>

<summary>Первый  пример</summary>

```python
def s(i,d,s):
 t=0
 for j in i:
  t+=j
 if d:
  t-=(t*s)
 return t
```

</details>

<details>

<summary>Второй пример</summary>

```python
def calculate_total_cost(items, discount, size_of_discount):
    total = 0
    for item_cost in items:
        total += item_cost
    if discount:
        total -= (total * size_of_discount)
    return total
```

</details>

*Основные моменты:*

* 4 пробела в качестве отступа. Не используйте табуляцию.
* Именуйте функции и переменные в стиле "snake\_case"
* Соблюдайтедлину строки 79 символов, длину комментариев 72 символа
* Вокруг операторов ставьте пробелы. Кроме присвоения для аргументов функций — тогда пробел вокруг "=" не ставится
* Используйте пропуск строки для отделения смысловых частей кода
* Используйте пропуск в две строки между функциями

*Несколько примеров:*&#x20;

```python
# YES — ДЕЛАЙТЕ ТАК:
spam(ham[1], {eggs: 2})
spam(1)
x = 1
y = 2
long_variable = 3
#----------
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
#----------
def complex(real, imag=0.0):
    return magic(r=real, i=imag)

#NO — ВНИМАНИЕ (!) — ТАК НЕ РЕКОМЕНДОВАНО ДЕЛАТЬ:
spam( ham[ 1 ], { eggs: 2 } )
spam (1)
x             = 1
y             = 2
long_variable = 3
#----------
i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
#----------
def complex(real, imag = 0.0):
    return magic(r = real, i = imag)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timosii.gitbook.io/py_tutorial/praktika-programmirovaniya/standarty-napisaniya-koda.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
