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