diff options
Diffstat (limited to 'python.html.markdown')
-rw-r--r-- | python.html.markdown | 64 |
1 files changed, 30 insertions, 34 deletions
diff --git a/python.html.markdown b/python.html.markdown index 9c59a8d7..e3f04e19 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -87,6 +87,8 @@ not False #=> True # A newer way to format strings is the format method. # This method is the preferred way "{0} can be {1}".format("strings", "formatted") +# You can use keywords if you don't want to count. +"{name} wants to eat {food}".format(name="Bob", food="lasagna") # None is an object None #=> None @@ -96,6 +98,10 @@ None #=> None "etc" is None #=> False None is None #=> True +# The 'is' operator tests for object identity. This isn't +# very useful when dealing with primitive values, but is +# very useful when dealing with objects. + # None, 0, and empty strings/lists all evaluate to False. # All other values are True 0 == False #=> True @@ -114,16 +120,12 @@ print "I'm Python. Nice to meet you!" some_var = 5 # Convention is to use lower_case_with_underscores some_var #=> 5 -# Accessing a previously unassigned variable is an exception -try: - some_other_var -except NameError: - print "Raises a name error" +# Accessing a previously unassigned variable is an exception. +# See Control Flow to learn more about exception handling. +some_other_var # Raises a name error # if can be used as an expression -some_var = a if a > b else b -# If a is greater than b, then a is assigned to some_var. -# Otherwise b is assigned to some_var. +"yahoo!" if 1 > 2 else 2 #=> "yahoo!" # Lists store sequences li = [] @@ -146,10 +148,7 @@ li[0] #=> 1 li[-1] #=> 3 # Looking out of bounds is an IndexError -try: - li[4] # Raises an IndexError -except IndexError: - print "Raises an IndexError" +li[4] # Raises an IndexError # You can look at ranges with slice syntax. # (It's a closed/open range for you mathy types.) @@ -180,10 +179,7 @@ li2 = [1, "Hello", [[], "Hi", 5,]] # Tuples are like lists but are immutable. tup = (1, 2, 3) tup[0] #=> 1 -try: - tup[0] = 3 # Raises a TypeError -except TypeError: - print "Tuples cannot be mutated." +tup[0] = 3 # Raises a TypeError # You can do all those list thingies on tuples too len(tup) #=> 3 @@ -220,13 +216,12 @@ filled_dict.values() #=> [3, 2, 1] "one" in filled_dict #=> True 1 in filled_dict #=> False -# Trying to look up a non-existing key will raise a KeyError -filled_dict["four"] #=> KeyError + # Looking up a non-existing key is a KeyError +filled_dict["four"] # KeyError # Use get method to avoid the KeyError filled_dict.get("one") #=> 1 filled_dict.get("four") #=> None - # The get method supports a default argument when the value is missing filled_dict.get("one", 4) #=> 1 filled_dict.get("four", 4) #=> 4 @@ -248,7 +243,7 @@ filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4} filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5} # Do set intersection with & -other_set = set{3, 4, 5, 6} +other_set = {3, 4, 5, 6} filled_set & other_set #=> {3, 4, 5} # Do set union with | @@ -269,7 +264,7 @@ filled_set | other_set #=> {1, 2, 3, 4, 5, 6} # Let's just make a variable some_var = 5 -# Here is an if statement. INDENTATION IS SIGNIFICANT IN PYTHON! +# Here is an if statement. Indentation is significant in python! # prints "some var is smaller than 10" if some_var > 10: print "some_var is totally bigger than 10." @@ -324,12 +319,6 @@ try: except IndexError as e: pass # Pass is just a no-op. Usually you would do recovery here. -# Works for Python 2.7 and down: -try: - raise IndexError("This is an index error") -except IndexError, e: # No "as", comma instead - pass - #################################################### ## 4. Functions @@ -367,16 +356,17 @@ def all_the_args(*args, **kwargs): print kwargs """ all_the_args(1, 2, a=3, b=4) prints: - [1, 2] + (1, 2) {"a": 3, "b": 4} """ -# You can also use * and ** when calling a function +# When calling functions, you can do the opposite of varargs/kwargs! +# Use * to expand tuples and use ** to expand kwargs. args = (1, 2, 3, 4) kwargs = {"a": 3, "b": 4} -foo(*args) # equivalent to foo(1, 2, 3, 4) -foo(**kwargs) # equivalent to foo(a=3, b=4) -foo(*args, **kwargs) # equivalent to foo(1, 2, 3, 4, a=3, b=4) +all_the_args(*args) # equivalent to foo(1, 2, 3, 4) +all_the_args(**kwargs) # equivalent to foo(a=3, b=4) +all_the_args(*args, **kwargs) # equivalent to foo(1, 2, 3, 4, a=3, b=4) # Python has first class functions def create_adder(x): @@ -476,14 +466,20 @@ import math as m math.sqrt(16) == m.sqrt(16) #=> True # Python modules are just ordinary python files. You -# can write your own, and import them. +# can write your own, and import them. The name of the +# module is the same as the name of the file. ``` ## Further Reading -Still up for more? Try [Learn Python The Hard Way](http://learnpythonthehardway.org/book/) +Still up for more? Try: + +* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/) +* [Dive Into Python](http://www.diveintopython.net/) +* [The Official Docs](http://docs.python.org/2.6/) +* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/) Python has a huge amount of modules within the standard library. See the [official documentation](http://docs.python.org/2/library/index.html) or |