From 6cfb00d10650c46508703d4356dddeccacb70e03 Mon Sep 17 00:00:00 2001 From: Nick Presta Date: Fri, 28 Jun 2013 18:34:30 -0400 Subject: Removing the bit about commas and exceptions. --- python.html.markdown | 6 ------ 1 file changed, 6 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index a599f5d3..2c08e73e 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -283,12 +283,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 -- cgit v1.2.3 From 03461ad0dec6cf8a2f44c6ddbc40d2bb381d2253 Mon Sep 17 00:00:00 2001 From: Leah Hanson Date: Sat, 29 Jun 2013 16:21:55 -0400 Subject: tiny typo fix in python we -> be --- 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 2b67ab83..eddff031 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -15,7 +15,7 @@ to Python 2.x. Look for another tour of Python 3 soon! ```python # Single line comments start with a hash. -""" Multiline strings can we written +""" Multiline strings can be written using three "'s, and are often used as comments """ -- cgit v1.2.3 From 83aeecb68a20751d09bb83793691f19a8dc97aa2 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 29 Jun 2013 20:19:14 -0700 Subject: Added filename parameter --- python.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index eddff031..d1152b82 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -2,6 +2,7 @@ language: python author: Louie Dinh author_url: http://ldinh.ca +filename: learnpython.py --- Python was created by Guido Van Rossum in the early 90's. It is now one of the most popular -- cgit v1.2.3 From 79c26e6679948063036675b132a409b855d43cf1 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Sat, 29 Jun 2013 21:35:11 -0700 Subject: Added a few lines about truthiness, how lists can contain arbitrary data types, how you can unpack both tuples and lists, using ange, assigning lambdas to variables, dictionary comprehensions, modules, and links to more info about the standard library. --- python.html.markdown | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index d1152b82..9c59a8d7 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -91,6 +91,16 @@ not False #=> True # None is an object None #=> None +# Don't use the equality `==` symbol to compare objects to None +# Use `is` instead +"etc" is None #=> False +None is None #=> True + +# None, 0, and empty strings/lists all evaluate to False. +# All other values are True +0 == False #=> True +"" == False #=> True + #################################################### ## 2. Variables and Collections @@ -164,6 +174,9 @@ li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] # Examine the length with len len(li) #=> 6 +# Note: lists can contain arbitrary values +li2 = [1, "Hello", [[], "Hi", 5,]] + # Tuples are like lists but are immutable. tup = (1, 2, 3) tup[0] #=> 1 @@ -178,7 +191,7 @@ tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6) tup[:2] #=> (1, 2) 2 in tup #=> True -# You can unpack tuples into variables +# You can unpack tuples (or lists) into variables 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 @@ -276,6 +289,18 @@ prints: for animal in ["dog", "cat", "mouse"]: # You can use % to interpolate formatted strings print "%s is a mammal" % animal + +""" +`range(number)` returns a list of numbers +from zero to the given number +prints: + 0 + 1 + 2 + 3 +""" +for i in range(4): + print i """ While loops go until a condition is no longer met. @@ -364,6 +389,8 @@ add_10(3) #=> 13 # There are also anonymous functions (lambda x: x > 2)(3) #=> True +rectangle_area = lambda a, b: a * b +print rectangle_area(3, 4) #=> 12 # There are built-in higher order functions map(add_10, [1,2,3]) #=> [11, 12, 13] @@ -373,6 +400,9 @@ filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7] [add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13] [x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7] +# You can also use dictionary comprehensions +{i: add_10(i) for i in [1, 2, 3]} #=> {1: 11, 2: 12, 3: 13} + #################################################### ## 5. Classes #################################################### @@ -388,7 +418,8 @@ class Human(object): # Assign the argument to the instance's name attribute self.name = name - # An instance method. All methods take self as the first argument + # An instance method. All methods take self as the first argument, + # which refers to the instance of this class def say(self, msg): return "%s: %s" % (self.name, msg) @@ -421,9 +452,39 @@ j.get_species() #=> "H. neanderthalensis" # Call the static method Human.grunt() #=> "*grunt*" + + +#################################################### +## 6. Modules +#################################################### + +# You can import modules +import math +print math.sqrt(16) #=> 4 + +# You can get specific functions from a module +from math import ceil, floor +print ceil(3.7) #=> 4.0 +print floor3.7) #=> 3.0 + +# You can import all functions from a module. +# Warning: this is not recommended +from math import * + +# You can shorten module names +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. + + ``` ## Further Reading Still up for more? Try [Learn Python The Hard Way](http://learnpythonthehardway.org/book/) +Python has a huge amount of modules within the standard library. See the +[official documentation](http://docs.python.org/2/library/index.html) or +[Python Module of the Week](http://pymotw.com/2/) for more. -- cgit v1.2.3 From ba55f6fcaadebbcd76501ab69b5be03a66d0460f Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 29 Jun 2013 23:13:53 -0700 Subject: Fix whitespace --- python.html.markdown | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index d1152b82..467a179e 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -111,7 +111,7 @@ except NameError: print "Raises a name error" # if can be used as an expression -some_var = a if a > b else b +some_var = 1 if 1 > 2 else 2 # => 2 # If a is greater than b, then a is assigned to some_var. # Otherwise b is assigned to some_var. @@ -207,8 +207,11 @@ 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 +try: + # Trying to look up a non-existing key will raise a KeyError + filled_dict["four"] #=> KeyError +except KeyError: + pass # Use get method to avoid the KeyError filled_dict.get("one") #=> 1 @@ -235,7 +238,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 | @@ -337,7 +340,7 @@ def keyword_args(**kwargs): 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): +def foo(*args, **kwargs): print args print kwargs """ -- cgit v1.2.3 From 77672e78916c4d35a67934b6c78e20b015ec687a Mon Sep 17 00:00:00 2001 From: ldinh Date: Sun, 30 Jun 2013 15:16:55 -0700 Subject: Fix some inconsistencies so that the doc can be read top to bottom --- python.html.markdown | 47 ++++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 467a179e..9b0f0241 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 @@ -104,16 +106,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 = 1 if 1 > 2 else 2 # => 2 -# 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 = [] @@ -136,10 +134,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.) @@ -167,10 +162,7 @@ len(li) #=> 6 # 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 @@ -207,16 +199,12 @@ filled_dict.values() #=> [3, 2, 1] "one" in filled_dict #=> True 1 in filled_dict #=> False -try: - # Trying to look up a non-existing key will raise a KeyError - filled_dict["four"] #=> KeyError -except KeyError: - pass + # 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 @@ -259,7 +247,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." @@ -340,21 +328,22 @@ def keyword_args(**kwargs): keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} # You can do both at once, if you like -def foo(*args, **kwargs): +def all_the_args(*args, **kwargs): print args 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): -- cgit v1.2.3 From ae5060a4bf432095539bc3ff77bfec67082cbbb3 Mon Sep 17 00:00:00 2001 From: ldinh Date: Sun, 30 Jun 2013 15:35:20 -0700 Subject: Add to Python reference material --- python.html.markdown | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 9b0f0241..76f96d57 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -417,5 +417,8 @@ Human.grunt() #=> "*grunt*" ## 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/) -- cgit v1.2.3 From 34a9f1aab1b35258c56e46b858f6b1e7a7f8e28b Mon Sep 17 00:00:00 2001 From: ldinh Date: Sun, 30 Jun 2013 15:36:37 -0700 Subject: Add line breaks --- python.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 76f96d57..34fa2680 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -420,5 +420,7 @@ Human.grunt() #=> "*grunt*" 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/) -- cgit v1.2.3 From 868b259db1e74010ded00c7020d8800255c4034d Mon Sep 17 00:00:00 2001 From: ldinh Date: Sun, 30 Jun 2013 15:47:56 -0700 Subject: Change formatting --- python.html.markdown | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 34fa2680..f34412ab 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -419,8 +419,6 @@ Human.grunt() #=> "*grunt*" 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/) +*[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/) -- cgit v1.2.3 From 4d501a48fcd4d0f5e64f55a0a962b5e4302272a4 Mon Sep 17 00:00:00 2001 From: lodin Date: Sun, 30 Jun 2013 16:02:37 -0700 Subject: Fix typo. --- python.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index f34412ab..cf3ef9c5 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -419,6 +419,6 @@ Human.grunt() #=> "*grunt*" 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/) +* [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/) -- cgit v1.2.3 From ec6ffed3c92928bc86424ae9f2ea6d1e02bbbc03 Mon Sep 17 00:00:00 2001 From: lodin Date: Sun, 30 Jun 2013 16:18:20 -0700 Subject: Add hitchhiker's guide to python. --- python.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index cf3ef9c5..e2ad1eff 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -422,3 +422,4 @@ 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/) -- cgit v1.2.3 From cc3dc30518605f80e2ef11751a050ebd2997bac9 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Sat, 29 Jun 2013 21:35:11 -0700 Subject: Added a few lines about truthiness, how lists can contain arbitrary data types, how you can unpack both tuples and lists, using ange, assigning lambdas to variables, dictionary comprehensions, modules, and links to more info about the standard library. --- python.html.markdown | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 19e2aebe..c75e90c4 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -93,6 +93,16 @@ not False #=> True # None is an object None #=> None +# Don't use the equality `==` symbol to compare objects to None +# Use `is` instead +"etc" is None #=> False +None is None #=> True + +# None, 0, and empty strings/lists all evaluate to False. +# All other values are True +0 == False #=> True +"" == False #=> True + #################################################### ## 2. Variables and Collections @@ -159,6 +169,9 @@ li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] # Examine the length with len len(li) #=> 6 +# Note: lists can contain arbitrary values +li2 = [1, "Hello", [[], "Hi", 5,]] + # Tuples are like lists but are immutable. tup = (1, 2, 3) tup[0] #=> 1 @@ -170,7 +183,7 @@ tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6) tup[:2] #=> (1, 2) 2 in tup #=> True -# You can unpack tuples into variables +# You can unpack tuples (or lists) into variables 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 @@ -267,6 +280,18 @@ prints: for animal in ["dog", "cat", "mouse"]: # You can use % to interpolate formatted strings print "%s is a mammal" % animal + +""" +`range(number)` returns a list of numbers +from zero to the given number +prints: + 0 + 1 + 2 + 3 +""" +for i in range(4): + print i """ While loops go until a condition is no longer met. @@ -350,6 +375,8 @@ add_10(3) #=> 13 # There are also anonymous functions (lambda x: x > 2)(3) #=> True +rectangle_area = lambda a, b: a * b +print rectangle_area(3, 4) #=> 12 # There are built-in higher order functions map(add_10, [1,2,3]) #=> [11, 12, 13] @@ -359,6 +386,9 @@ filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7] [add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13] [x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7] +# You can also use dictionary comprehensions +{i: add_10(i) for i in [1, 2, 3]} #=> {1: 11, 2: 12, 3: 13} + #################################################### ## 5. Classes #################################################### @@ -374,7 +404,8 @@ class Human(object): # Assign the argument to the instance's name attribute self.name = name - # An instance method. All methods take self as the first argument + # An instance method. All methods take self as the first argument, + # which refers to the instance of this class def say(self, msg): return "%s: %s" % (self.name, msg) @@ -407,6 +438,33 @@ j.get_species() #=> "H. neanderthalensis" # Call the static method Human.grunt() #=> "*grunt*" + + +#################################################### +## 6. Modules +#################################################### + +# You can import modules +import math +print math.sqrt(16) #=> 4 + +# You can get specific functions from a module +from math import ceil, floor +print ceil(3.7) #=> 4.0 +print floor3.7) #=> 3.0 + +# You can import all functions from a module. +# Warning: this is not recommended +from math import * + +# You can shorten module names +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. + + ``` ## Further Reading @@ -417,3 +475,7 @@ Still up for more? Try: * [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 +[Python Module of the Week](http://pymotw.com/2/) for more. -- cgit v1.2.3 From c74780755887d270aebf5563bcfa4449e170dab0 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 1 Jul 2013 06:21:03 -0700 Subject: Incorporated feedback from lodin/adambard/others Changes: - Added a few lines on using "is" to compare object identity. - Removed example about lists containing arbitrary values. - Removed example about assigning lambdas to variables. - Removed example about dictionary comprehensions. - Removed the additional explanation about 'self' - Added a clarification on modules. --- python.html.markdown | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index c75e90c4..9324e29b 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -98,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 @@ -169,9 +173,6 @@ li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] # Examine the length with len len(li) #=> 6 -# Note: lists can contain arbitrary values -li2 = [1, "Hello", [[], "Hi", 5,]] - # Tuples are like lists but are immutable. tup = (1, 2, 3) tup[0] #=> 1 @@ -375,8 +376,6 @@ add_10(3) #=> 13 # There are also anonymous functions (lambda x: x > 2)(3) #=> True -rectangle_area = lambda a, b: a * b -print rectangle_area(3, 4) #=> 12 # There are built-in higher order functions map(add_10, [1,2,3]) #=> [11, 12, 13] @@ -386,9 +385,6 @@ filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7] [add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13] [x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7] -# You can also use dictionary comprehensions -{i: add_10(i) for i in [1, 2, 3]} #=> {1: 11, 2: 12, 3: 13} - #################################################### ## 5. Classes #################################################### @@ -404,8 +400,7 @@ class Human(object): # Assign the argument to the instance's name attribute self.name = name - # An instance method. All methods take self as the first argument, - # which refers to the instance of this class + # An instance method. All methods take self as the first argument def say(self, msg): return "%s: %s" % (self.name, msg) @@ -462,7 +457,8 @@ 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. ``` -- cgit v1.2.3 From 83900dc50427bddebae92545f82530e555fe6365 Mon Sep 17 00:00:00 2001 From: Louie Dinh Date: Mon, 1 Jul 2013 10:32:42 -0700 Subject: Tighten up py doc a bit --- python.html.markdown | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index e3f04e19..90eaf1c9 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -125,7 +125,7 @@ some_var #=> 5 some_other_var # Raises a name error # if can be used as an expression -"yahoo!" if 1 > 2 else 2 #=> "yahoo!" +"yahoo!" if 3 > 2 else 2 #=> "yahoo!" # Lists store sequences li = [] @@ -173,8 +173,6 @@ li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] # Examine the length with len len(li) #=> 6 -# Note: lists can contain arbitrary values -li2 = [1, "Hello", [[], "Hi", 5,]] # Tuples are like lists but are immutable. tup = (1, 2, 3) @@ -379,8 +377,6 @@ add_10(3) #=> 13 # There are also anonymous functions (lambda x: x > 2)(3) #=> True -rectangle_area = lambda a, b: a * b -print rectangle_area(3, 4) #=> 12 # There are built-in higher order functions map(add_10, [1,2,3]) #=> [11, 12, 13] @@ -390,9 +386,6 @@ filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7] [add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13] [x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7] -# You can also use dictionary comprehensions -{i: add_10(i) for i in [1, 2, 3]} #=> {1: 11, 2: 12, 3: 13} - #################################################### ## 5. Classes #################################################### @@ -408,8 +401,7 @@ class Human(object): # Assign the argument to the instance's name attribute self.name = name - # An instance method. All methods take self as the first argument, - # which refers to the instance of this class + # An instance method. All methods take self as the first argument def say(self, msg): return "%s: %s" % (self.name, msg) @@ -480,7 +472,4 @@ Still up for more? Try: * [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 -[Python Module of the Week](http://pymotw.com/2/) for more. +* [Python Module of the Week](http://pymotw.com/2/) -- cgit v1.2.3 From 0e0fed1e304e89ef16d449d9e2ffcc374a152071 Mon Sep 17 00:00:00 2001 From: Louie Dinh Date: Mon, 1 Jul 2013 11:50:19 -0700 Subject: Fix 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 90eaf1c9..59a0b85c 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -447,7 +447,7 @@ print math.sqrt(16) #=> 4 # You can get specific functions from a module from math import ceil, floor print ceil(3.7) #=> 4.0 -print floor3.7) #=> 3.0 +print floor(3.7) #=> 3.0 # You can import all functions from a module. # Warning: this is not recommended -- cgit v1.2.3