Returns the nearest whole number less than or equal to the given number.
import math
math.floor(3.5) # 3
math.floor(3.9) # 3
math.floor(-3.5) # -4
math.floor(-2.8) # -3
โ
trunc()
Cuts off the decimal part and moves the number towards zero.
math.trunc(2.8) # 2
math.trunc(-2.8) # -2
๐ Number Conversion
โค Binary
bin(2) # '0b10'
int('10', 2) # 2
โค Octal
oct(64) # '0o100'
int('100', 8) # 64
โค Hexadecimal
hex(64) # '0x40'
int('40', 16) # 64
๐ข Decimal
Used for exact decimal values (like money).
from decimal import Decimal
Decimal('0.1') + Decimal('0.2') # Decimal('0.3')
โ Fraction
Used for exact fractions (no float errors).
from fractions import Fraction
Fraction(1, 3) # Fraction(1, 3)
Fraction('3/4') # Fraction(3, 4)
๐ฐ Set
Used to store unique items. Removes duplicates.
a = {1, 2, 2, 3} # {1, 2, 3}
{1, 2} | {2, 3} # Union โ {1, 2, 3}
{1, 2} & {2, 3} # Intersection โ {2}
{1, 2, 3} - {2} # Difference โ {1, 3}