Python - Документация
Типы
Числовые типы
int (целые числа)
a = 10 # целое число b = -256 # отрицательное целое c = 0b1010 # двоичное число (10) d = 0o12 # восьмеричное число (10) e = 0xAF # шестнадцатеричное число (175)
float (вещественные числа)
x = 3.14 # обычное число с плавающей точкой y = -0.001 # отрицательное число z = 1.2e3 # экспоненциальная запись (1200.0)
complex (комплексные числа)
c1 = 2 + 3j # комплексное число c2 = complex(1, -2) # альтернативный способ создания
Строковые типы
str (строки)
text1 = 'Hello' # одинарные кавычки text2 = "World" # двойные кавычки text3 = '''Многострочный текст''' text4 = f"Имя: {name}" # f-строка text5 = r'C:\path' # сырая строка
Булевый тип
is_true = True is_false = False
Коллекции
list (списки)
numbers = [1, 2, 3, 4, 5] mixed = [1, 'text', 3.14]
tuple (кортежи)
point = (10, 20) colors = ('red', 'green', 'blue')
dict (словари)
person = {'name': 'Анна', 'age': 25} settings = dict(theme='dark', font_size=14)
set (множества)
unique_numbers = {1, 2, 3, 4, 5} letters = set('hello') # {'e', 'h', 'l', 'o'}
Байтовые типы
bytes data = b'Hello' binary = bytes([65, 66, 67]) # ABC в байтах bytearray arr = bytearray(b'Hello') arr[0] = 72 # можно изменять memoryview mem = memoryview(b'Hello')
Специальные типы
NoneType result = None Ellipsis ellipsis = ...
Примеры операций с типами:
# Преобразование типов
num = int('123') # 123 float_num = float('3.14') # 3.14 text = str(123) # '123'
# Проверка типа
isinstance(10, int) # True type('Hello') == str # True
# Математические операции
a = 5 + 3 # 8 b = 10 / 2 # 5.0 c = 2 ** 3 # 8
# Операции со строками
text = 'Hello' + 'World' # 'HelloWorld' length = len(text) # 10
# Работа со списками
numbers = [1, 2, 3] numbers.append(4) # [1, 2, 3, 4]
# Работа со словарями
person = {'name': 'Анна'} person['age'] = 25 # {'name': 'Анна', 'age': 25}
Логика
(VotingAchievement.uuid == nomination_id) if nomination_id else True ~ VotingAchievement.uuid
SQLAlchemy
nominations = await self._session.execute( select(VotingAchievement).where( ~VotingAchievement.is_deleted, VotingAchievement.is_published, (VotingAchievement.uuid == nomination_id) if nomination_id else True, ).order_by(VotingAchievement.position).options( selectinload(VotingAchievement.icon), selectinload(VotingAchievement.preview) ) ) for nomination in nominations.scalars().all(): ...
result = [] for nomination in nominations.scalars().all(): query = select( ExponentVotesAggregation.uuid, ExponentVotesAggregation.title_ru, ExponentVotesAggregation.title_en, func.sum(ExponentVotesAggregation.count).label("count") ).where( ExponentVotesAggregation.created_at >= since, ExponentVotesAggregation.created_at <= till, ExponentVotesAggregation.nomination_id == nomination.uuid, ).group_by( ExponentVotesAggregation.uuid, ExponentVotesAggregation.title_ru, ExponentVotesAggregation.title_en, ).order_by(desc(text("count"))) exponents = await self._session.execute( query ) result.append((nomination, exponents.all(),))
Ссылки
2024/04/05 15:44 | linkbacks Linkbacks | python, alembic | ||
2024/05/20 12:34 | linkbacks Linkbacks | languages, python, book, pip, python-setuptools, easy install | ||
2024/05/26 22:49 | linkbacks Linkbacks | languages, python, book, pip, python-setuptools, easy install | ||
2015/10/13 09:01 | linkbacks Linkbacks | languages, python, book, pip, python-setuptools, easy install | ||
2024/05/26 22:53 | linkbacks Linkbacks | languages, python, book, pip, python-setuptools, easy install | ||
2024/05/01 11:14 | linkbacks Linkbacks | languages, python, book, pip, python-setuptools, easy install, smtplib, email | ||
2016/01/31 14:41 | linkbacks Linkbacks | languages, python, book, pip, python-setuptools, easy install, smtplib, email | ||
2015/11/26 23:14 | linkbacks Linkbacks | service, unix, linux, python, supervisor, initd |