diff options
author | Cameron Schermerhorn <Cameron.Schermerhorn@its.ny.gov> | 2015-10-09 13:30:07 -0400 |
---|---|---|
committer | Cameron Schermerhorn <Cameron.Schermerhorn@its.ny.gov> | 2015-10-09 13:30:07 -0400 |
commit | d2010c08604c25b3166977e0cde795732ecde551 (patch) | |
tree | d4d6d72b9364e85decc55770e1f451f479321b67 /python3.html.markdown | |
parent | bf7d33037f64ea9f80f106a37929e3fdf20bd24d (diff) | |
parent | ea943b61fbee8fb0ba34f88b4d0380400e890f30 (diff) |
Merge remote-tracking branch 'refs/remotes/adambard/master'
Conflicts:
java.html.markdown
Diffstat (limited to 'python3.html.markdown')
-rw-r--r-- | python3.html.markdown | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/python3.html.markdown b/python3.html.markdown index 971ca0a4..cd1a83cc 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -96,7 +96,7 @@ False or True #=> True 1 < 2 < 3 # => True 2 < 3 < 2 # => False -# (is vs. ==) is checks if two variable refer to the same object, but == checks +# (is vs. ==) is checks if two variable refer to the same object, but == checks # if the objects pointed to have the same values. a = [1, 2, 3, 4] # Point a at a new list, [1, 2, 3, 4] b = a # Point b at what a is pointing to @@ -256,8 +256,8 @@ empty_dict = {} # Here is a prefilled dictionary filled_dict = {"one": 1, "two": 2, "three": 3} -# Note keys for dictionaries have to be immutable types. This is to ensure that -# the key can be converted to a constant hash value for quick look-ups. +# Note keys for dictionaries have to be immutable types. This is to ensure that +# the key can be converted to a constant hash value for quick look-ups. # Immutable types include ints, floats, strings, tuples. invalid_dict = {[1,2,3]: "123"} # => Raises a TypeError: unhashable type: 'list' valid_dict = {(1,2,3):[1,2,3]} # Values can be of any type, however. @@ -423,7 +423,7 @@ else: # Optional clause to the try/except block. Must follow all except blocks print("All good!") # Runs only if the code in try raises no exceptions finally: # Execute under all circumstances print("We can clean up resources here") - + # Instead of try/finally to cleanup resources you can use a with statement with open("myfile.txt") as f: for line in f: @@ -550,10 +550,13 @@ add_10(3) # => 13 # There are also anonymous functions (lambda x: x > 2)(3) # => True +(lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5 # TODO - Fix for iterables # There are built-in higher order functions map(add_10, [1, 2, 3]) # => [11, 12, 13] +map(max, [1, 2, 3], [4, 2, 1]) # => [4, 2, 3] + filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7] # We can use list comprehensions for nice maps and filters |