From cae876b4c9af1ff766670edd54ec44d7ba027c95 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sun, 8 Feb 2009 15:17:09 -0500 Subject: [PATCH] more work on numbers section --- native-datatypes.html | 72 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 9 deletions(-) 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

-

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'>
+
    +
  1. You can use the type() function to check the type of any value or variable. As you might expect, 1 is an int. +
  2. Adding an int to an int yields an int. +
  3. Adding an 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'>
    -
  1. -
  2. -
  3. -
  4. +
  5. You can explicitly coerce an int to a float by calling the float() function. +
  6. Unsurprisingly, you can also coerce a float to an int by calling int(). +
  7. The int() function will truncate, not round. +
  8. The int() function truncates negative numbers towards 0. It's a true truncate function, not a a floor function. +
  9. Floating point numbers are accurate to 15 decimal places.
  10. Integers can be arbitrarily large.
-

Python 2 had separate types for int and long. The int datatype was limited by sys.maxint, which varied by platform but was usually 232-1. Python 3 has just one integer type, which behaves mostly like the old long type from Python 2. +

Python 2 had separate types for int and long. The int datatype was limited by sys.maxint, which varied by platform but was usually 232-1. Python 3 has just one integer type, which behaves mostly like the old long type 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
+
+
    +
  1. The / operator performs floating point division. It returns a float even if both the numerator and denominator are ints. +
  2. The // 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. +
  3. When integer-dividing negative numbers, the // 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. +
  4. The // 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. +
  5. The ** operator means “raised to the power of.” 112 is 121. +
  6. The % 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

Lists

FIXME

Sets

@@ -206,6 +249,17 @@ samp class="prompt">>>> a_dict >>> x == y True +
+

Further reading (FIXME)

+ +

© 2001-4, 2009 ark Pilgrim, CC-BY-3.0