From d2f0d8222e9e5c4c17f6ccde096df3c4d02d43af Mon Sep 17 00:00:00 2001 From: ldinh Date: Mon, 5 Aug 2013 17:42:38 -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 e7ee6fbd..1a54e633 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -232,7 +232,7 @@ 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 -some_set = set([1,2,2,3,4]) # filled_set is now set([1, 2, 3, 4]) +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} -- cgit v1.2.3 From 5617b2804895e096801c2c9bb5f1386d18095276 Mon Sep 17 00:00:00 2001 From: Louie Dinh Date: Mon, 5 Aug 2013 17:54:50 -0700 Subject: Expand on References. Add Books. --- python.html.markdown | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 1a54e633..298b7bb7 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -470,12 +470,19 @@ dir(math) ``` -## Further Reading +## Ready For More? -Still up for more? Try: +### Free Online * [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 Module of the Week](http://pymotw.com/2/) + +### Dead Tree + +* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20) +* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20) +* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20) + -- cgit v1.2.3 From 0dff3a936cc7ca9e5a9574a889fcf1d4180efcab Mon Sep 17 00:00:00 2001 From: cssmagic Date: Wed, 7 Aug 2013 22:48:11 +0800 Subject: Fix tiny typos. --- 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 298b7bb7..a32db51a 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -67,7 +67,7 @@ not False #=> True 2 <= 2 #=> True 2 >= 2 #=> True -# Comparisons can be chained ! +# Comparisons can be chained! 1 < 2 < 3 #=> True 2 < 3 < 2 #=> False @@ -214,7 +214,7 @@ filled_dict.values() #=> [3, 2, 1] "one" in filled_dict #=> True 1 in filled_dict #=> False - # Looking up a non-existing key is a KeyError +# Looking up a non-existing key is a KeyError filled_dict["four"] # KeyError # Use get method to avoid the KeyError @@ -263,7 +263,7 @@ filled_set | other_set #=> {1, 2, 3, 4, 5, 6} some_var = 5 # Here is an if statement. Indentation is significant in python! -# prints "some var is smaller than 10" +# prints "some_var is smaller than 10" if some_var > 10: print "some_var is totally bigger than 10." elif some_var < 10: # This elif clause is optional. @@ -394,7 +394,7 @@ filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7] # We subclass from object to get a class. class Human(object): - # A class attribute. It is shared by all instances of this class + # A class attribute. It is shared by all instances of this class species = "H. sapiens" # Basic initializer -- cgit v1.2.3 From 10ffa5a7815ae40803ad409dd25170883291e498 Mon Sep 17 00:00:00 2001 From: michaelstewart Date: Thu, 8 Aug 2013 18:49:45 +1000 Subject: Added an apostrophe to Its --- 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 a32db51a..f0b74d08 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -6,7 +6,7 @@ filename: learnpython.py --- Python was created by Guido Van Rossum in the early 90's. It is now one of the most popular -languages in existence. I fell in love with Python for its syntactic clarity. Its basically +languages in existence. I fell in love with Python for its syntactic clarity. It's basically executable pseudocode. Feedback would be highly appreciated! You can reach me at [@louiedinh](http://twitter.com/louiedinh) or louiedinh [at] [google's email service] -- cgit v1.2.3 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 From 1e60055977dd82e6e69de43973e2481d93f78b64 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Tue, 26 Nov 2013 18:12:53 +1030 Subject: [python] == doesn't coerce types; use bool() to demonstrate truthiness instead. Closes #418. --- python.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 22d236ac..941ba9f4 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -105,8 +105,8 @@ None is None #=> True # None, 0, and empty strings/lists all evaluate to False. # All other values are True -0 == False #=> True -"" == False #=> True +bool(0) #=> False +bool("") #=> False #################################################### -- cgit v1.2.3 From 550b9c9f2ebf8ead67eb723d3b4256064e284b82 Mon Sep 17 00:00:00 2001 From: charalambosp Date: Sun, 16 Feb 2014 18:36:09 +0000 Subject: generators & decorators --- python.html.markdown | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 941ba9f4..fe0ddc8c 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -473,6 +473,54 @@ import math dir(math) +#################################################### +## 6. Advanced +#################################################### + +# Generators help you make lazy code +def double_numbers(iterable): + for i in iterable: + yield i + i + +# generator creates the value on the fly +# instead of generating and returning all values at once it creates one in each iteration +# this means values bigger than 15 wont be processed in double_numbers +# note range is a generator too, creating a list 1-900000000 would take lot of time to be made +_range = range(1, 900000000) +# will double all numbers until a result >=30 found +for i in double_numbers(_range): + print(i) + if i >= 30: + break + + +# Decorators +# in this example beg wraps say +# Beg will call say. If say_please is True then it will change the returned message +from functools import wraps + + +def beg(_say): + @wraps(_say) + def wrapper(*args, **kwargs): + msg, say_please = _say(*args, **kwargs) + if say_please: + return "{} {}".format(msg, "Please! I am poor :(") + return msg + + return wrapper + + +@beg +def say(say_please=False): + msg = "Can you buy me a beer?" + return msg, say_please + + +print(say()) # Can you buy me a beer? +print(say(say_please=True)) # Can you buy me a beer? Please! I am poor :( + + ``` ## Ready For More? -- cgit v1.2.3 From 3233394cd5079893acb725027f8d0a82e28c16cc Mon Sep 17 00:00:00 2001 From: charalambosp Date: Sun, 16 Feb 2014 18:37:02 +0000 Subject: fix section number --- 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 fe0ddc8c..15f27d37 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -474,7 +474,7 @@ dir(math) #################################################### -## 6. Advanced +## 7. Advanced #################################################### # Generators help you make lazy code -- cgit v1.2.3 From da3b7c3520b18bf5b94d99bcf8170d679051810a Mon Sep 17 00:00:00 2001 From: Mark Whiting Date: Mon, 17 Feb 2014 21:07:29 -0500 Subject: Better explanation of list slices Step size and list slice syntax was not quite clear. Upon seeing li[::-1] as it was previously, I was not sure what this really meant so I looked it up and found that this section of the guide may benefit from more explanation. --- 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 15f27d37..908a0638 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -160,8 +160,12 @@ li[1:3] #=> [2, 4] li[2:] #=> [4, 3] # Omit the end li[:3] #=> [1, 2, 4] +# Select every second entry +li[::2] #=>[1,4] # Revert the list li[::-1] #=> [3, 4, 2, 1] +# Use any combination of these to make advanced slices +# li[start:end:step] # Remove arbitrary elements from a list with "del" del li[2] # li is now [1, 2, 3] -- cgit v1.2.3 From 7db61d1ce143bb8c38eeee662f17754a3f7d5da9 Mon Sep 17 00:00:00 2001 From: Guillermo Garza Date: Fri, 21 Mar 2014 18:14:55 -0500 Subject: Modify python to be pep8 compliant This mostly adds spaces. --- python.html.markdown | 209 ++++++++++++++++++++++++++------------------------- 1 file changed, 108 insertions(+), 101 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 908a0638..ced01910 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -16,7 +16,9 @@ Note: This article applies to Python 2.7 specifically, but should be applicable to Python 2.x. Look for another tour of Python 3 soon! ```python + # Single line comments start with a hash. + """ Multiline strings can be written using three "'s, and are often used as comments @@ -27,60 +29,60 @@ to Python 2.x. Look for another tour of Python 3 soon! #################################################### # You have numbers -3 #=> 3 +3 # => 3 # Math is what you would expect -1 + 1 #=> 2 -8 - 1 #=> 7 -10 * 2 #=> 20 -35 / 5 #=> 7 +1 + 1 # => 2 +8 - 1 # => 7 +10 * 2 # => 20 +35 / 5 # => 7 # Division is a bit tricky. It is integer division and floors the results # automatically. -5 / 2 #=> 2 +5 / 2 # => 2 # To fix division we need to learn about floats. 2.0 # This is a float -11.0 / 4.0 #=> 2.75 ahhh...much better +11.0 / 4.0 # => 2.75 ahhh...much better # Enforce precedence with parentheses -(1 + 3) * 2 #=> 8 +(1 + 3) * 2 # => 8 # Boolean values are primitives True False # negate with not -not True #=> False -not False #=> True +not True # => False +not False # => True # Equality is == -1 == 1 #=> True -2 == 1 #=> False +1 == 1 # => True +2 == 1 # => False # Inequality is != -1 != 1 #=> False -2 != 1 #=> True +1 != 1 # => False +2 != 1 # => True # More comparisons -1 < 10 #=> True -1 > 10 #=> False -2 <= 2 #=> True -2 >= 2 #=> True +1 < 10 # => True +1 > 10 # => False +2 <= 2 # => True +2 >= 2 # => True # Comparisons can be chained! -1 < 2 < 3 #=> True -2 < 3 < 2 #=> False +1 < 2 < 3 # => True +2 < 3 < 2 # => False # Strings are created with " or ' "This is a string." 'This is also a string.' # Strings can be added too! -"Hello " + "world!" #=> "Hello world!" +"Hello " + "world!" # => "Hello world!" # A string can be treated like a list of characters -"This is a string"[0] #=> 'T' +"This is a string"[0] # => 'T' # % can be used to format strings, like this: "%s can be %s" % ("strings", "interpolated") @@ -92,12 +94,12 @@ not False #=> True "{name} wants to eat {food}".format(name="Bob", food="lasagna") # None is an object -None #=> None +None # => None # Don't use the equality "==" symbol to compare objects to None # Use "is" instead -"etc" is None #=> False -None is None #=> True +"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 @@ -105,8 +107,8 @@ None is None #=> True # None, 0, and empty strings/lists all evaluate to False. # All other values are True -bool(0) #=> False -bool("") #=> False +bool(0) # => False +bool("") # => False #################################################### @@ -121,14 +123,14 @@ print "I'm also Python!" # No need to declare variables before assigning to them. some_var = 5 # Convention is to use lower_case_with_underscores -some_var #=> 5 +some_var # => 5 # 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 -"yahoo!" if 3 > 2 else 2 #=> "yahoo!" +"yahoo!" if 3 > 2 else 2 # => "yahoo!" # Lists store sequences li = [] @@ -136,63 +138,63 @@ li = [] other_li = [4, 5, 6] # Add stuff to the end of a list with append -li.append(1) #li is now [1] -li.append(2) #li is now [1, 2] -li.append(4) #li is now [1, 2, 4] -li.append(3) #li is now [1, 2, 4, 3] +li.append(1) # li is now [1] +li.append(2) # li is now [1, 2] +li.append(4) # li is now [1, 2, 4] +li.append(3) # li is now [1, 2, 4, 3] # Remove from the end with pop -li.pop() #=> 3 and li is now [1, 2, 4] +li.pop() # => 3 and li is now [1, 2, 4] # Let's put it back li.append(3) # li is now [1, 2, 4, 3] again. # Access a list like you would any array -li[0] #=> 1 +li[0] # => 1 # Look at the last element -li[-1] #=> 3 +li[-1] # => 3 # Looking out of bounds is an IndexError -li[4] # 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.) -li[1:3] #=> [2, 4] +li[1:3] # => [2, 4] # Omit the beginning -li[2:] #=> [4, 3] +li[2:] # => [4, 3] # Omit the end -li[:3] #=> [1, 2, 4] +li[:3] # => [1, 2, 4] # Select every second entry -li[::2] #=>[1,4] +li[::2] # =>[1, 4] # Revert the list -li[::-1] #=> [3, 4, 2, 1] +li[::-1] # => [3, 4, 2, 1] # Use any combination of these to make advanced slices # li[start:end:step] # Remove arbitrary elements from a list with "del" -del li[2] # li is now [1, 2, 3] +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 +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 +1 in li # => True # Examine the length with "len()" -len(li) #=> 6 +len(li) # => 6 # Tuples are like lists but are immutable. tup = (1, 2, 3) -tup[0] #=> 1 +tup[0] # => 1 tup[0] = 3 # Raises a TypeError # You can do all those list thingies on tuples too -len(tup) #=> 3 -tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6) -tup[:2] #=> (1, 2) -2 in tup #=> True +len(tup) # => 3 +tup + (4, 5, 6) # => (1, 2, 3, 4, 5, 6) +tup[:2] # => (1, 2) +2 in tup # => True # 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 @@ -208,60 +210,60 @@ empty_dict = {} filled_dict = {"one": 1, "two": 2, "three": 3} # Look up values with [] -filled_dict["one"] #=> 1 +filled_dict["one"] # => 1 # Get all keys as a list with "keys()" -filled_dict.keys() #=> ["three", "two", "one"] +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()" -filled_dict.values() #=> [3, 2, 1] +filled_dict.values() # => [3, 2, 1] # Note - Same as above regarding key ordering. # Check for existence of keys in a dictionary with "in" -"one" in filled_dict #=> True -1 in filled_dict #=> False +"one" in filled_dict # => True +1 in filled_dict # => False # Looking up a non-existing key is a KeyError -filled_dict["four"] # KeyError +filled_dict["four"] # KeyError # Use "get()" method to avoid the KeyError -filled_dict.get("one") #=> 1 -filled_dict.get("four") #=> None +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 +filled_dict.get("one", 4) # => 1 +filled_dict.get("four", 4) # => 4 # "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 +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 -some_set = set([1,2,2,3,4]) # some_set is now set([1, 2, 3, 4]) +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} +filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5} # Do set intersection with & other_set = {3, 4, 5, 6} -filled_set & other_set #=> {3, 4, 5} +filled_set & other_set # => {3, 4, 5} # Do set union with | -filled_set | other_set #=> {1, 2, 3, 4, 5, 6} +filled_set | other_set # => {1, 2, 3, 4, 5, 6} # Do set difference with - -{1,2,3,4} - {2,3,5} #=> {1, 4} +{1, 2, 3, 4} - {2, 3, 5} # => {1, 4} # Check for existence in a set with in -2 in filled_set #=> True -10 in filled_set #=> False +2 in filled_set # => True +10 in filled_set # => False #################################################### @@ -337,17 +339,18 @@ def add(x, y): return x + y # Return values with a return statement # Calling functions with parameters -add(5, 6) #=> prints out "x is 5 and y is 6" and returns 11 +add(5, 6) # => prints out "x is 5 and y is 6" and returns 11 # 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 def varargs(*args): return args -varargs(1, 2, 3) #=> (1,2,3) +varargs(1, 2, 3) # => (1, 2, 3) # You can define functions that take a variable number of @@ -356,7 +359,8 @@ def keyword_args(**kwargs): return kwargs # Let's call it to see what happens -keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} +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): @@ -372,9 +376,10 @@ all_the_args(1, 2, a=3, b=4) prints: # Use * to expand tuples and use ** to expand kwargs. args = (1, 2, 3, 4) kwargs = {"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) +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): @@ -383,23 +388,24 @@ def create_adder(x): return adder add_10 = create_adder(10) -add_10(3) #=> 13 +add_10(3) # => 13 # There are also anonymous functions -(lambda x: x > 2)(3) #=> True +(lambda x: x > 2)(3) # => True # There are built-in higher order functions -map(add_10, [1,2,3]) #=> [11, 12, 13] -filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7] +map(add_10, [1, 2, 3]) # => [11, 12, 13] +filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7] # We can use list comprehensions for nice maps and filters -[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] +[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] #################################################### ## 5. Classes #################################################### + # We subclass from object to get a class. class Human(object): @@ -413,7 +419,7 @@ class Human(object): # An instance method. All methods take "self" as the first argument def say(self, msg): - return "%s: %s" % (self.name, msg) + return "%s: %s" % (self.name, msg) # A class method is shared among all instances # They are called with the calling class as the first argument @@ -432,18 +438,18 @@ i = Human(name="Ian") 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" +i.get_species() # => "H. sapiens" # Change the shared attribute Human.species = "H. neanderthalensis" -i.get_species() #=> "H. neanderthalensis" -j.get_species() #=> "H. neanderthalensis" +i.get_species() # => "H. neanderthalensis" +j.get_species() # => "H. neanderthalensis" # Call the static method -Human.grunt() #=> "*grunt*" +Human.grunt() # => "*grunt*" #################################################### @@ -452,12 +458,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 @@ -465,7 +471,7 @@ from math import * # You can shorten module names import math as m -math.sqrt(16) == m.sqrt(16) #=> True +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 @@ -486,10 +492,12 @@ def double_numbers(iterable): for i in iterable: yield i + i -# generator creates the value on the fly -# instead of generating and returning all values at once it creates one in each iteration -# this means values bigger than 15 wont be processed in double_numbers -# note range is a generator too, creating a list 1-900000000 would take lot of time to be made +# A generator creates values on the fly. +# Instead of generating and returning all values at once it creates one in each +# iteration. This means values bigger than 15 wont be processed in +# double_numbers. +# Note range is a generator too. Creating a list 1-900000000 would take lot of +# time to be made _range = range(1, 900000000) # will double all numbers until a result >=30 found for i in double_numbers(_range): @@ -500,7 +508,8 @@ for i in double_numbers(_range): # Decorators # in this example beg wraps say -# Beg will call say. If say_please is True then it will change the returned message +# Beg will call say. If say_please is True then it will change the returned +# message from functools import wraps @@ -523,8 +532,6 @@ def say(say_please=False): print(say()) # Can you buy me a beer? print(say(say_please=True)) # Can you buy me a beer? Please! I am poor :( - - ``` ## Ready For More? -- cgit v1.2.3 From 777c333ff078a7095f9b7ac303829b249f499b21 Mon Sep 17 00:00:00 2001 From: Sam Dodrill Date: Mon, 14 Apr 2014 11:04:44 -0700 Subject: Remove references to hash and hashtag in favor of number symbol --- 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 ced01910..bbc1bd92 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -17,7 +17,7 @@ to Python 2.x. Look for another tour of Python 3 soon! ```python -# Single line comments start with a hash. +# Single line comments start with a number symbol. """ Multiline strings can be written using three "'s, and are often used -- cgit v1.2.3 From fce8598a749fe885157c9582fc437f57fa111f3d Mon Sep 17 00:00:00 2001 From: Juan David Pastas Date: Thu, 1 May 2014 13:53:01 -0500 Subject: Python: clearer add lists note. --- 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 bbc1bd92..210c9619 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -173,7 +173,7 @@ li[::-1] # => [3, 4, 2, 1] 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 +li + other_li # => [1, 2, 3, 4, 5, 6] - Note: values for li and for other_li are not modified. # Concatenate lists with "extend()" li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] -- cgit v1.2.3 From 2a9a4f39babe369395de580d0bd7f405a0283803 Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 13 Jul 2014 14:51:34 -0400 Subject: [python/en] added int div, modulo, and scoping Added integer or truncation division, the modulo operator which were missing from operators section. Added function scoping in the functions section. --- python.html.markdown | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 210c9619..d1b88b25 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -45,6 +45,13 @@ to Python 2.x. Look for another tour of Python 3 soon! 2.0 # This is a float 11.0 / 4.0 # => 2.75 ahhh...much better +# Truncation or Integer division +5 // 3 # => 1 +5.0 // 3.0 # => 1.0 + +# Modulo operation +7 % 3 # => 1 + # Enforce precedence with parentheses (1 + 3) * 2 # => 8 @@ -380,6 +387,22 @@ 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) +# Function Scope +x = 5 + +def setX(num): + # Local var x not the same as global variable x + x = num # => 43 + print (x) # => 43 + +def setGlobalX(num): + global x + print (x) # => 5 + x = num # global var x is now set to 6 + print (x) # => 6 + +setX(43) +setGlobalX(6) # Python has first class functions def create_adder(x): -- cgit v1.2.3 From 0dcc5640b6e1a9bba857114e1257a29aa9829ce9 Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 13 Jul 2014 14:52:31 -0400 Subject: [python/en] added int div, modulo, and scoping Added integer or truncation division, the modulo operator which were missing from operators section. Added function scoping in the functions section. --- 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 d1b88b25..aa077e57 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -47,7 +47,7 @@ to Python 2.x. Look for another tour of Python 3 soon! # Truncation or Integer division 5 // 3 # => 1 -5.0 // 3.0 # => 1.0 +5.0 // 3.0 # => 1.0 # works on floats too # Modulo operation 7 % 3 # => 1 -- cgit v1.2.3 From e6a289be491c9b0f1a7c2d5b364de0c4c8241bd9 Mon Sep 17 00:00:00 2001 From: Shashwat986 Date: Sat, 2 Aug 2014 19:37:28 +0530 Subject: Update python.html.markdown Added more details to try/except section. --- 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 aa077e57..5bec5190 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -334,6 +334,10 @@ try: raise IndexError("This is an index error") except IndexError as e: pass # Pass is just a no-op. Usually you would do recovery here. +except (TypeError, NameError): + pass # Multiple exceptions can be handled together, if required. +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 #################################################### -- cgit v1.2.3 From 65a1b6279fa1038efa013f64d98e7e947b527567 Mon Sep 17 00:00:00 2001 From: Shashwat986 Date: Tue, 5 Aug 2014 01:06:54 +0530 Subject: Update python.html.markdown range() is not a generator in python 2.x --- python.html.markdown | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 5bec5190..b7d5895a 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -523,11 +523,13 @@ def double_numbers(iterable): # Instead of generating and returning all values at once it creates one in each # iteration. This means values bigger than 15 wont be processed in # double_numbers. -# Note range is a generator too. Creating a list 1-900000000 would take lot of -# time to be made -_range = range(1, 900000000) +# Note xrange is a generator that does the same thing range does. +# Creating a list 1-900000000 would take lot of time and space to be made. +# xrange creates an xrange generator object instead of creating the entire list like range does. +_xrange = xrange(1, 900000000) + # will double all numbers until a result >=30 found -for i in double_numbers(_range): +for i in double_numbers(_xrange): print(i) if i >= 30: break -- cgit v1.2.3 From 472142d11f8052f6042a8cd4366c7ea7d6680c0d Mon Sep 17 00:00:00 2001 From: "Mikael E. Wikner" Date: Wed, 30 Jul 2014 11:28:21 +0200 Subject: Squashed commits to prepare for merge moved underscore for range variable, added comment renamed parameter in beg decorator added comment about double underscores Update python.html.markdown --- python.html.markdown | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index b7d5895a..73963a3c 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -439,7 +439,10 @@ class Human(object): # A class attribute. It is shared by all instances of this class species = "H. sapiens" - # Basic initializer + # Basic initializer, this is called when this class is instantiated. + # Note that the double leading and trailing underscores denote objects + # or attributes that are used by python but that live in user-controlled + # namespaces. You should not invent such names on your own. def __init__(self, name): # Assign the argument to the instance's name attribute self.name = name @@ -526,10 +529,12 @@ def double_numbers(iterable): # Note xrange is a generator that does the same thing range does. # Creating a list 1-900000000 would take lot of time and space to be made. # xrange creates an xrange generator object instead of creating the entire list like range does. -_xrange = xrange(1, 900000000) +# We use a trailing underscore in variable names when we want to use a name that +# would normally collide with a python keyword +xrange_ = xrange(1, 900000000) # will double all numbers until a result >=30 found -for i in double_numbers(_xrange): +for i in double_numbers(xrange_): print(i) if i >= 30: break @@ -542,10 +547,10 @@ for i in double_numbers(_xrange): from functools import wraps -def beg(_say): - @wraps(_say) +def beg(target_function): + @wraps(target_function) def wrapper(*args, **kwargs): - msg, say_please = _say(*args, **kwargs) + msg, say_please = target_function(*args, **kwargs) if say_please: return "{} {}".format(msg, "Please! I am poor :(") return msg -- cgit v1.2.3 From daf83c91232a259eee4cadef5f28210b7c83316d Mon Sep 17 00:00:00 2001 From: Evgeniy Ginzburg Date: Wed, 6 Aug 2014 23:43:37 +0300 Subject: aded negative integer division same as in python3 --- python.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index b7d5895a..312e3c15 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -45,9 +45,11 @@ to Python 2.x. Look for another tour of Python 3 soon! 2.0 # This is a float 11.0 / 4.0 # => 2.75 ahhh...much better -# Truncation or Integer division +# Result of integer division truncated down both for positive and negative. 5 // 3 # => 1 5.0 // 3.0 # => 1.0 # works on floats too +-5 // 3 # => -2 +-5.0 // 3.0 # => -2.0 # Modulo operation 7 % 3 # => 1 -- cgit v1.2.3 From 62f4c0de22452177d96ba24ce0cf8cce119af47b Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 7 Sep 2014 19:22:01 -0400 Subject: [python/en] Added Bool operators Bool operators --- python.html.markdown | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 9057dde2..6b2a40de 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -57,9 +57,17 @@ to Python 2.x. Look for another tour of Python 3 soon! # Enforce precedence with parentheses (1 + 3) * 2 # => 8 -# Boolean values are primitives -True -False +# Boolean Operators ++# Note "and" and "or" are case-sensitive ++True and False #=> False ++False or True #=> True ++ ++# Note with ints ++0 and 2 #=> 0 ++-5 or 0 #=> -5 ++0 == False #=> True ++2 == True #=> False +1 == True #=> True # negate with not not True # => False -- cgit v1.2.3 From 81b4f49e999a6ec41bd1f79d77208d2db0a29871 Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 7 Sep 2014 19:32:46 -0400 Subject: [python/en] Added Bool operators Modified Bool operators --- 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 6b2a40de..390c7b76 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -62,7 +62,7 @@ to Python 2.x. Look for another tour of Python 3 soon! +True and False #=> False +False or True #=> True + -+# Note with ints ++# Note using Bool operators with ints +0 and 2 #=> 0 +-5 or 0 #=> -5 +0 == False #=> True -- cgit v1.2.3 From 6629b5cd39b4d87ef47b57060d050919c7f40aa8 Mon Sep 17 00:00:00 2001 From: Alexander Sologub Date: Thu, 9 Oct 2014 19:50:55 +0300 Subject: [python/en] Removed unnecessary plus signs in bool operators section --- python.html.markdown | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 390c7b76..42a1f63a 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -58,15 +58,15 @@ to Python 2.x. Look for another tour of Python 3 soon! (1 + 3) * 2 # => 8 # Boolean Operators -+# Note "and" and "or" are case-sensitive -+True and False #=> False -+False or True #=> True -+ -+# Note using Bool operators with ints -+0 and 2 #=> 0 -+-5 or 0 #=> -5 -+0 == False #=> True -+2 == True #=> False +# Note "and" and "or" are case-sensitive +True and False #=> False +False or True #=> True + +# Note using Bool operators with ints +0 and 2 #=> 0 +-5 or 0 #=> -5 +0 == False #=> True +2 == True #=> False 1 == True #=> True # negate with not -- cgit v1.2.3 From 39d8753d5285348b998a7b87ae68614acf816df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=20Polykanine=20A=2EK=2EA=2E=20Menelion=20Elens=C3=BA?= =?UTF-8?q?l=C3=AB?= Date: Thu, 9 Oct 2014 22:49:47 +0300 Subject: [python/en] Adding exponentiation operator --- 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 390c7b76..a980b6dc 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -3,6 +3,7 @@ language: python contributors: - ["Louie Dinh", "http://ldinh.ca"] - ["Amin Bandali", "http://aminbandali.com"] + - "Andre Polykanine", "https://github.com/Oire"] filename: learnpython.py --- @@ -54,6 +55,9 @@ to Python 2.x. Look for another tour of Python 3 soon! # Modulo operation 7 % 3 # => 1 +# Exponentiation (x to the y'th power) +2 ** 4 # => 16 + # Enforce precedence with parentheses (1 + 3) * 2 # => 8 -- cgit v1.2.3 From 68c9aed1346fe20df741014de415f4297460dc67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=20Polykanine=20A=2EK=2EA=2E=20Menelion=20Elens=C3=BA?= =?UTF-8?q?l=C3=AB?= Date: Thu, 9 Oct 2014 23:06:24 +0300 Subject: Corrected as per @iirelu's comment --- 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 a980b6dc..cac9d539 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -56,7 +56,7 @@ to Python 2.x. Look for another tour of Python 3 soon! 7 % 3 # => 1 # Exponentiation (x to the y'th power) -2 ** 4 # => 16 +2**4 # => 16 # Enforce precedence with parentheses (1 + 3) * 2 # => 8 -- cgit v1.2.3 From a2043f3adbbeaca72c7ef15c4505924903111c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=20Polykanine=20A=2EK=2EA=2E=20Menelion=20Elens=C3=BA?= =?UTF-8?q?l=C3=AB?= Date: Thu, 9 Oct 2014 23:22:24 +0300 Subject: Gosh, what happened to me today?( Fixed the left bracket --- 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 cac9d539..febfa119 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -3,7 +3,7 @@ language: python contributors: - ["Louie Dinh", "http://ldinh.ca"] - ["Amin Bandali", "http://aminbandali.com"] - - "Andre Polykanine", "https://github.com/Oire"] + - ["Andre Polykanine", "https://github.com/Oire"] filename: learnpython.py --- -- cgit v1.2.3 From 0698ed27c1c63100fabf01be654a412186982042 Mon Sep 17 00:00:00 2001 From: Jack Hooper Date: Wed, 12 Nov 2014 00:28:03 +1100 Subject: There's no apostrophe in 90s Apostrophes are for indicating an abbreviation or for denoting possession. '90s' is merely a plural. It is an abbreviation of 1990s, so technically it ought to be '90s, but for all intents and purposes this is optional. --- 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 ba236fb3..411194f4 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -7,7 +7,7 @@ contributors: filename: learnpython.py --- -Python was created by Guido Van Rossum in the early 90's. It is now one of the most popular +Python was created by Guido Van Rossum in the early 90s. It is now one of the most popular languages in existence. I fell in love with Python for its syntactic clarity. It's basically executable pseudocode. -- cgit v1.2.3 From 0429c31d6d640889cd40ad0fba89a476bf1e7480 Mon Sep 17 00:00:00 2001 From: Jack Hooper Date: Wed, 12 Nov 2014 00:30:19 +1100 Subject: Updated note regarding Python 3 tutorial. --- 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 411194f4..961d0965 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -14,7 +14,7 @@ executable pseudocode. Feedback would be highly appreciated! You can reach me at [@louiedinh](http://twitter.com/louiedinh) or louiedinh [at] [google's email service] Note: This article applies to Python 2.7 specifically, but should be applicable -to Python 2.x. Look for another tour of Python 3 soon! +to Python 2.x. For Python 3.x, take a look at the Python 3 tutorial. ```python -- cgit v1.2.3 From 4d85f161d8399f80e3c7ca93eb7ad8ed8018a327 Mon Sep 17 00:00:00 2001 From: Jack Hooper Date: Wed, 12 Nov 2014 02:03:50 +1100 Subject: More apostrophe fixes I hate to be that guy, but... I found a couple more. --- python.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 961d0965..f7b0082c 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -21,7 +21,7 @@ to Python 2.x. For Python 3.x, take a look at the Python 3 tutorial. # Single line comments start with a number symbol. """ Multiline strings can be written - using three "'s, and are often used + using three "s, and are often used as comments """ @@ -55,7 +55,7 @@ to Python 2.x. For Python 3.x, take a look at the Python 3 tutorial. # Modulo operation 7 % 3 # => 1 -# Exponentiation (x to the y'th power) +# Exponentiation (x to the yth power) 2**4 # => 16 # Enforce precedence with parentheses -- cgit v1.2.3 From d886036b59f14da678536b1a2a57c6ad2fadee08 Mon Sep 17 00:00:00 2001 From: Doug Ilijev Date: Mon, 17 Nov 2014 01:26:19 -0600 Subject: [python/en] Fix 80 col limit and clarify a few points. --- python.html.markdown | 60 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 16 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index f7b0082c..53381f32 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -102,6 +102,9 @@ not False # => True # Strings can be added too! "Hello " + "world!" # => "Hello world!" +# ... or multiplied +"Hello" * 3 # => "HelloHelloHello" + # A string can be treated like a list of characters "This is a string"[0] # => 'T' @@ -136,11 +139,12 @@ bool("") # => False ## 2. Variables and Collections #################################################### -# 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!" - +# Python has a print statement, in all 2.x versions but removed from 3. +print "I'm Python. Nice to meet you!" +# Python also has a print function, available in versions 2.7 and 3... +# but for 2.7 you need to add the import (uncommented): +# from __future__ import print_function +print("I'm also Python! ") # No need to declare variables before assigning to them. some_var = 5 # Convention is to use lower_case_with_underscores @@ -170,6 +174,10 @@ li.append(3) # li is now [1, 2, 4, 3] again. # Access a list like you would any array li[0] # => 1 +# Assign new values to indexes that have already been initialized with = +li[0] = 42 +li[0] # => 42 +li[0] = 1 # Note: setting it back to the original value # Look at the last element li[-1] # => 3 @@ -194,7 +202,8 @@ li[::-1] # => [3, 4, 2, 1] del li[2] # li is now [1, 2, 3] # You can add lists -li + other_li # => [1, 2, 3, 4, 5, 6] - Note: values for li and for other_li are not modified. +li + other_li # => [1, 2, 3, 4, 5, 6] +# Note: values for li and for other_li are not modified. # Concatenate lists with "extend()" li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] @@ -255,17 +264,25 @@ 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 +# note that filled_dict.get("four") is still => 4 +# (get doesn't set the value in the dictionary) + +# set the value of a key with a syntax similar to lists +filled_dict["four"] = 4 # now, filled_dict["four"] => 4 # "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 -# Sets store ... well sets +# Sets store ... well sets (which are like lists but can contain no duplicates) empty_set = set() # 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]) +# order is not guaranteed, even though it may sometimes look sorted +another_set = set([4, 3, 2, 2, 1]) # another_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} @@ -371,7 +388,7 @@ add(y=6, x=5) # Keyword arguments can arrive in any order. # You can define functions that take a variable number of -# positional arguments +# positional args, which will be interpreted as a tuple if you do not use the * def varargs(*args): return args @@ -379,7 +396,7 @@ varargs(1, 2, 3) # => (1, 2, 3) # You can define functions that take a variable number of -# keyword arguments, as well +# keyword args, as well, which will be interpreted as a map if you do not use ** def keyword_args(**kwargs): return kwargs @@ -398,26 +415,33 @@ all_the_args(1, 2, a=3, b=4) prints: """ # When calling functions, you can do the opposite of args/kwargs! -# Use * to expand tuples and use ** to expand kwargs. +# Use * to expand positional args and use ** to expand keyword args. args = (1, 2, 3, 4) kwargs = {"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) +# you can pass args and kwargs along to other functions that take args/kwargs +# by expanding them with * and ** respectively +def pass_all_the_args(*args, **kwargs): + all_the_args(*args, **kwargs) + print varargs(*args) + print keyword_args(**kwargs) + # Function Scope x = 5 def setX(num): # Local var x not the same as global variable x x = num # => 43 - print (x) # => 43 + print x # => 43 def setGlobalX(num): global x - print (x) # => 5 + print x # => 5 x = num # global var x is now set to 6 - print (x) # => 6 + print x # => 6 setX(43) setGlobalX(6) @@ -442,11 +466,11 @@ 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] + #################################################### ## 5. Classes #################################################### - # We subclass from object to get a class. class Human(object): @@ -516,6 +540,9 @@ from math import * # You can shorten module names import math as m math.sqrt(16) == m.sqrt(16) # => True +# you can also test that the functions are equivalent +from math import sqrt +math.sqrt == m.sqrt == sqrt # => True # Python modules are just ordinary python files. You # can write your own, and import them. The name of the @@ -542,8 +569,9 @@ def double_numbers(iterable): # double_numbers. # Note xrange is a generator that does the same thing range does. # Creating a list 1-900000000 would take lot of time and space to be made. -# xrange creates an xrange generator object instead of creating the entire list like range does. -# We use a trailing underscore in variable names when we want to use a name that +# xrange creates an xrange generator object instead of creating the entire list +# like range does. +# We use a trailing underscore in variable names when we want to use a name that # would normally collide with a python keyword xrange_ = xrange(1, 900000000) -- cgit v1.2.3 From 28fcbc8e1c7d22bbc192418b4f3513f2ca4ead3e Mon Sep 17 00:00:00 2001 From: Louie Dinh Date: Tue, 23 Dec 2014 11:23:22 -0800 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 53381f32..da04d381 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -264,7 +264,7 @@ 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 -# note that filled_dict.get("four") is still => 4 +# note that filled_dict.get("four") is still => None # (get doesn't set the value in the dictionary) # set the value of a key with a syntax similar to lists -- cgit v1.2.3 From 22a0f44e64e6c7ac969556b9648ebcfca4bad187 Mon Sep 17 00:00:00 2001 From: suuuzi Date: Wed, 4 Feb 2015 15:36:20 -0200 Subject: Refering Python3 tutorial link --- 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 da04d381..478804cd 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -14,7 +14,7 @@ executable pseudocode. Feedback would be highly appreciated! You can reach me at [@louiedinh](http://twitter.com/louiedinh) or louiedinh [at] [google's email service] Note: This article applies to Python 2.7 specifically, but should be applicable -to Python 2.x. For Python 3.x, take a look at the Python 3 tutorial. +to Python 2.x. For Python 3.x, take a look at the [Python 3 tutorial](http://learnxinyminutes.com/docs/python3/). ```python -- cgit v1.2.3 From 2ad26dd44a9129aade696ac64d9d6ae1a1f6a98b Mon Sep 17 00:00:00 2001 From: Sriram Sundarraj Date: Thu, 23 Apr 2015 01:30:40 +0530 Subject: [python/en] Adding strings without + added. --- 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 478804cd..63547bf6 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -101,6 +101,8 @@ not False # => True # Strings can be added too! "Hello " + "world!" # => "Hello world!" +# Strings can be added without using '+' +"Hello " "world!" # => "Hello world!" # ... or multiplied "Hello" * 3 # => "HelloHelloHello" -- cgit v1.2.3 From 98aa4ef43c84b9bdaa988971174bbb21e042e6ac Mon Sep 17 00:00:00 2001 From: Sriram Sundarraj Date: Fri, 24 Apr 2015 01:50:17 +0530 Subject: [python/en] Added range(start, stop). --- python.html.markdown | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 63547bf6..668e04f9 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -346,6 +346,18 @@ prints: for i in range(4): print(i) +""" +"range(lower, upper)" returns a list of numbers +from the lower number to the upper number +prints: + 4 + 5 + 6 + 7 +""" +for i in range(4, 8): + print(i) + """ While loops go until a condition is no longer met. prints: -- cgit v1.2.3 From 6b6342a7dc8d4280eb4465894bc15cdd5b7ff383 Mon Sep 17 00:00:00 2001 From: Sriram Sundarraj Date: Fri, 24 Apr 2015 22:57:33 +0530 Subject: [python/en] Moved to default python2 print statements. --- python.html.markdown | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 668e04f9..899a97f8 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -145,8 +145,10 @@ bool("") # => False print "I'm Python. Nice to meet you!" # Python also has a print function, available in versions 2.7 and 3... # but for 2.7 you need to add the import (uncommented): -# from __future__ import print_function +from __future__ import print_function print("I'm also Python! ") +# This is useful to maintain compatibility, +# but for this doc, we'll use the python 2 print statement # No need to declare variables before assigning to them. some_var = 5 # Convention is to use lower_case_with_underscores @@ -316,11 +318,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." """ @@ -332,7 +334,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 @@ -344,7 +346,7 @@ prints: 3 """ for i in range(4): - print(i) + print i """ "range(lower, upper)" returns a list of numbers @@ -356,7 +358,7 @@ prints: 7 """ for i in range(4, 8): - print(i) + print i """ While loops go until a condition is no longer met. @@ -368,7 +370,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 @@ -391,7 +393,7 @@ else: # Optional clause to the try/except block. Must follow all except blocks # 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 @@ -420,8 +422,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) @@ -517,10 +519,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" @@ -540,12 +542,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 @@ -591,7 +593,7 @@ xrange_ = xrange(1, 900000000) # will double all numbers until a result >=30 found for i in double_numbers(xrange_): - print(i) + print i if i >= 30: break @@ -620,8 +622,8 @@ def say(say_please=False): return msg, say_please -print(say()) # Can you buy me a beer? -print(say(say_please=True)) # Can you buy me a beer? Please! I am poor :( +print say() # Can you buy me a beer? +print say(say_please=True) # Can you buy me a beer? Please! I am poor :( ``` ## Ready For More? -- cgit v1.2.3 From c2963cdc31ea9c0cce9d5abf8b0a6d606d11b3a0 Mon Sep 17 00:00:00 2001 From: Sriram Sundarraj Date: Fri, 24 Apr 2015 23:29:01 +0530 Subject: [python/en] Removed python3 print. --- python.html.markdown | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 899a97f8..7281a330 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -141,14 +141,8 @@ bool("") # => False ## 2. Variables and Collections #################################################### -# Python has a print statement, in all 2.x versions but removed from 3. +# Python has a print statement print "I'm Python. Nice to meet you!" -# Python also has a print function, available in versions 2.7 and 3... -# but for 2.7 you need to add the import (uncommented): -from __future__ import print_function -print("I'm also Python! ") -# This is useful to maintain compatibility, -# but for this doc, we'll use the python 2 print statement # No need to declare variables before assigning to them. some_var = 5 # Convention is to use lower_case_with_underscores -- cgit v1.2.3 From e13e00945dc4bb20c7cf6b9cd4af87f73b965ddb Mon Sep 17 00:00:00 2001 From: Rinoc Johnson Date: Thu, 30 Apr 2015 17:56:01 -0400 Subject: Fix Python typo. --- 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 7281a330..f081a6a7 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -46,7 +46,7 @@ to Python 2.x. For Python 3.x, take a look at the [Python 3 tutorial](http://lea 2.0 # This is a float 11.0 / 4.0 # => 2.75 ahhh...much better -# Result of integer division truncated down both for positive and negative. +# Result of integer division truncated down both for positive and negative. 5 // 3 # => 1 5.0 // 3.0 # => 1.0 # works on floats too -5 // 3 # => -2 @@ -191,7 +191,7 @@ li[2:] # => [4, 3] li[:3] # => [1, 2, 4] # Select every second entry li[::2] # =>[1, 4] -# Revert the list +# Reverse the list li[::-1] # => [3, 4, 2, 1] # Use any combination of these to make advanced slices # li[start:end:step] @@ -439,14 +439,14 @@ def pass_all_the_args(*args, **kwargs): print varargs(*args) print keyword_args(**kwargs) -# Function Scope +# Function Scope x = 5 def setX(num): # Local var x not the same as global variable x x = num # => 43 print x # => 43 - + def setGlobalX(num): global x print x # => 5 -- cgit v1.2.3 From 5ebe2dcb6eeaf7e34daf07f76dcfa2403f05a332 Mon Sep 17 00:00:00 2001 From: Rinoc Johnson Date: Thu, 30 Apr 2015 19:31:38 -0400 Subject: Clarify wording for list reversal. --- python.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index f081a6a7..ace3f794 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -191,14 +191,14 @@ li[2:] # => [4, 3] li[:3] # => [1, 2, 4] # Select every second entry li[::2] # =>[1, 4] -# Reverse the list +# Reverse a copy of the list li[::-1] # => [3, 4, 2, 1] # Use any combination of these to make advanced slices # li[start:end:step] # Remove arbitrary elements from a list with "del" del li[2] # li is now [1, 2, 3] - +r # You can add lists li + other_li # => [1, 2, 3, 4, 5, 6] # Note: values for li and for other_li are not modified. -- cgit v1.2.3 From 92096a65ed309e84439f84f7b410a044386a2959 Mon Sep 17 00:00:00 2001 From: Michael Herman Date: Sun, 5 Jul 2015 08:35:31 -0600 Subject: python/en - added two resources --- 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 ace3f794..354fd9a1 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -630,10 +630,12 @@ print say(say_please=True) # Can you buy me a beer? Please! I am poor :( * [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) +* [First Steps With Python](https://realpython.com/learn/python-first-steps/) ### Dead Tree * [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20) * [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20) * [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20) +* [Real Python](https://realpython.com/) -- cgit v1.2.3 From c6caa7664c53d8decb2076987f601a7c99fd6951 Mon Sep 17 00:00:00 2001 From: Michael Herman Date: Sun, 5 Jul 2015 21:12:12 -0600 Subject: Update python.html.markdown --- python.html.markdown | 2 -- 1 file changed, 2 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 354fd9a1..b89fe57d 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -637,5 +637,3 @@ print say(say_please=True) # Can you buy me a beer? Please! I am poor :( * [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20) * [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20) * [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20) -* [Real Python](https://realpython.com/) - -- cgit v1.2.3 From d995a0ae2792b3e6acc832438b84e9fed570d0e4 Mon Sep 17 00:00:00 2001 From: Mark Miller <725mrm@gmail.com> Date: Thu, 16 Jul 2015 16:45:25 -0500 Subject: Changed %s formatting to {n} style. If line 117 is accurate, then {n} style string formatting is the preferred style, and it should be reflected in the examples. --- 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 b89fe57d..3b233f7f 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -327,8 +327,8 @@ prints: mouse is a mammal """ for animal in ["dog", "cat", "mouse"]: - # You can use % to interpolate formatted strings - print "%s is a mammal" % animal + # You can use {0} to interpolate formatted strings. (See above.) + print "{0} is a mammal".format(animal) """ "range(number)" returns a list of numbers @@ -387,7 +387,7 @@ else: # Optional clause to the try/except block. Must follow all except blocks # Use "def" to create new functions def add(x, y): - print "x is %s and y is %s" % (x, y) + print "x is {0} and y is {1}".format(x, y) return x + y # Return values with a return statement # Calling functions with parameters @@ -497,7 +497,7 @@ class Human(object): # An instance method. All methods take "self" as the first argument def say(self, msg): - return "%s: %s" % (self.name, msg) + return "{0}: {1}".format(self.name, msg) # A class method is shared among all instances # They are called with the calling class as the first argument -- cgit v1.2.3 From 8a968db2bc0f596e2e9d865b73d909b02ff774d6 Mon Sep 17 00:00:00 2001 From: Al Sweigart Date: Thu, 23 Jul 2015 11:24:40 -0700 Subject: Added links to the free book "Automate the Boring Stuff with Python" to the "Free Online" section. --- 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 3b233f7f..88e0deb1 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -624,6 +624,7 @@ print say(say_please=True) # Can you buy me a beer? Please! I am poor :( ### Free Online +* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com) * [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 a24e5ef7a115eb32e929a003fdfcdb3704927064 Mon Sep 17 00:00:00 2001 From: Denis Gladkikh Date: Tue, 1 Sep 2015 09:27:40 -0700 Subject: Python: add finally and with statements https://docs.python.org/2/tutorial/errors.html --- python.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 88e0deb1..9493e6a3 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -379,7 +379,13 @@ except (TypeError, NameError): pass # Multiple exceptions can be handled together, if required. 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 with +with open("myfile.txt") as f: + for line in f: + print line #################################################### ## 4. Functions -- cgit v1.2.3 From 3e93c5e5f4a4f968a2371dc0b69047bc78da4640 Mon Sep 17 00:00:00 2001 From: Denis Gladkikh Date: Tue, 1 Sep 2015 10:23:31 -0700 Subject: Update python.html.markdown --- 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 9493e6a3..16a94c8f 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -382,7 +382,7 @@ else: # Optional clause to the try/except block. Must follow all except blocks finally: # Execute under all circumstances print "We can clean up resources here" -# Instead of try/finally to cleanup resources you can use with +# Instead of try/finally to cleanup resources you can use a with statement with open("myfile.txt") as f: for line in f: print line -- cgit v1.2.3 From 09874aa5c008c3944093dc2d3a5ddca489e2c998 Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Wed, 30 Sep 2015 10:48:59 +0800 Subject: map->dict --- 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 16a94c8f..352f7349 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -412,7 +412,7 @@ varargs(1, 2, 3) # => (1, 2, 3) # You can define functions that take a variable number of -# keyword args, as well, which will be interpreted as a map if you do not use ** +# keyword args, as well, which will be interpreted as a dict if you do not use ** def keyword_args(**kwargs): return kwargs -- cgit v1.2.3 From f35472d8d268f3e07dbbcccf953882d425a53240 Mon Sep 17 00:00:00 2001 From: Jesse Huang Date: Fri, 2 Oct 2015 14:00:54 -0400 Subject: Removed random "r" --- 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 352f7349..5572e38e 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -198,7 +198,7 @@ li[::-1] # => [3, 4, 2, 1] # Remove arbitrary elements from a list with "del" del li[2] # li is now [1, 2, 3] -r + # You can add lists li + other_li # => [1, 2, 3, 4, 5, 6] # Note: values for li and for other_li are not modified. -- cgit v1.2.3