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}