Почему при делении целого автоматически получается float?

Рейтинг: 3Ответов: 1Опубликовано: 28.06.2023

Например:

a = int(input()) 
рrint(a/3)

И без разницы, делится нацело или с остатком.

Ответы

▲ 7

Оператор / в третьем Питоне всегда возвращает вещественное число. Такое правило. Используйте // для деления нацело.

6.7. Binary arithmetic operations

The / (division) and // (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result. Division by zero raises the ZeroDivisionError exception.