diff options
Diffstat (limited to 'python3.html.markdown')
| -rw-r--r-- | python3.html.markdown | 44 | 
1 files changed, 30 insertions, 14 deletions
| diff --git a/python3.html.markdown b/python3.html.markdown index c7fbf342..c4f15867 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -41,10 +41,10 @@ Note: This article applies to Python 3 specifically. Check out [here](http://lea  10 * 2  # => 20  35 / 5  # => 7.0 -# Result of integer division truncated down both for positive and negative. +# Integer division rounds down for both positive and negative numbers.  5 // 3       # => 1 -5.0 // 3.0   # => 1.0 # works on floats too  -5 // 3      # => -2 +5.0 // 3.0   # => 1.0 # works on floats too  -5.0 // 3.0  # => -2.0  # The result of division is always a float @@ -72,15 +72,24 @@ not False  # => True  True and False  # => False  False or True   # => True -# Note using Bool operators with ints -# False is 0 and True is 1 +# True and False are actually 1 and 0 but with different keywords +True + True # => 2 +True * 8    # => 8 +False - 5   # => -5 + +# Comparison operators look at the numerical value of True and False +0 == False  # => True +1 == True   # => True +2 == True   # => False +-5 != False # => True + +# Using boolean logical operators on ints casts them to booleans for evaluation, but their non-cast value is returned  # Don't mix up with bool(ints) and bitwise and/or (&,|) +bool(0)     # => False +bool(4)     # => True +bool(-6)    # => True  0 and 2     # => 0  -5 or 0     # => -5 -0 == False  # => True -2 == True   # => False -1 == True   # => True --5 != False != True #=> True  # Equality is ==  1 == 1  # => True @@ -96,7 +105,10 @@ False or True   # => True  2 <= 2  # => True  2 >= 2  # => True -# Comparisons can be chained! +# Seeing whether a value is in a range +1 < 2 and 2 < 3  # => True +2 < 3 and 3 < 2  # => False +# Chaining makes this look nicer  1 < 2 < 3  # => True  2 < 3 < 2  # => False @@ -302,16 +314,19 @@ valid_dict = {(1,2,3):[1,2,3]}   # Values can be of any type, however.  filled_dict["one"]  # => 1  # Get all keys as an iterable with "keys()". We need to wrap the call in list() -# to turn it into a list. We'll talk about those later.  Note - Dictionary key -# ordering is not guaranteed. Your results might not match this exactly. -list(filled_dict.keys())  # => ["three", "two", "one"] +# to turn it into a list. We'll talk about those later.  Note - for Python +# versions <3.7, dictionary key ordering is not guaranteed. Your results might +# not match the example below exactly. However, as of Python 3.7, dictionary +# items maintain the order at which they are inserted into the dictionary. +list(filled_dict.keys())  # => ["three", "two", "one"] in Python <3.7 +list(filled_dict.keys())  # => ["one", "two", "three"] in Python 3.7+  # Get all values as an iterable with "values()". Once again we need to wrap it  # in list() to get it out of the iterable. Note - Same as above regarding key  # ordering. -list(filled_dict.values())  # => [3, 2, 1] - +list(filled_dict.values())  # => [3, 2, 1]  in Python <3.7 +list(filled_dict.values())  # => [1, 2, 3] in Python 3.7+  # Check for existence of keys in a dictionary with "in"  "one" in filled_dict  # => True @@ -793,6 +808,7 @@ class Superhero(Human):          # add additional class attributes:          self.fictional = True          self.movie = movie +        # be aware of mutable default values, since defaults are shared          self.superpowers = superpowers          # The "super" function lets you access the parent class's methods | 
