From 2e9f5a642bc7a90045830892af1ade48ea613a2a Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 27 Jun 2013 10:53:43 -0700 Subject: Small edits to python version --- python.html.markdown | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 168f1ea1..615ee249 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -106,7 +106,8 @@ li[-1] #=> 4 # Looking out of bounds is an IndexError li[4] # Raises an IndexError -# You can look at ranges with slice syntax. It's an closed/open range for you mathy types. +# You can look at ranges with slice syntax. +# (It's a closed/open range for you mathy types.) li[1:3] #=> [2, 4] # Omit the beginning li[:3] #=> [1, 2, 4] @@ -233,7 +234,8 @@ while x < 4: # Handle exceptions with a try/except block try: - raise IndexError("This is an index error") # Use raise to raise an error + # Use raise to raise an error + raise IndexError("This is an index error") except IndexError as e: pass # Pass is just a no-op. Usually you would do recovery here. @@ -252,20 +254,26 @@ add(5, 6) #=> 11 and prints out "x is 5 and y is 6" # Another way to call functions is with keyword arguments add(y=6, x=5) # Keyword arguments can arrive in any order. -# You can define functions that take a variable number of positional arguments +# You can define functions that take a variable number of +# positional arguments def varargs(*args): return args varargs(1, 2, 3) #=> (1,2,3) -# You can define functions that take a variable number of keyword arguments +# You can define functions that take a variable number of +# keyword arguments, as well def keyword_args(**kwargs): return kwargs # Let's call it to see what happens keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} +# You can do both at once, if you like +def all_the_args(*args, **kwargs): + pass + # Python has first class functions def create_adder(x): -- cgit v1.2.3 From 26d6fe55e382517ed3015ee137afa3c057b4a02d Mon Sep 17 00:00:00 2001 From: Julia Evans Date: Thu, 27 Jun 2013 14:35:33 -0400 Subject: Fix typo 8-1 is 7 =) --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 615ee249..eb8f5cb4 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -26,7 +26,7 @@ to Python 2.x. Look for another tour of Python 3 soon! # Math is what you would expect 1 + 1 #=> 2 -8 - 1 #=> 9 +8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 -- cgit v1.2.3 From d164d5a56ea003c003ff65a0d316a61b31571481 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 27 Jun 2013 18:18:05 -0700 Subject: Minor fixes to python doc --- python.html.markdown | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index eb8f5cb4..bf3739ba 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -233,12 +233,20 @@ while x < 4: x += 1 # Shorthand for x = x + 1 # Handle exceptions with a try/except block + +# Works on Python 2.6 and up: try: # Use raise to raise an error raise IndexError("This is an index error") 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 @@ -272,7 +280,13 @@ keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} # You can do both at once, if you like def all_the_args(*args, **kwargs): - pass + print args + print kwargs +""" +all_the_args(1, 2, a=3, b=4) prints: + [1, 2] + {"a": 3, "b": 4} +""" # Python has first class functions -- cgit v1.2.3 From 1fed90cba669b64af4e39e53d167b3c5e94ea1af Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 27 Jun 2013 18:22:30 -0700 Subject: Fixed any syntax errors --- python.html.markdown | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index bf3739ba..07a83042 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -81,7 +81,10 @@ some_var = 5 # Convention is to use lower_case_with_underscores some_var #=> 5 # Accessing a previously unassigned variable is an exception -some_other_var # Will raise a NameError +try: + some_other_var +except NameError: + print "Raises a name error" # Lists store sequences @@ -103,8 +106,12 @@ li.append(3) # li is now [1, 2, 4, 3] again. li[0] #=> 1 # Look at the last element li[-1] #=> 4 + # Looking out of bounds is an IndexError -li[4] # Raises an IndexError +try: + li[4] # Raises an IndexError +except IndexError: + print "Raises an IndexError" # You can look at ranges with slice syntax. # (It's a closed/open range for you mathy types.) @@ -132,7 +139,10 @@ len(li) #=> 6 # Tuples are like lists but are immutable. tup = (1, 2, 3) tup[0] #=> 1 -tup[0] = 3 # Raises a TypeError +try: + tup[0] = 3 # Raises a TypeError +except TypeError: + print "Tuples cannot be mutated." # You can do all those list thingies on tuples too len(tup) #=> 3 @@ -295,7 +305,7 @@ def create_adder(x): return x + y return adder -add_10 = create_adder(10): +add_10 = create_adder(10) add_10(3) #=> 13 # There are also anonymous functions @@ -357,3 +367,5 @@ j.get_species() #=> "H. neanderthalensis" # Call the static method Human.grunt() #=> "*grunt*" +``` + -- cgit v1.2.3 From e37b1745a7ef0cfcb4328d8264b3673bc445746f Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 27 Jun 2013 18:27:14 -0700 Subject: Fixed a typo --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 07a83042..42801657 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -154,7 +154,7 @@ tup[:2] #=> (1, 2) a, b, c = (1, 2, 3) # a is now 1, b is now 2 and c is now 3 # Tuples are created by default if you leave out the parentheses d, e, f = 4, 5, 6 -# Now look how easy it is to swap to values +# Now look how easy it is to swap two values e, d = d, e # d is now 5 and e is now 4 -- cgit v1.2.3 From ff14723ee6a4486a9e567f56dfb2eb7c69df582a Mon Sep 17 00:00:00 2001 From: "js.sevestre" Date: Fri, 28 Jun 2013 09:52:39 +0200 Subject: Add inequality and chained comparisons --- python.html.markdown | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 42801657..300a5519 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -49,11 +49,24 @@ False not True #=> False not False #=> True - # Equality is == 1 == 1 #=> True 2 == 1 #=> False +# Inequality is != +1 != 1 #=> False +2 != 1 #=> True + +# More comparisons +1 < 10 #=> True +1 > 10 #=> False +2 <= 2 #=> True +2 >= 2 #=> True + +# Comparisons can be chained ! +1 < 2 < 3 #=> True +2 < 3 < 2 #=> False + # Strings are created with " or ' "This is a string." 'This is also a string.' -- cgit v1.2.3 From 9e17e878854531c75205147f30842850a75a5a20 Mon Sep 17 00:00:00 2001 From: Liteye Date: Fri, 28 Jun 2013 22:59:25 +0800 Subject: add conditional expressions, get and setdefault for dictionary and a little tip about fuction calling. Signed-off-by: Liteye --- python.html.markdown | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 300a5519..982333ca 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -99,6 +99,10 @@ try: except NameError: print "Raises a name error" +# Conditional Expressions can be used when assigning +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. # Lists store sequences li = [] @@ -192,6 +196,17 @@ 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 + +# Use get method to avoid the KeyError +filled_dict.get("one", 4) #=> 1 +filled_dict.get("four", 4) #=> 4 + +# Setdefault method is a safe way to add new key-value pair into dictionary +filled_dict.setdefault("five", 5) #filled_dict["five"] is set to 5 +filled_dict.setdefault("five", 6) #filled_dict["five"] is still 5 + # Sets store ... well sets empty_set = set() @@ -311,6 +326,12 @@ all_the_args(1, 2, a=3, b=4) prints: {"a": 3, "b": 4} """ +# You can also use * and ** when calling a function +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) # Python has first class functions def create_adder(x): -- cgit v1.2.3 From 2e805c8c020719da0aab40987b7c064c864db9c8 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 28 Jun 2013 12:59:45 -0700 Subject: Updates --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 982333ca..39a2349d 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -395,7 +395,7 @@ print j.say("hello") #prints out "Joel: hello" i.get_species() #=> "H. sapiens" # Change the shared attribute -i.species = "H. neanderthalensis" +Human.species = "H. neanderthalensis" i.get_species() #=> "H. neanderthalensis" j.get_species() #=> "H. neanderthalensis" -- cgit v1.2.3 From f7352567f4f76a26ebfe09365faa8bc790463dd3 Mon Sep 17 00:00:00 2001 From: mrshankly Date: Fri, 28 Jun 2013 21:29:03 +0100 Subject: Corrected the last element of li in the lists explanation. --- python.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 39a2349d..0d9a3ed6 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -122,7 +122,7 @@ li.append(3) # li is now [1, 2, 4, 3] again. # Access a list like you would any array li[0] #=> 1 # Look at the last element -li[-1] #=> 4 +li[-1] #=> 3 # Looking out of bounds is an IndexError try: @@ -145,7 +145,7 @@ del li[2] # li is now [1, 2, 3] li + other_li #=> [1, 2, 3, 4, 5, 6] - Note: li and other_li is left alone # Concatenate lists with extend -li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] +li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] # Check for existence in a list with in 1 in li #=> True @@ -255,14 +255,14 @@ prints: """ for animal in ["dog", "cat", "mouse"]: # You can use % to interpolate formatted strings - print "%s is a mammal" % animal + print "%s is a mammal" % animal """ While loops go until a condition is no longer met. prints: 0 1 - 2 + 2 3 """ x = 0 -- cgit v1.2.3 From fe5f72d868313987cb3d6a55de707876046d912d Mon Sep 17 00:00:00 2001 From: Kyle Partridge Date: Fri, 28 Jun 2013 13:55:16 -0700 Subject: added some clarification on the default value for dictionary.get --- python.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 39a2349d..a3ced659 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -200,6 +200,10 @@ filled_dict.values() #=> [3, 2, 1] 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 -- cgit v1.2.3