diff --git a/native-datatypes.html b/native-datatypes.html index 4b0901d..4db6307 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -88,7 +88,7 @@ body{counter-reset:h1 2} >>> size < 0 True
Numbers are awesome. There are so many to choose from. Python supports both integers and floating point numbers. +
Numbers are awesome. There are so many to choose from. Python supports both integers and floating point numbers. There's no type declaration to distinguish them; Python tells them apart by the presence or absence of a decimal point.
>>> type(1) ① <class 'int'> @@ -97,21 +97,64 @@ body{counter-reset:h1 2} >>> 1 + 1.0 ③ 2.0 >>> type(2.0) -<class 'float'> ->>> 1.12345678901234567890 ④ +<class 'float'>+
type() function to check the type of any value or variable. As you might expect, 1 is an int.
+int to an int yields an int.
+int to a float yields a float. Python coerces the int into a float to perform the addition, then returns a float as the result.
+As you just saw, some operators (like addition) will coerce integers to floating point numbers as needed. You can also coerce them by yourself. +
+>>> float(2) ① +2.0 +>>> int(2.0) ② +2 +>>> int(2.5) ③ +2 +>>> int(-2.5) ④ +-2 +>>> 1.12345678901234567890 ⑤ 1.1234567890123457 ->>> type(1000000000000000) ⑤ +>>> type(1000000000000000) ⑥ <class 'int'>
int to a float by calling the float() function.
+float to an int by calling int().
+int() function will truncate, not round.
+int() function truncates negative numbers towards 0. It's a true truncate function, not a a floor function.
+-+☞Python 2 had separate types for
intandlong. Theintdatatype was limited bysys.maxint, which varied by platform but was usually232-1. Python 3 has just one integer type, which behaves mostly like the oldlongtype from Python 2. +☞Python 2 had separate types for
intandlong. Theintdatatype was limited bysys.maxint, which varied by platform but was usually232-1. Python 3 has just one integer type, which behaves mostly like the oldlongtype from Python 2. See PEP 237 for details.
You can do all kinds of things with numbers. +
+>>> 11 / 2 ① +5.5 +>>> 11 // 2 ② +5 +>>> −11 // 2 ④ +−6 +>>> 11.0 // 2 ③ +5.0 +>>> 11 ** 2 ⑤ +121 +>>> 11 % 2 ⑥ +1 ++
/ operator performs floating point division. It returns a float even if both the numerator and denominator are ints.
+// operator performs a quirky kind of integer division. When the result is positive, you can think of it as truncating (not rounding) to 0 decimal places, but be careful with that.
+// operator rounds “up” to the nearest integer. Mathematically speaking, it's rounding “down” since −6 is less than −5, but it could trip you up if you expecting it to truncate to −5.
+// operator doesn't always return an integer. If either the numerator or denominator is a float, it will still round to the nearest integer, but the actual return value will be a float.
+** operator means “raised to the power of.” 112 is 121.
+% operator gives the remainder after performing integer division. 11 divided by 2 is 5 with a remainder of 1, so the result here is 1.
+++☞In Python 2, the
/operator usually meant integer division, but you could make it behave like floating point division by including a special directive in your code. In Python 3, the/operator always means floating point division. See PEP 238 for details. +
FIXME fractions, math module, numbers in a boolean context
FIXME
© 2001-4, 2009 ℳark Pilgrim, CC-BY-3.0