From 8c1a1d8a7b55761f71c0a202967fdc7547b506f1 Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Fri, 23 Aug 2013 00:06:47 +0300 Subject: some python keywords, which are in comments, enclosed in quotation marks to become clear (understandable) --- python.html.markdown | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index f0b74d08..f44c23e9 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -158,19 +158,19 @@ li[2:] #=> [4, 3] # Omit the end li[:3] #=> [1, 2, 4] -# Remove arbitrary elements from a list with del +# Remove arbitrary elements from a list with `del` del li[2] # li is now [1, 2, 3] # You can add lists li + other_li #=> [1, 2, 3, 4, 5, 6] - Note: li and other_li is left alone -# Concatenate lists with extend +# Concatenate lists with `extend()` li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] -# Check for existence in a list with in +# Check for existence in a list with `in` 1 in li #=> True -# Examine the length with len +# Examine the length with `len()` len(li) #=> 6 @@ -201,37 +201,37 @@ filled_dict = {"one": 1, "two": 2, "three": 3} # Look up values with [] filled_dict["one"] #=> 1 -# Get all keys as a list +# Get all keys as a list with `keys()` filled_dict.keys() #=> ["three", "two", "one"] # Note - Dictionary key ordering is not guaranteed. # Your results might not match this exactly. -# Get all values as a list +# Get all values as a list with `values()` filled_dict.values() #=> [3, 2, 1] # Note - Same as above regarding key ordering. -# Check for existence of keys in a dictionary with in +# Check for existence of keys in a dictionary with `in` "one" in filled_dict #=> True 1 in filled_dict #=> False # Looking up a non-existing key is a KeyError filled_dict["four"] # KeyError -# Use get method to avoid the 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 -# Setdefault method is a safe way to add new key-value pair into dictionary +# `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() -# Initialize a set with a bunch of values +# Initialize a "set()" with a bunch of values some_set = set([1,2,2,3,4]) # some_set is now set([1, 2, 3, 4]) # Since Python 2.7, {} can be used to declare a set @@ -312,7 +312,7 @@ while x < 4: # Works on Python 2.6 and up: try: - # 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. @@ -322,7 +322,7 @@ except IndexError as e: ## 4. Functions #################################################### -# Use def to create new functions +# Use `def` to create new functions def add(x, y): print "x is %s and y is %s" % (x, y) return x + y # Return values with a return statement @@ -359,7 +359,7 @@ all_the_args(1, 2, a=3, b=4) prints: {"a": 3, "b": 4} """ -# When calling functions, you can do the opposite of varargs/kwargs! +# When calling functions, you can do the opposite of args/kwargs! # Use * to expand tuples and use ** to expand kwargs. args = (1, 2, 3, 4) kwargs = {"a": 3, "b": 4} @@ -402,7 +402,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 + # An instance method. All methods take `self` as the first argument def say(self, msg): return "%s: %s" % (self.name, msg) -- cgit v1.2.3 From de027ddf9e1c8124dd47f077c3ba2ed01d9cefdd Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Fri, 23 Aug 2013 09:54:21 +0300 Subject: regular quotes instead of `. --- python.html.markdown | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index f44c23e9..bad9a360 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -93,8 +93,8 @@ not False #=> True # None is an object None #=> None -# Don't use the equality `==` symbol to compare objects to None -# Use `is` instead +# Don't use the equality "==" symbol to compare objects to None +# Use "is" instead "etc" is None #=> False None is None #=> True @@ -158,19 +158,19 @@ li[2:] #=> [4, 3] # Omit the end li[:3] #=> [1, 2, 4] -# Remove arbitrary elements from a list with `del` +# Remove arbitrary elements from a list with "del" del li[2] # li is now [1, 2, 3] # You can add lists li + other_li #=> [1, 2, 3, 4, 5, 6] - Note: li and other_li is left alone -# Concatenate lists with `extend()` +# Concatenate lists with "extend()" li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] -# Check for existence in a list with `in` +# Check for existence in a list with "in" 1 in li #=> True -# Examine the length with `len()` +# Examine the length with "len()" len(li) #=> 6 @@ -201,30 +201,30 @@ filled_dict = {"one": 1, "two": 2, "three": 3} # Look up values with [] filled_dict["one"] #=> 1 -# Get all keys as a list with `keys()` +# Get all keys as a list with "keys()" filled_dict.keys() #=> ["three", "two", "one"] # Note - Dictionary key ordering is not guaranteed. # Your results might not match this exactly. -# Get all values as a list with `values()` +# Get all values as a list with "values()" filled_dict.values() #=> [3, 2, 1] # Note - Same as above regarding key ordering. -# Check for existence of keys in a dictionary with `in` +# Check for existence of keys in a dictionary with "in" "one" in filled_dict #=> True 1 in filled_dict #=> False # Looking up a non-existing key is a KeyError filled_dict["four"] # KeyError -# Use `get()` method to avoid the 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 -# `setdefault()` method is a safe way to add new key-value pair into dictionary +# "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 @@ -284,7 +284,7 @@ for animal in ["dog", "cat", "mouse"]: print "%s is a mammal" % animal """ -`range(number)` returns a list of numbers +"range(number)" returns a list of numbers from zero to the given number prints: 0 @@ -312,7 +312,7 @@ while x < 4: # Works on Python 2.6 and up: try: - # 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. @@ -322,7 +322,7 @@ except IndexError as e: ## 4. Functions #################################################### -# Use `def` to create new functions +# Use "def" to create new functions def add(x, y): print "x is %s and y is %s" % (x, y) return x + y # Return values with a return statement @@ -402,7 +402,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 + # An instance method. All methods take "self" as the first argument def say(self, msg): return "%s: %s" % (self.name, msg) -- cgit v1.2.3 From 1405dc6387ad95f0161e788a27340e02f3f82471 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Fri, 20 Sep 2013 19:32:58 +0930 Subject: [python] Clarify setdefault, as per #234 --- 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 bad9a360..bbb493da 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -224,7 +224,7 @@ filled_dict.get("four") #=> None 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 +# "setdefault()" inserts into a dictionary only if the given key isn't present filled_dict.setdefault("five", 5) #filled_dict["five"] is set to 5 filled_dict.setdefault("five", 6) #filled_dict["five"] is still 5 @@ -282,9 +282,9 @@ 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 +"range(number)" returns a list of numbers from zero to the given number prints: 0 @@ -459,7 +459,7 @@ 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. The name of the +# can write your own, and import them. The name of the # module is the same as the name of the file. # You can find out which functions and attributes -- cgit v1.2.3 From 6d8ffb19b4858d88fc33599aab279a849991c0d8 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Fri, 20 Sep 2013 19:33:39 +0930 Subject: [python] Fix typo in comment, as per #265 --- 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 bbb493da..ff6781da 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -235,7 +235,7 @@ empty_set = set() some_set = set([1,2,2,3,4]) # some_set is now set([1, 2, 3, 4]) # Since Python 2.7, {} can be used to declare a set -filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4} +filled_set = {1, 2, 2, 3, 4} # => {1, 2, 3, 4} # Add more items to a set filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5} -- cgit v1.2.3 From de36671ac194983a0b18d6a93b9f20f58f23be8f Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Fri, 20 Sep 2013 19:49:38 +0930 Subject: [python] Change to print function, as per #119 --- python.html.markdown | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index ff6781da..08e68407 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -112,8 +112,10 @@ None is None #=> True ## 2. Variables and Collections #################################################### -# Printing is pretty easy -print "I'm Python. Nice to meet you!" +# Python has a print function, available in versions 2.7 and 3... +print("I'm Python. Nice to meet you!") +# and an older print statement, in all 2.x versions but removed from 3. +print "I'm also Python!" # No need to declare variables before assigning to them. @@ -265,11 +267,11 @@ some_var = 5 # 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." + print("some_var is totally bigger than 10.") elif some_var < 10: # This elif clause is optional. - print "some_var is smaller than 10." + print("some_var is smaller than 10.") else: # This is optional too. - print "some_var is indeed 10." + print("some_var is indeed 10.") """ @@ -281,7 +283,7 @@ 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) """ "range(number)" returns a list of numbers @@ -293,7 +295,7 @@ prints: 3 """ for i in range(4): - print i + print(i) """ While loops go until a condition is no longer met. @@ -305,7 +307,7 @@ prints: """ x = 0 while x < 4: - print x + print(x) x += 1 # Shorthand for x = x + 1 # Handle exceptions with a try/except block @@ -324,7 +326,7 @@ except IndexError as e: # Use "def" to create new functions def add(x, y): - print "x is %s and y is %s" % (x, y) + print("x is %s and y is %s" % (x, y)) return x + y # Return values with a return statement # Calling functions with parameters @@ -351,8 +353,8 @@ 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): - print args - print kwargs + print(args) + print(kwargs) """ all_the_args(1, 2, a=3, b=4) prints: (1, 2) @@ -420,10 +422,10 @@ class Human(object): # Instantiate a class i = Human(name="Ian") -print i.say("hi") # prints out "Ian: hi" +print(i.say("hi")) # prints out "Ian: hi" j = Human("Joel") -print j.say("hello") #prints out "Joel: hello" +print(j.say("hello")) #prints out "Joel: hello" # Call our class method i.get_species() #=> "H. sapiens" @@ -443,12 +445,12 @@ Human.grunt() #=> "*grunt*" # You can import modules import math -print math.sqrt(16) #=> 4 +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 floor(3.7) #=> 3.0 +print(ceil(3.7)) #=> 4.0 +print(floor(3.7)) #=> 3.0 # You can import all functions from a module. # Warning: this is not recommended -- cgit v1.2.3 From eb8afb2ff27e3dba477f9ab7257d4b13e55718b1 Mon Sep 17 00:00:00 2001 From: Amin Bandali Date: Mon, 11 Nov 2013 23:17:34 -0500 Subject: reverting a list using slices --- python.html.markdown | 3 +++ 1 file changed, 3 insertions(+) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 08e68407..50c4e63f 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -2,6 +2,7 @@ language: python contributors: - ["Louie Dinh", "http://ldinh.ca"] + - ["Amin Bandali", "http://aminbandali.com"] filename: learnpython.py --- @@ -159,6 +160,8 @@ li[1:3] #=> [2, 4] li[2:] #=> [4, 3] # Omit the end li[:3] #=> [1, 2, 4] +# Revert the list +li[::-1] #=> [3, 4, 2, 1] # Remove arbitrary elements from a list with "del" del li[2] # li is now [1, 2, 3] -- cgit v1.2.3 From f5c9b8037788a6871e1193b48442f3a6d0e3f561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Migda=C5=82?= Date: Tue, 19 Nov 2013 17:17:28 +0100 Subject: added an online intro into Python for scientists --- 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 50c4e63f..22d236ac 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -484,6 +484,7 @@ dir(math) * [The Official Docs](http://docs.python.org/2.6/) * [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/) * [Python Module of the Week](http://pymotw.com/2/) +* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182) ### Dead Tree -- cgit v1.2.3