from typing import List, Dict, Tuple, Set from typing import Union from typing import Optional ''' Docstring - строка документирования. Вызывается: help(abs) abs.__doc__ В docstring попадет только первая строка из начала функции. Слабая и сильная (строгая) типизация. В языках со строгой типизация (Python) с объектами определенных типов можно производить только ограниченный набор действий. В Python динамическая строгая типизация. Аннотация типов в Python foo: float foo: int = 100 В функции содержится атрибут __annotations__ (содержит словарь аннотаций) ''' def get_even(lst) -> list: 'Функция возвращает список из четных чисел списка lst' even_lst = [] for elem in lst: if elem % 2 == 0: even_lst.append(elem) return even_lst def func_1(): numbers: List[int] = [1, 2, 3] languages: Dict = {} temperature: Tuple = (1, 2, 3) letters: Set[str] = set('qwerty') param_1: Union[int, float, bool] param_2: int | float | str def get_first_repeated_word(words: List[str]) -> Optional[str]: 'Находит первый дубль в списке' foo = set() for w in words: if w in foo: return w else: foo.add(w) return None def rotate(lst: list[int | float], shift: int = 1) -> list[int | float] : 'Функция выполняет циклический сдвиг списка на shift позиций вправо(shift>0) или влево(shift<0)' shifted_list = [] if shift > len(lst): foo = shift%len(lst) if shift < 0: foo = - foo - 1 else: foo = shift if shift < 0: shifted_list.extend(lst[-foo:]) shifted_list.extend(lst[:-foo]) elif shift > 0: shifted_list.extend(lst[-foo:]) shifted_list.extend(lst[:-foo]) return shifted_list def main(): # print(get_even.__doc__) # print(get_first_repeated_word.__doc__) # print(get_first_repeated_word.__annotations__) # print(get_first_repeated_word(['ab', 'ca', 'bc', 'ca', 'ab', 'bc'])) print(rotate([1, 2, 3, 4, 5, 6], 2)) print(rotate([1, 2, 3, 4, 5, 6], -10)) print(rotate([1, 2, 3, 4, 5, 6, 7], 21)) print(rotate([1, 2, 3, 4, 5, 6], -3)) if __name__ == '__main__': main()