diff --git a/about.html b/about.html index 0a60fdc..e654473 100644 --- a/about.html +++ b/about.html @@ -22,6 +22,7 @@ h1:before{content:""}
© 2001–4, 2009 ℳark Pilgrim diff --git a/native-datatypes.html b/native-datatypes.html index 7790453..0c937e5 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -22,12 +22,20 @@ body{counter-reset:h1 2}
None
+
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) ① @@ -115,6 +133,7 @@ body{counter-reset:h1 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.Common numerical operations
You can do all kinds of things with numbers.
>>> 11 / 2 ① @@ -141,7 +160,68 @@ body{counter-reset:h1 2}-☞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 +
Fractions
+Python isn't limited to integers and floating point numbers. It can also do all the fancy math you learned in high school and promptly forgot about. +
+>>> import fractions ① +>>> x = fractions.Fraction(1, 3) ② +>>> x +Fraction(1, 3) +>>> x * 2 ③ +Fraction(2, 3) +>>> fractions.Fraction(6, 4) ④ +Fraction(3, 2)++
+- To start using fractions, import the
fractionsmodule. +- To define a fraction, create a
Fractionobject and pass in the numerator and denominator. +- You can perform all the usual mathematical operations with fractions. Operations return a new
Fractionobject.2 * (1/3) = (2/3)+- The
Fractionobject will automatically reduce fractions.(6/4) = (3/2)+Trigonometry
+You can also do basic trigonometry in Python. +
+>>> import math +>>> math.pi ① +3.1415926535897931 +>>> math.sin(math.pi / 2) ② +1.0 +>>> math.tan(math.pi / 4) ③ +0.99999999999999989++
+- The
mathmodule has a constant for π, the ratio of a circle's circumference to its diameter. +- The
mathmodule has all the basic trigonometric functions, includingsin(),cos(),tan(), and variants likeasin(). +- Note, however, that Python does not have infinite precision.
tan(π / 4)should return1.0, not0.99999999999999989. +Numbers in a boolean context
+You can use numbers in a boolean context, such as an
ifstatement. Zero values are false, and non-zero values are true. ++>>> def is_it_true(anything): ① +... if anything: +... print("yes, it's true") +... else: +... print("no, it's false") +... +>>> is_it_true(1) ② +yes, it's true +>>> is_it_true(-1) +yes, it's true +>>> is_it_true(0) +no, it's false +>>> is_it_true(0.1) ③ +yes, it's true +>>> is_it_true(0.0) +no, it's false +>>> import fractions +>>> is_it_true(fractions.Fraction(1, 2)) ④ +yes, it's true +>>> is_it_true(fractions.Fraction(0, 1)) +no, it's false++
- Did you know you can define your own functions in the Python interactive shell? Just press ENTER at the end of each line, and ENTER on a blank line to finish. +
- In a boolean context, non-zero integers are true;
0is false. +- Non-zero floating point numbers are true;
0.0is false. Be careful with this one! If there's the slightest rounding error (not impossible, as you saw in the previous section) then Python will be testing0.0000000000001instead of0and will returnTrue. +- Fractions can also be used in a boolean context.
Fraction(0, n)is false for all values of n. All other fractions are true. +Lists
Lists are Python's workhorse datatype. When I say “list,” you might be thinking “array whose size I have to declare in advance, that can only contain items of the same type, &c.” Don't think that. Lists are much cooler than that.
@@ -263,6 +343,23 @@ ValueError: list.index(x): x not in list
index() method finds the first occurrence of a value in the list. In this case, 'new' occurs twice in the list, in a_list[2] and a_list[4], but the index() method will return only the index of the first occurrence.
-1). While this may seem annoying at first, I think you will come to appreciate it. It means your program will crash at the source of the problem instead of failing strangely and silently later.
+You can also use a list in a boolean context, such as an if statement.
+
+>>> def is_it_true(anything): ① +... if anything: +... print("yes, it's true") +... else: +... print("no, it's false") +... +>>> is_it_true([]) ② +no, it's false +>>> is_it_true(['a']) ③ +yes, it's true+
len() function gives you the number of items in a dictionary.
1000 is a key in the SUFFIXES dictionary; its value is a list of eight items (eight strings, to be precise).
1024 is a key in the SUFFIXES dictionary; its value is also a list of eight items.
SUFFIXES[1000] is a list, you can address individual items in the list by their 0-based index.
+You can also use a list in a boolean context, such as an if statement.
+
+>>> def is_it_true(anything): ① +... if anything: +... print("yes, it's true") +... else: +... print("no, it's false") +... +>>> is_it_true({}) ② +no, it's false +>>> is_it_true({'a': 1}) ③ +yes, it's true+
NoneNone is a special constant in Python. It is a null value. None is not False; it is not 0; it is not an empty string. Comparing None to anything other than None will always return False.
+
None is a special constant in Python. It is a null value. None is not the same as False. None is not 0. None is not an empty string. Comparing None to anything other than None will always return False.
None is the only null value. It has its own datatype (NoneType). You can assign None to any variable, but you can not create other NoneType objects. All variables whose value is None are equal to each other.
>>> type(None) @@ -357,14 +474,25 @@ KeyError: 'db.diveintopython3.org'>>> x == y True +
None in a boolean contextIn a boolean context, None is false and not None is true.
+
+>>> def is_it_true(anything): +... if anything: +... print("yes, it's true") +... else: +... print("no, it's false") +... +>>> is_it_true(None) +no, it's false +>>> is_it_true(not None) +yes, it's true
fractions module
+math module
+© 2001–4, 2009 ℳark Pilgrim • open standards • open content • open source