This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from datetime import date | |
| class Person: | |
| # можна заборонити додавання нових атрибутів на льоту | |
| # __slots__ = ("_name", "_birthday", "_knows_python") # дозволені атрибути | |
| def __init__(self): | |
| self._name = "Микола" | |
| self._birthday = date(1970, 1, 1) | |
| self._knows_python = False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import annotations | |
| from dataclasses import dataclass, field | |
| from datetime import date | |
| from enum import Enum | |
| from random import randint | |
| from typing import List | |
| class EyesColor(Enum): | |
| YELLOW = "Yellow" | |
| BLACK = "Black" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AlwaysNounsNotVerbs: | |
| """сlass documentation comment | |
| опис класу: що він робить, для чого призначений | |
| """ | |
| # 1. класові (статичні) змінні - належать класу, спільні для всіх екземплярів | |
| class_counter = 0 | |
| DEFAULT_TIMEOUT = 30 | |
| def __init__(self, name: str, value: int = 0): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| # декоратор: логування в консоль (через print) | |
| def log_to_console(func): | |
| def wrapper(*args, **kwargs): | |
| print(f"[log console] виклик {func.__name__} | args: {args} | kwargs: {kwargs}") | |
| try: | |
| result = func(*args, **kwargs) | |
| print(f"[log console] {func.__name__} > {result}") | |
| return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # фабрика логерів з ізольованим станом для відстеження статусів | |
| def create_logger(level='INFO', prefix=''): | |
| # приватний журнал повідомлень для цього логера | |
| log_messages = [] | |
| def log(message): | |
| # додаємо повідомлення в журнал і одразу виводимо | |
| nonlocal log_messages | |
| formatted = f"[{level}] {prefix}{message}" | |
| log_messages.append(formatted) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| print("1. Множина імператорів з найяскравішими характерами") | |
| emperors = { | |
| "Гай Юлій Цезар Октавіан Август", | |
| "Тиберій Клавдій Нерон", | |
| "Гай Юлій Цезар Август Германік (Калігула)", | |
| "Нерон Клавдій Цезар Август Германік", | |
| "Марк Аврелій Коммод Антонін", | |
| "Марк Аврелій Север Антонін Бассіан (Каракалла)", | |
| "Гай Аврелій Антонін Геліогабал", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* ========================================================= | |
| DEMO: змінні, умови, цикли, курсори, процедури (MySQL) | |
| ========================================================= */ | |
| /* ---------- очистка ---------- */ | |
| DROP PROCEDURE IF EXISTS demo_all; | |
| DROP PROCEDURE IF EXISTS rectangle; | |
| DROP TABLE IF EXISTS Product; | |
| /* ---------- таблиця ---------- */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| lambda x, y: x + y # з точки зору пайтон, це просто звичайний вираз, як 1 + 2 або "hello", який НЕ вимагає обов’язкового «прив’язування» до імені чи контексту, як у більшості C-подібних мов | |
| print((lambda x: x ** 2)(5)) # 25 (викликається одразу після визначення) | |
| ############################################################### | |
| numbers = [1, 2, 3, 4, 5, 6] | |
| squared = list(map(lambda x: x ** 2, numbers)) # квадрати всіх чисел у списку | |
| evens = list(filter(lambda x: x % 2 == 0, numbers)) # лише парні числа зі списку | |
| print(squared) # [1, 4, 9, 16, 25, 36] | |
| print(evens) # [2, 4, 6] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ──────────────────────────────────────────────────────────────────── | |
| # персонаж у настолці Манчкін — словник з характеристиками та речами | |
| # ──────────────────────────────────────────────────────────────────── | |
| character = { | |
| "level": 1, | |
| "race": "Людина", # початкова раса | |
| "class": "Воїн", | |
| "gender": "чоловіча", | |
| "equipped": ["Рогатий Шолом", "Короткий Меч"], | |
| "big_items": ["Величезна Камінюка"], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| # список кортежів | |
| STORES = [ | |
| (45.351, 28.840, "Сільпо Ізмаїл, вул. Миколи Аркаса"), | |
| (46.482, 30.732, "Сільпо Одеса, Таїровське"), | |
| (50.450, 30.523, "Сільпо Київ, Поділ"), | |
| ] | |
| PRODUCTS = [ |
NewerOlder