From 3386171160dd7fc3e4e6e9aaf2d5abfebdb38d6f Mon Sep 17 00:00:00 2001 From: Aswin Sanakan Date: Tue, 5 Dec 2017 12:24:43 +0530 Subject: Fix omitting in list and clarified comments --- python3.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index 37987582..153384de 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -209,9 +209,9 @@ li[4] # Raises an IndexError # The start index is included, the end index is not # (It's a closed/open range for you mathy types.) li[1:3] # => [2, 4] -# Omit the end +# Omit the beginning and return the list li[2:] # => [4, 3] -# Omit the beginning +# Omit the end and return the list li[:3] # => [1, 2, 4] # Select every second entry li[::2] # =>[1, 4] -- cgit v1.2.3 From 213019c689323af7f5c531fb156c34abe8771960 Mon Sep 17 00:00:00 2001 From: Rommel Martinez Date: Sat, 3 Feb 2018 18:08:24 +0800 Subject: [python3/en]: fix typo --- python3.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index 37987582..864228e4 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -6,6 +6,7 @@ contributors: - ["Andre Polykanine", "https://github.com/Oire"] - ["Zachary Ferguson", "http://github.com/zfergus2"] - ["evuez", "http://github.com/evuez"] + - ["Rommel Martinez", "https://ebzzry.io"] filename: learnpython3.py --- @@ -546,9 +547,9 @@ 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 all_the_args(1, 2, 3, 4) +all_the_args(**kwargs) # equivalent to all_the_args(a=3, b=4) +all_the_args(*args, **kwargs) # equivalent to all_the_args(1, 2, 3, 4, a=3, b=4) # Returning multiple values (with tuple assignments) def swap(x, y): -- cgit v1.2.3 From 50f93a94d4337b5a4f268d0832bf0fea55f55c37 Mon Sep 17 00:00:00 2001 From: qzhangjhu Date: Wed, 28 Feb 2018 10:08:29 -0500 Subject: Corrections on Python3 page --- python3.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index b0f04a02..019934cb 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -790,11 +790,11 @@ class Superhero(Human): # This calls the parent class constructor: super().__init__(name) - # overload the sing method + # override the sing method def sing(self): return 'Dun, dun, DUN!' - # add an additional class method + # add an additional instance method def boast(self): for power in self.superpowers: print("I wield the power of {pow}!".format(pow=power)) @@ -817,7 +817,7 @@ if __name__ == '__main__': # Calls parent method but uses its own class attribute print(sup.get_species()) # => Superhuman - # Calls overloaded method + # Calls overridden method print(sup.sing()) # => Dun, dun, DUN! # Calls method from Human @@ -872,7 +872,7 @@ class Batman(Superhero, Bat): def __init__(self, *args, **kwargs): # Typically to inherit attributes you have to call super: - #super(Batman, self).__init__(*args, **kwargs) + # super(Batman, self).__init__(*args, **kwargs) # However we are dealing with multiple inheritance here, and super() # only works with the next base class in the MRO list. # So instead we explicitly call __init__ for all ancestors. @@ -901,7 +901,7 @@ if __name__ == '__main__': # Calls parent method but uses its own class attribute print(sup.get_species()) # => Superhuman - # Calls overloaded method + # Calls overridden method print(sup.sing()) # => nan nan nan nan nan batman! # Calls method from Human, because inheritance order matters -- cgit v1.2.3 From b6aed7a0b38bf189922dbc47ed802de3c9769b73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=90=86=E7=B5=B5=E5=AD=90?= <41843094+lowlevelware@users.noreply.github.com> Date: Sun, 29 Jul 2018 11:53:33 +0900 Subject: added example for f-string Literal string interpolation is new in python 3.6 [pep-0498](https://www.python.org/dev/peps/pep-0498/) --- python3.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index 019934cb..d6cfbf59 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -138,6 +138,10 @@ len("This is a string") # => 16 # still use the old style of formatting: "%s can be %s the %s way" % ("Strings", "interpolated", "old") # => "Strings can be interpolated the old way" +# You can also format using f-strings or formatted string literals +name = "Reiko" +f"She said her name is {name}." # => "She said her name is Reiko" + # None is an object None # => None -- cgit v1.2.3 From 04de3a348ab825f3b34eb38d07dcb3cdfc6feb7a Mon Sep 17 00:00:00 2001 From: Roberto Fernandez Diaz Date: Tue, 7 Aug 2018 11:01:04 +0200 Subject: Clarification about sets I indicate that a set doesn't contain duplicate elements and add a little example --- python3.html.markdown | 3 +++ 1 file changed, 3 insertions(+) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index d6cfbf59..b378a8c6 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -7,6 +7,7 @@ contributors: - ["Zachary Ferguson", "http://github.com/zfergus2"] - ["evuez", "http://github.com/evuez"] - ["Rommel Martinez", "https://ebzzry.io"] + - ["Roberto Fernandez Diaz", "https://github.com/robertofd1995"] filename: learnpython3.py --- @@ -352,6 +353,8 @@ valid_set = {(1,), 1} # Add one more item to the set filled_set = some_set filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5} +# Sets do not have duplicate elements +filled_set.add(5) # it remains as before {1, 2, 3, 4, 5} # Do set intersection with & other_set = {3, 4, 5, 6} -- cgit v1.2.3 From 56258dfb72c17192d1963d56da6aaf12624b3e43 Mon Sep 17 00:00:00 2001 From: Joseph G Date: Thu, 11 Oct 2018 14:10:16 -0700 Subject: Add f-string clarification. --- python3.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index b378a8c6..7683bc60 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -139,9 +139,11 @@ len("This is a string") # => 16 # still use the old style of formatting: "%s can be %s the %s way" % ("Strings", "interpolated", "old") # => "Strings can be interpolated the old way" -# You can also format using f-strings or formatted string literals +# You can also format using f-strings or formatted string literals (in Python 3.6+) name = "Reiko" f"She said her name is {name}." # => "She said her name is Reiko" +# You can basically put any Python statement inside the braces and it will be output in the string. +f"{name} is {len(name)} characters long." # None is an object -- cgit v1.2.3 From 30cdf4b25327e01c751db384e42692fd95178db1 Mon Sep 17 00:00:00 2001 From: Divay Prakash Date: Fri, 26 Oct 2018 02:35:15 +0530 Subject: Fix tuple unpacking example in python3, closes #3130 (#3328) --- python3.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index 7683bc60..c7fbf342 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -281,7 +281,8 @@ a, b, c = (1, 2, 3) # a is now 1, b is now 2 and c is now 3 # You can also do extended unpacking a, *b, c = (1, 2, 3, 4) # a is now 1, b is now [2, 3] and c is now 4 # Tuples are created by default if you leave out the parentheses -d, e, f = 4, 5, 6 +d, e, f = 4, 5, 6 # tuple 4, 5, 6 is unpacked into variables d, e and f +# respectively such that d = 4, e = 5 and f = 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 4a891817ee54fb66cdf793990350f2370c70728d Mon Sep 17 00:00:00 2001 From: Niels Bom Date: Fri, 30 Nov 2018 11:52:37 +0100 Subject: [python3/en] show chaining operators nicer --- python3.html.markdown | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index c7fbf342..ff527716 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -96,7 +96,10 @@ False or True # => True 2 <= 2 # => True 2 >= 2 # => True -# Comparisons can be chained! +# Seeing whether a value is in a range +1 < 2 and 2 < 3 # => True +2 < 3 and 3 < 2 # => False +# Chaining makes this look nicer 1 < 2 < 3 # => True 2 < 3 < 2 # => False -- cgit v1.2.3 From 609abd33284284a487f1ac3bd0a6898ed6a77267 Mon Sep 17 00:00:00 2001 From: Niels Bom Date: Sat, 1 Dec 2018 05:29:58 +0100 Subject: [python3/en] show that True and False are ints (#3412) * [python3/en] show that True and False are ints * [python3/en] rework some boolean stuff I removed the example `-5 != False != True #=> True` because we didn't cover chaining yet. --- python3.html.markdown | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index c7fbf342..b3f89372 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -72,15 +72,24 @@ not False # => True True and False # => False False or True # => True -# Note using Bool operators with ints -# False is 0 and True is 1 +# True and False are actually 1 and 0 but with different keywords +True + True # => 2 +True * 8 # => 8 +False - 5 # => -5 + +# Comparison operators look at the numerical value of True and False +0 == False # => True +1 == True # => True +2 == True # => False +-5 != False # => True + +# Using boolean logical operators on ints casts them to booleans for evaluation, but their non-cast value is returned # Don't mix up with bool(ints) and bitwise and/or (&,|) +bool(0) # => False +bool(4) # => True +bool(-6) # => True 0 and 2 # => 0 -5 or 0 # => -5 -0 == False # => True -2 == True # => False -1 == True # => True --5 != False != True #=> True # Equality is == 1 == 1 # => True -- cgit v1.2.3 From 7bdc3d8f2f256c4ab704226e392948cad2a0ff13 Mon Sep 17 00:00:00 2001 From: Roman Garanin Date: Wed, 19 Dec 2018 22:09:29 +0300 Subject: [python3/en] A note about common gotcha with mutable defaults A reminder to be aware of using mutable defaults. Whether it is a real problem or not depends on exact use-case, but an update fully describing that and updating code with copying list, or defaulting with None and conditional initialization, would probably negatively affect readability of the example. --- python3.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index 4d5bb3ae..795d9e99 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -805,6 +805,7 @@ class Superhero(Human): # add additional class attributes: self.fictional = True self.movie = movie + # be aware of mutable default values, since defaults are shared self.superpowers = superpowers # The "super" function lets you access the parent class's methods -- cgit v1.2.3 From 62926bd431868a0c8de3912b29d30dc11c196aee Mon Sep 17 00:00:00 2001 From: Nico Dinata Date: Fri, 21 Dec 2018 05:42:35 +1100 Subject: [python3/en] Add note about dictionary item ordering in Python 3.7+ (#3423) * Add changes to dict item ordering in Python 3.7+ * Fix line length * Fix typo in example --- python3.html.markdown | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index 4d5bb3ae..25f88943 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -314,16 +314,19 @@ valid_dict = {(1,2,3):[1,2,3]} # Values can be of any type, however. filled_dict["one"] # => 1 # Get all keys as an iterable with "keys()". We need to wrap the call in list() -# to turn it into a list. We'll talk about those later. Note - Dictionary key -# ordering is not guaranteed. Your results might not match this exactly. -list(filled_dict.keys()) # => ["three", "two", "one"] +# to turn it into a list. We'll talk about those later. Note - for Python +# versions <3.7, dictionary key ordering is not guaranteed. Your results might +# not match the example below exactly. However, as of Python 3.7, dictionary +# items maintain the order at which they are inserted into the dictionary. +list(filled_dict.keys()) # => ["three", "two", "one"] in Python <3.7 +list(filled_dict.keys()) # => ["one", "two", "three"] in Python 3.7+ # Get all values as an iterable with "values()". Once again we need to wrap it # in list() to get it out of the iterable. Note - Same as above regarding key # ordering. -list(filled_dict.values()) # => [3, 2, 1] - +list(filled_dict.values()) # => [3, 2, 1] in Python <3.7 +list(filled_dict.values()) # => [1, 2, 3] in Python 3.7+ # Check for existence of keys in a dictionary with "in" "one" in filled_dict # => True -- cgit v1.2.3 From 08bd255df5050c946b8e3f0467035d06dd414c9b Mon Sep 17 00:00:00 2001 From: Gaven Finch <35317008+twigleg2@users.noreply.github.com> Date: Tue, 5 Feb 2019 12:43:07 -0700 Subject: Update python3.html.markdown --- python3.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index 1b8fa88e..c4f15867 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -41,10 +41,10 @@ Note: This article applies to Python 3 specifically. Check out [here](http://lea 10 * 2 # => 20 35 / 5 # => 7.0 -# Result of integer division truncated down both for positive and negative. +# Integer division rounds down for both positive and negative numbers. 5 // 3 # => 1 -5.0 // 3.0 # => 1.0 # works on floats too -5 // 3 # => -2 +5.0 // 3.0 # => 1.0 # works on floats too -5.0 // 3.0 # => -2.0 # The result of division is always a float -- cgit v1.2.3 From a9a01e4fff78ba325738dd2d344970a153277a0d Mon Sep 17 00:00:00 2001 From: Ryota Kayanuma Date: Mon, 15 Jul 2019 17:26:57 +0900 Subject: Add output for python3 f-string. --- python3.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index c4f15867..ecdbd932 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -155,7 +155,7 @@ len("This is a string") # => 16 name = "Reiko" f"She said her name is {name}." # => "She said her name is Reiko" # You can basically put any Python statement inside the braces and it will be output in the string. -f"{name} is {len(name)} characters long." +f"{name} is {len(name)} characters long." # => "Reiko is 5 characters long." # None is an object -- cgit v1.2.3 From 0b5245e2d83453f23e13195ea164a35603c4855a Mon Sep 17 00:00:00 2001 From: Ben Landry Date: Tue, 13 Aug 2019 06:35:13 -0400 Subject: [python3/en] Added enumerate function (#3601) * Added enumerate function * adjusted spacing --- python3.html.markdown | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index ecdbd932..ef78ce37 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -462,8 +462,19 @@ prints: """ for i in range(4, 8, 2): print(i) + +""" +To loop over a list, and retrieve both the index and the value of each item in the list +prints: + 0 dog + 1 cat + 2 mouse """ +list = ["dog", "cat", "mouse"] +for i, value in enumerate(list): + print(i, value) +""" While loops go until a condition is no longer met. prints: 0 -- cgit v1.2.3 From 77ab89a4b3ead12172230781e61bed629e12f7b5 Mon Sep 17 00:00:00 2001 From: Ben Landry Date: Wed, 21 Aug 2019 21:14:03 -0400 Subject: Added write to and read from a file syntax --- python3.html.markdown | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index ef78ce37..4cabb27b 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -505,6 +505,26 @@ with open("myfile.txt") as f: for line in f: print(line) +# Writing to a file +contents = {"aa": 12, "bb": 21} +with open("myfile1.txt", "w+") as file: + file.write(str(contents)) # writes a string to a file + +with open("myfile2.txt", "w+") as file: + file.write(json.dumps(contents)) # writes an object to a file + +# Reading from a file +with open('myfile1.txt', "r+") as file: + contents = file.read() # reads a string from a file +print(contents) +# print: {"aa": 12, "bb": 21} + +with open('myfile2.txt', "r+") as file: + contents = json.load(file) # reads a json object from a file +print(contents) +# print: {"aa": 12, "bb": 21} + + # Python offers a fundamental abstraction called the Iterable. # An iterable is an object that can be treated as a sequence. # The object returned by the range function, is an iterable. -- cgit v1.2.3 From 80096795e04d66bde02535775f66224f98a7a17f Mon Sep 17 00:00:00 2001 From: Divay Prakash Date: Tue, 24 Sep 2019 10:17:02 +0530 Subject: Fix explanation for slices --- python3.html.markdown | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index 4cabb27b..430927a9 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -228,15 +228,11 @@ li[4] # Raises an IndexError # You can look at ranges with slice syntax. # The start index is included, the end index is not # (It's a closed/open range for you mathy types.) -li[1:3] # => [2, 4] -# Omit the beginning and return the list -li[2:] # => [4, 3] -# Omit the end and return the list -li[:3] # => [1, 2, 4] -# Select every second entry -li[::2] # =>[1, 4] -# Return a reversed copy of the list -li[::-1] # => [3, 4, 2, 1] +li[1:3] # Return list from index 1 to 3 => [2, 4] +li[2:] # Return list starting from index 2 => [4, 3] +li[:3] # Return list from beginning uptil index 3 => [1, 2, 4] +li[::2] # Return list selecting every second entry => [1, 4] +li[::-1] # Return list in reverse order => [3, 4, 2, 1] # Use any combination of these to make advanced slices # li[start:end:step] -- cgit v1.2.3 From 2486fa8c1e51e975c603fa7972542deae287817b Mon Sep 17 00:00:00 2001 From: Mariusz Skoneczko Date: Tue, 22 Oct 2019 12:08:08 +1100 Subject: [python3/en] Clarify difference between iterators and iterables in the last example (closes #3586) --- python3.html.markdown | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index 430927a9..61c53408 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -550,8 +550,14 @@ next(our_iterator) # => "three" # After the iterator has returned all of its data, it raises a StopIteration exception next(our_iterator) # Raises StopIteration -# You can grab all the elements of an iterator by calling list() on it. -list(filled_dict.keys()) # => Returns ["one", "two", "three"] +# We can also loop over it, in fact, "for" does this implicitly! +our_iterator = iter(our_iterable) +for i in our_iterator: + print(i) # Prints one, two, three + +# You can grab all the elements of an iterable or iterator by calling list() on it. +list(our_iterable) # => Returns ["one", "two", "three"] +list(our_iterator) # => Returns [] because state is saved #################################################### -- cgit v1.2.3 From 49a7c2dd3b38df21e5844b2d9b03067d2c910acf Mon Sep 17 00:00:00 2001 From: caminsha Date: Thu, 14 Nov 2019 00:11:02 +0100 Subject: Fixed typo: uptil => until --- python3.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'python3.html.markdown') diff --git a/python3.html.markdown b/python3.html.markdown index 61c53408..d09c2819 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -8,6 +8,7 @@ contributors: - ["evuez", "http://github.com/evuez"] - ["Rommel Martinez", "https://ebzzry.io"] - ["Roberto Fernandez Diaz", "https://github.com/robertofd1995"] + - ["caminsha", "https://github.com/caminsha"] filename: learnpython3.py --- @@ -230,7 +231,7 @@ li[4] # Raises an IndexError # (It's a closed/open range for you mathy types.) li[1:3] # Return list from index 1 to 3 => [2, 4] li[2:] # Return list starting from index 2 => [4, 3] -li[:3] # Return list from beginning uptil index 3 => [1, 2, 4] +li[:3] # Return list from beginning until index 3 => [1, 2, 4] li[::2] # Return list selecting every second entry => [1, 4] li[::-1] # Return list in reverse order => [3, 4, 2, 1] # Use any combination of these to make advanced slices -- cgit v1.2.3