diff --git a/native-datatypes.html b/native-datatypes.html index 21fd725..3591a1d 100755 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -28,6 +28,7 @@ body{counter-reset:h1 2}
Booleans are either true or false. Python has two constants, cleverly True and False, which can be used to assign boolean values directly. Expressions can also evaluate to a boolean value. In certain places (like if statements), Python expects an expression to evaluate to a boolean value. These places are called boolean contexts. You can use virtually any expression in a boolean context, and Python will try to determine its truth value. Different datatypes have different rules about which values are true or false in a boolean context. (This will make more sense once you see some concrete examples later in this chapter.)
+
Booleans are either true or false. Python has two constants, cleverly True and False, which can be used to assign boolean values directly. Expressions can also evaluate to a boolean value. In certain places (like if statements), Python expects an expression to evaluate to a boolean value. These places are called boolean contexts. You can use virtually any expression in a boolean context, and Python will try to determine its truth value. Different datatypes have different rules about which values are true or false in a boolean context. (This will make more sense once you see some concrete examples later in this chapter.)
For example, take this snippet from humansize.py:
if size < 0:
raise ValueError('number must be non-negative')
@@ -69,7 +70,7 @@ ZeroDivisionError: int division or modulo by zero
⁂
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. +
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'> @@ -213,7 +214,7 @@ ZeroDivisionError: Fraction(0, 0)
⁂
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. +
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.
@@ -440,6 +441,66 @@ IndexError: pop from empty list☞A list in Python is like an array in Perl 5. In Perl 5, variables that store arrays always start with the
@character; in Python, variables can be named anything, and Python keeps track of the datatype internally.
⁂ +
A tuple is an immutable list. A tuple can not be changed in any way once it is created. + +
+>>> a_tuple = ("a", "b", "mpilgrim", "z", "example") ① +>>> a_tuple +('a', 'b', 'mpilgrim', 'z', 'example') +>>> a_tuple[0] ② +'a' +>>> a_tuple[-1] ③ +'example' +>>> a_tuple[1:3] ④ +('b', 'mpilgrim')+
a_tuple[0].
+The major difference between tuples and lists is that tuples can not be changed. In technical terms, tuples are immutable. In practical terms, they have no methods that would allow you to change them. Lists have methods like append(), extend(), insert(), remove(), and pop(). Tuples have none of these methods. You can slice a tuple (because that creates a new tuple), and you can check whether a tuple contains a particular value (because that doesn’t change the tuple), and… that’s about it.
+
+
+# continued from the previous example
+>>> a_tuple
+('a', 'b', 'mpilgrim', 'z', 'example')
+>>> a_tuple.append("new") ①
+Traceback (innermost last):
+ File "<interactive input>", line 1, in ?
+AttributeError: 'tuple' object has no attribute 'append'
+>>> a_tuple.remove("z") ②
+Traceback (innermost last):
+ File "<interactive input>", line 1, in ?
+AttributeError: 'tuple' object has no attribute 'remove'
+>>> a_tuple.index("example") ③
+4
+>>> "z" in a_tuple ④
+True
+append() or extend() method.
+remove() or pop() method.
+in operator to check if an element exists in the tuple.
+So what are tuples good for?
+ +assert statement that shows this data is constant, and that special thought (and a specific function) is required to override that.
+++ +☞Tuples can be converted into lists, and vice-versa. The built-in
tuple()function takes a list and returns a tuple with the same elements, and thelist()function takes a tuple and returns a list. In effect,tuple()freezes a list, andlist()thaws a tuple. +
⁂ +
A set is an unordered “bag” of unique values. A single set can contain values of any datatype. Once you have a two sets, you can do standard set operations like union, intersection, and set difference. @@ -493,7 +554,7 @@ IndexError: pop from empty list <class 'dict'>
set() with no arguments.
-{}, perhaps? That would denote an empty dictionary, not an empty set. (You’ll learn about dictionaries later in this chapter.)
+{}, perhaps? That would denote an empty dictionary, not an empty set. You’ll learn about dictionaries later in this chapter.
⁂
One of Python’s most important datatypes is the dictionary. A dictionary is an unordered set of key-value pairs. When you add a key to a dictionary, you must also add a value for that key. (You can always change the value later.) Python dictionaries are optimized for retrieving the value when you know the key, but not the other way around. + +
A dictionary is an unordered set of key-value pairs. When you add a key to a dictionary, you must also add a value for that key. (You can always change the value later.) Python dictionaries are optimized for retrieving the value when you know the key, but not the other way around.
@@ -785,7 +847,7 @@ KeyError: 'db.diveintopython3.org'☞A dictionary in Python is like a hash in Perl 5. In Perl 5, variables that store hashes always start with a
%character. In Python, variables can be named anything, and Python keeps track of the datatype internally.
⁂
NoneNone 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 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) @@ -822,8 +884,13 @@ KeyError: 'db.diveintopython3.org'