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 From 777423dcc519b2f25bbcf5238c12ee3a0d4c67c9 Mon Sep 17 00:00:00 2001 From: Cadel Watson Date: Wed, 7 Oct 2015 17:01:28 +1100 Subject: Add examples of higher-order functions taking multiple arguments --- 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 5572e38e..6cfb5dca 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -473,9 +473,12 @@ add_10(3) # => 13 # There are also anonymous functions (lambda x: x > 2)(3) # => True +(lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5 # There are built-in higher order functions map(add_10, [1, 2, 3]) # => [11, 12, 13] +map(max, [1, 2, 3], [4, 2, 1]) # => [4, 2, 3] + filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7] # We can use list comprehensions for nice maps and filters -- cgit v1.2.3 From 9ee3a687204cbe6caaa11b9fd28470651a70ae2b Mon Sep 17 00:00:00 2001 From: Ankit Aggarwal Date: Sun, 11 Oct 2015 19:51:43 +0530 Subject: Added input operations in learn Python 2.7 doc --- python.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 6cfb5dca..95146ca8 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -4,6 +4,7 @@ contributors: - ["Louie Dinh", "http://ldinh.ca"] - ["Amin Bandali", "http://aminbandali.com"] - ["Andre Polykanine", "https://github.com/Oire"] + - ["Ankit Aggarwal", "http://ankitaggarwal.me"] filename: learnpython.py --- @@ -144,6 +145,10 @@ bool("") # => False # Python has a print statement print "I'm Python. Nice to meet you!" +# Simple way to get input data from console +input_string_var = raw_input("Enter some data: ") # Data is stored as a string +input_number_var = input("Enter a number: ") # Data is stored as a number + # No need to declare variables before assigning to them. some_var = 5 # Convention is to use lower_case_with_underscores some_var # => 5 -- cgit v1.2.3 From ba06e9af37fda3dd363ab5a39bf03cd65bbe2ddd Mon Sep 17 00:00:00 2001 From: Ananya W Cleetus Date: Mon, 12 Oct 2015 11:56:37 -0400 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 6cfb5dca..5b36083d 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -142,7 +142,7 @@ bool("") # => False #################################################### # Python has a print statement -print "I'm Python. Nice to meet you!" +print "I'm Python. Nice to meet you!" # => I'm Python. Nice to meet you! # 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 c05400477b05eb2f5f9ef7fb2d168c84e14fc26e Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Tue, 13 Oct 2015 17:56:48 -0700 Subject: Update Python 2 vs Python 3 Add fact that Python 2 is reaching end of life. And note that you can use `__future__` to add Python 2 and 3 compatible code. --- python.html.markdown | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 5b36083d..57a4d0d6 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -14,7 +14,13 @@ 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](http://learnxinyminutes.com/docs/python3/). +to Python 2.x. Python 2.7 is reachong end of life and will stop beeign maintained in 2020, +it is though recommended to start learnign Python with Python 3. +For Python 3.x, take a look at the [Python 3 tutorial](http://learnxinyminutes.com/docs/python3/). + +It is also possible to write Python code which is compatible with Python 2.7 and 3.x at the same time, +using Python [`__future__` imports](https://docs.python.org/2/library/__future__.html). `__future__` imports +allow you to write Python 3 code that will run on Python 2, so check out the Python 3 tutorial. ```python -- cgit v1.2.3 From e1d2f8c718858323e1716cc0bde4c652e9b87ce6 Mon Sep 17 00:00:00 2001 From: Ankit Aggarwal Date: Wed, 14 Oct 2015 10:27:25 +0530 Subject: Updated the input() method usage and description --- python.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 95146ca8..11642ff8 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -4,7 +4,6 @@ contributors: - ["Louie Dinh", "http://ldinh.ca"] - ["Amin Bandali", "http://aminbandali.com"] - ["Andre Polykanine", "https://github.com/Oire"] - - ["Ankit Aggarwal", "http://ankitaggarwal.me"] filename: learnpython.py --- @@ -146,8 +145,10 @@ bool("") # => False print "I'm Python. Nice to meet you!" # Simple way to get input data from console -input_string_var = raw_input("Enter some data: ") # Data is stored as a string -input_number_var = input("Enter a number: ") # Data is stored as a number +input_string_var = raw_input("Enter some data: ") # Returns the data as a string +input_var = input("Enter some data: ") # Evaluates the data as python code +# Warning: Caution is recommended for input() method usage +# Note: In python 3, input() is deprecated and raw_input() is renamed to input() # 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 375f0c18ae9b8d8732c480d0647becf1e82adf3e Mon Sep 17 00:00:00 2001 From: evuez Date: Wed, 14 Oct 2015 18:43:23 +0200 Subject: Add some stuff to lists, sets and classes This is meant to match Python 3 update. Lists: added remove, insert and index Sets: added ^, <=, >= operators Classes: added propery decorator (including getter, setter, deleter) --- python.html.markdown | 60 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 5b36083d..c66b7642 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -4,6 +4,7 @@ contributors: - ["Louie Dinh", "http://ldinh.ca"] - ["Amin Bandali", "http://aminbandali.com"] - ["Andre Polykanine", "https://github.com/Oire"] + - ["evuez", "http://github.com/evuez"] filename: learnpython.py --- @@ -206,6 +207,17 @@ li + other_li # => [1, 2, 3, 4, 5, 6] # Concatenate lists with "extend()" li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] +# Remove first occurrence of a value +li.remove(2) # li is now [1, 3, 4, 5, 6] +li.remove(2) # Raises a ValueError as 2 is not in the list + +# Insert an element at a specific index +li.insert(1, 2) # li is now [1, 2, 3, 4, 5, 6] again + +# Get the index of the first item found +li.index(2) # => 3 +li.index(7) # Raises a ValueError as 7 is not in the list + # Check for existence in a list with "in" 1 in li # => True @@ -297,6 +309,15 @@ filled_set | other_set # => {1, 2, 3, 4, 5, 6} # Do set difference with - {1, 2, 3, 4} - {2, 3, 5} # => {1, 4} +# Do set symmetric difference with ^ +{1, 2, 3, 4} ^ {2, 3, 5} # => {1, 4, 5} + +# Check if set on the left is a superset of set on the right +{1, 2} >= {1, 2, 3} # => False + +# Check if set on the left is a subset of set on the right +{1, 2} <= {1, 2, 3} # => True + # Check for existence in a set with in 2 in filled_set # => True 10 in filled_set # => False @@ -448,19 +469,19 @@ def pass_all_the_args(*args, **kwargs): # Function Scope x = 5 -def setX(num): +def set_x(num): # Local var x not the same as global variable x x = num # => 43 print x # => 43 -def setGlobalX(num): +def set_global_x(num): global x print x # => 5 x = num # global var x is now set to 6 print x # => 6 -setX(43) -setGlobalX(6) +set_x(43) +set_global_x(6) # Python has first class functions def create_adder(x): @@ -504,6 +525,10 @@ class Human(object): # Assign the argument to the instance's name attribute self.name = name + # Initialize property + self.age = 0 + + # An instance method. All methods take "self" as the first argument def say(self, msg): return "{0}: {1}".format(self.name, msg) @@ -519,6 +544,23 @@ class Human(object): def grunt(): return "*grunt*" + # A property is just like a getter. + # It turns the method age() into an read-only attribute + # of the same name. + @property + def age(self): + return self._age + + # This allows the property to be set + @age.setter + def age(self, age): + self._age = age + + # This allows the property to be deleted + @age.deleter + def age(self): + del self._age + # Instantiate a class i = Human(name="Ian") @@ -538,6 +580,16 @@ j.get_species() # => "H. neanderthalensis" # Call the static method Human.grunt() # => "*grunt*" +# Update the property +i.age = 42 + +# Get the property +i.age # => 42 + +# Delete the property +del i.age +i.age # => raises an AttributeError + #################################################### ## 6. Modules -- cgit v1.2.3 From a9bcaffd36dc3d3bfa6ec50f1861c9d006631d0b Mon Sep 17 00:00:00 2001 From: viv1 Date: Fri, 16 Oct 2015 12:57:31 +0530 Subject: [python/en]...Added some info on division --- 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 b939ebbe..3d8f0085 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -58,6 +58,12 @@ allow you to write Python 3 code that will run on Python 2, so check out the Pyt -5 // 3 # => -2 -5.0 // 3.0 # => -2.0 +# Note that we can also import division module(Section 6 Modules) +# to carry out normal division with just one '/'. +from __future__ import division +11/4 # => 2.75 ...normal division +11//4 # => 2 ...floored division + # Modulo operation 7 % 3 # => 1 -- cgit v1.2.3 From 451ccaec5b489f0144cf3d537c0debb73ebd3c82 Mon Sep 17 00:00:00 2001 From: Ankit Aggarwal Date: Fri, 16 Oct 2015 20:42:03 +0530 Subject: Adding documentation on ternary operator --- 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 b939ebbe..daaeb70c 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -165,6 +165,7 @@ some_var # => 5 some_other_var # Raises a name error # if can be used as an expression +# Equivalent of C's '?:' ternary operator "yahoo!" if 3 > 2 else 2 # => "yahoo!" # Lists store sequences -- cgit v1.2.3 From c60ddc6ef5d80cc87b051b848ccce0b7f335f5f1 Mon Sep 17 00:00:00 2001 From: MDS Date: Tue, 20 Oct 2015 01:01:14 +1300 Subject: Updated official python docs to always use the latest version of python 2 --- 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 675967f4..42a52bcf 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -707,7 +707,7 @@ print say(say_please=True) # Can you buy me a beer? Please! I am poor :( * [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/) +* [The Official Docs](http://docs.python.org/2/) * [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) -- cgit v1.2.3 From a6eb459b611b67132c76e34095afd87a5aada78c Mon Sep 17 00:00:00 2001 From: Vipul Sharma Date: Tue, 20 Oct 2015 09:57:53 +0530 Subject: Modified string format [python/en] --- python.html.markdown | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 42a52bcf..01e5d481 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -123,8 +123,11 @@ not False # => True # A string can be treated like a list of characters "This is a string"[0] # => 'T' -# % can be used to format strings, like this: -"%s can be %s" % ("strings", "interpolated") +#String formatting with % +#Even though the % string operator will be deprecated on Python 3.1 and removed later at some time, it may still be #good to know how it works. +x = 'apple' +y = 'lemon' +z = "The items in the basket are %s and %s" % (x,y) # A newer way to format strings is the format method. # This method is the preferred way -- cgit v1.2.3 From b238a58c9768d3b7694c4f02cbfe5ba2cac0015a Mon Sep 17 00:00:00 2001 From: payet-s Date: Wed, 21 Oct 2015 17:48:04 +0200 Subject: [python/en] Fix typos --- 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 42a52bcf..753d6e8c 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -15,8 +15,8 @@ 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. Python 2.7 is reachong end of life and will stop beeign maintained in 2020, -it is though recommended to start learnign Python with Python 3. +to Python 2.x. Python 2.7 is reaching end of life and will stop being maintained in 2020, +it is though recommended to start learning Python with Python 3. For Python 3.x, take a look at the [Python 3 tutorial](http://learnxinyminutes.com/docs/python3/). It is also possible to write Python code which is compatible with Python 2.7 and 3.x at the same time, -- cgit v1.2.3 From db903ac5b6c80fa4b0e8502fb4b3abfd1bed07ee Mon Sep 17 00:00:00 2001 From: Srinivasan R Date: Wed, 28 Oct 2015 16:25:54 +0530 Subject: Add one more string formatting example --- 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 753d6e8c..3d63183c 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -128,6 +128,7 @@ not False # => True # A newer way to format strings is the format method. # This method is the preferred way +"{} is a {}".format("This", "placeholder") "{0} can be {1}".format("strings", "formatted") # You can use keywords if you don't want to count. "{name} wants to eat {food}".format(name="Bob", food="lasagna") -- cgit v1.2.3 From cdd64ecee34af20ed101ba5dc8d7dc73a8189c15 Mon Sep 17 00:00:00 2001 From: Vipul Sharma Date: Thu, 29 Oct 2015 15:23:37 +0530 Subject: 80 char and proper commenting --- python.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 01e5d481..ff4471e9 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -124,7 +124,8 @@ not False # => True "This is a string"[0] # => 'T' #String formatting with % -#Even though the % string operator will be deprecated on Python 3.1 and removed later at some time, it may still be #good to know how it works. +#Even though the % string operator will be deprecated on Python 3.1 and removed +#later at some time, it may still be good to know how it works. x = 'apple' y = 'lemon' z = "The items in the basket are %s and %s" % (x,y) -- cgit v1.2.3 From 14c85ba0ffbb66d9c2a056006cedaa90df8f22f4 Mon Sep 17 00:00:00 2001 From: yihong Date: Fri, 30 Oct 2015 05:04:41 +0800 Subject: add more details on truthiness --- 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 42a52bcf..7055689e 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -63,7 +63,7 @@ allow you to write Python 3 code that will run on Python 2, so check out the Pyt # to carry out normal division with just one '/'. from __future__ import division 11/4 # => 2.75 ...normal division -11//4 # => 2 ...floored division +11//4 # => 2 ...floored division # Modulo operation 7 % 3 # => 1 @@ -144,8 +144,16 @@ None is None # => True # very useful when dealing with primitive values, but is # very useful when dealing with objects. -# None, 0, and empty strings/lists all evaluate to False. -# All other values are True +# Any object can be used in a Boolean context. +# The following values are considered falsey: +# - None +# - zero of any numeric type (e.g., 0, 0L, 0.0, 0j) +# - empty sequences (e.g., '', (), []) +# - empty containers (e.g., {}, set()) +# - instances of user-defined classes meeting certain conditions +# see: https://docs.python.org/2/reference/datamodel.html#object.__nonzero__ +# +# All other values are truthy (using the bool() function on them returns True). bool(0) # => False bool("") # => False -- cgit v1.2.3 From 5bda926dcc79dea4e936cc752fd1ff00e29d71bb Mon Sep 17 00:00:00 2001 From: Elijah Karari Date: Sat, 31 Oct 2015 09:47:55 +0300 Subject: Minor correction calling li.index(2) with li as [1, 2, 3, 4, 5, 6] will return 1 not 3 --- 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 541bd36d..80cef828 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -238,7 +238,7 @@ li.remove(2) # Raises a ValueError as 2 is not in the list li.insert(1, 2) # li is now [1, 2, 3, 4, 5, 6] again # Get the index of the first item found -li.index(2) # => 3 +li.index(2) # => 1 li.index(7) # Raises a ValueError as 7 is not in the list # Check for existence in a list with "in" -- cgit v1.2.3 From ab1acb1bb265577ea4f189d203ac35726f02970a Mon Sep 17 00:00:00 2001 From: Elijah Karari Date: Sat, 31 Oct 2015 09:56:06 +0300 Subject: Updated tuple examples for clarity --- python.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 541bd36d..b0add668 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -261,8 +261,9 @@ tup[:2] # => (1, 2) # 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 +d, e, f = 4, 5, 6 # you can leave out the parentheses # Tuples are created by default if you leave out the parentheses -d, e, f = 4, 5, 6 +g = 4, 5, 6 # => (4, 5, 6) # Now look how easy it is to swap two values e, d = d, e # d is now 5 and e is now 4 -- cgit v1.2.3 From a4a7b2dd8308a8d67722d8c93e4c1a8c052f7f6a Mon Sep 17 00:00:00 2001 From: EL Date: Mon, 2 Nov 2015 16:30:15 +0300 Subject: fixed unintended opposite meaning --- 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 2b43c5fc..f8f712d3 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -458,7 +458,7 @@ add(y=6, x=5) # Keyword arguments can arrive in any order. # You can define functions that take a variable number of -# positional args, which will be interpreted as a tuple if you do not use the * +# positional args, which will be interpreted as a tuple by using * def varargs(*args): return args @@ -466,7 +466,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 dict if you do not use ** +# keyword args, as well, which will be interpreted as a dict by using ** def keyword_args(**kwargs): return kwargs -- cgit v1.2.3