int
float
double
NOTE:
int
can be converted to a Python float
without loss of information
# in repl
>>> 2**53
#=> 9007199254740992
>>> float(2**53)
#=> 9007199254740992.0
>>> float(2**53 + 1) # loss of precision here
#=> 9007199254740992.0
>>> float(2**53 + 2) # but this is correct
#=> 9007199254740994.0
>>> float(2**53 + 3) # loss of precision here again
#=> 9007199254740996.0
>>> float(2**53 + 4) # but then this is correct
#=> 9007199254740996.0
For more, suggested reading: "What Every Computer Scientist Should Know About Floating-Point Arithmetic" by David Goldberg
decimal
ModuleDecimal
class
fractions
ModuleFraction
class
0.1
), then you may not get the result you wantDecimal
>>> from fractions import Fraction
>>> two_thirds = Fraction(2, 3)
>>> two_thirds
#=> Fraction(2, 3)
>>> four_fifths = Fraction(4, 5)
>>> four_fifths
Fraction(4, 5)
>>> Fraction(0.5)
#=> Fraction(1, 2)
>>> Fraction(0.1)
#=> Fraction(3602879701896397, 3602879701896398)
>>> Fraction(Decimal('0.1'))
#=> Fraction(1, 10)
>>> Fraction('22/7')
#=> Fraction(22, 7)
>>> Fraction(2, 3) + Fraction(4, 5)
#=> Fraction(22, 15)
>>> Fraction(2, 3) - Fraction(4, 5)
#=> Fraction(-2, 15)
>>> Fraction(2, 3) * Fraction(4, 5)
#=> Fraction(8, 15)
>>> Fraction(2, 3) / Fraction(4, 5)
#=> Fraction(5, 6)
>>> Fraction(2, 3) // Fraction(4, 5)
#=> 0
>>> Fraction(2, 3) % Fraction(4, 5)
#=> Fraction(2, 3)
cmath
Module{/* course link */}
TODO 🙃
abs()
# in repl
>>> abs(-5)
#=> 5
>>> abs(-5.0)
#=> 5.0
>>> abs(Decimal(-5))
#=> Decimal('5')
>>> abs(Fraction(-5, 1))
#=> Fraction(5, 1)
>>> abs(complex(0, -5))
#=> 5.0
round()
complex
float
values (since they can't be represented exactly in binary)# in repl
>>> round(0.2812, 3)
#=> 0.281
>>> round(0.625, 1)
#=> 0.6
### equally close alternatives
>>> round(1.5)
#=> 2
>>> round(2.5)
#=> 2
>>> round(Decimal('3.25'), 1)
#=> Decimal('3.2')
>>> round(Fraction(57, 100), 2)
#=> Fraction(57, 100)
>>> round(Fraction(57, 100), 1)
#=> Fraction(3, 5)
>>> round(Fraction(57, 100), 0)
#=> Fraction(1, 1)
TODO 🙃
datetime
Moduledate
: Gregorian Calendar date (assumes proleptic Gregorian calendar that extends into the infinite past and future)
year
, month
, day
time
: time within ideal day, ignoring leap seconds
hour
, minute
, second
, microsecond
datetime
: composite of date
and time
year
, month
, day
, hour
, minute
, second
, microsecond
time
and datetime
can be used in so-called "naïve" or "aware" modes
tzinfo
and concrete timezone
timedelta
: duration expressing difference between 2 date or datetime instances
days
, seconds
, microseconds
, milliseconds
, minutes
, hours
, weeks
days
, seconds
, microseconds
timezone
and tzinfo
# in repl
>>> import datetime
>>>
>>> cet = datetime.timezone(datetime.timedelta(hours=1), "CET")
>>> cet
#=> datetime.timezone(datetime.timedelta(0, 3600), 'CET')
>>>
>>> departure = datetime.datetime(year=2014, month=1, day=7,
... hour=11, minute=30, tzinfo=cet)
>>>
>>> arrival = datetime.datetime(year=2014, month=1, day=7,
... hour=13, minute=5,
... tzinfo=datetime.timezone.utc)
>>> arrival - departure
#=> datetime.datetime(0, 9300)
>>> str(arrival - departure)
#=> '2:35:00'
Important takeaway: use Fraction
for handling numbers in complex, precise computations