From 97831ea607d3a405b393d6f1a5c171a629fea6fb Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Tue, 20 Oct 2015 08:30:19 +1100 Subject: [PATCH 01/11] Create breaking-long-lines.rst --- breaking-long-lines.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 breaking-long-lines.rst diff --git a/breaking-long-lines.rst b/breaking-long-lines.rst new file mode 100644 index 0000000..08fcd6a --- /dev/null +++ b/breaking-long-lines.rst @@ -0,0 +1,14 @@ +In some of the programs discussed in the book including the sample solutions, you will see statements like: + +.. code:: + + print('Area: {0}, Estimated ({1}): {2}'. + format(area_of_circle, points, estimate(radius, points))) + + +This is an example of breaking a long line into two (or more) lines so that we don't end up with really long lines in our code. How long should a line be when you should think about breaking it? If your statement's length is more than 80 characters, you should think about breaking it up. In the book, we often had to do so because of layout reasons, and in your projects you will want to do it so that your statements are easier to read and on the average all lines have a similar length. + +How do you break? +================= + +Above, we saw one example of breaking it, which is an implicit Python mechanism From bdaf3304be11401c0cf6fb50199f1c42fd939a44 Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Wed, 4 Nov 2015 10:30:24 +1100 Subject: [PATCH 02/11] Update breaking-long-lines.rst --- breaking-long-lines.rst | 58 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/breaking-long-lines.rst b/breaking-long-lines.rst index 08fcd6a..bed1b99 100644 --- a/breaking-long-lines.rst +++ b/breaking-long-lines.rst @@ -6,9 +6,63 @@ In some of the programs discussed in the book including the sample solutions, yo format(area_of_circle, points, estimate(radius, points))) -This is an example of breaking a long line into two (or more) lines so that we don't end up with really long lines in our code. How long should a line be when you should think about breaking it? If your statement's length is more than 80 characters, you should think about breaking it up. In the book, we often had to do so because of layout reasons, and in your projects you will want to do it so that your statements are easier to read and on the average all lines have a similar length. +This is really the following single statement: + +.. code:: + + print('Area: {0}, Estimated ({1}): {2}'.format(area_of_circle, points, estimate(radius, points))) + +The first code snippet above is an example of breaking a long line into two (or more) lines so that we don't end up with really long lines in our code. How long should a line be when you should think about breaking it? If your statement's length is more than 80 characters, you should think about breaking it up. + +In the book, we often had to do so because of layout reasons even though the statement may not have exceeded 80 characters, and in your projects you will want to do it so that your statements are easier to read and on the average all lines have a similar length. This is formalized (among other things) in `PEP 8 Date: Wed, 4 Nov 2015 10:31:33 +1100 Subject: [PATCH 03/11] Update breaking-long-lines.rst --- breaking-long-lines.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/breaking-long-lines.rst b/breaking-long-lines.rst index bed1b99..d1cbef6 100644 --- a/breaking-long-lines.rst +++ b/breaking-long-lines.rst @@ -61,7 +61,10 @@ You can even do this: + x**6/6 + x**7/7 + x**8/8) -** Use the implicit continuation operator ** +**Use the implicit continuation operator** + +Dealing with long strings +~~~~~~~~~~~~~~~~~~~~~~~~~ From 740388dd31892e8eaa6642ea3172079d9d8a1d3c Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Wed, 4 Nov 2015 11:03:23 +1100 Subject: [PATCH 04/11] Update breaking-long-lines.rst --- breaking-long-lines.rst | 58 +++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/breaking-long-lines.rst b/breaking-long-lines.rst index d1cbef6..07f3d5f 100644 --- a/breaking-long-lines.rst +++ b/breaking-long-lines.rst @@ -21,38 +21,34 @@ Note that the examples below will for illustrative purposes break lines waaaaay How do you break? ================= -When calling functions -~~~~~~~~~~~~~~~~~~~~~~ - -By default, when calling functions you can just press enter and without doing anything more keep writing your statement over multiple lines. For example: - -.. code:: - - x = 1 - print(x, - x) - When not calling function ~~~~~~~~~~~~~~~~~~~~~~~~~ When you are not calling a function, you essentially have two choices: -**Use parantheses** +**Use paranthesis** -This is exactly how we break the long statement in the example we started this article with. Here is another example: +This is exactly how we break the long statement in the example we started this article with. For the moment ignore the call to ``print()`` and assume that the statement is: .. code:: - s1 = x + x**2/2 + x**3/3 + x**4/4 + x**5/5 + x**6/6 + x**7/7 + x**8/8 + s = 'Area: {0}, Estimated ({1}): {2}'.format(area_of_circle, points, estimate(radius, points)) + +This essentially just creates the string ``s``. If we were to split this statement over multiple lines, we would do the following: + +.. code:: + s = ('Area: {0}, Estimated ({1}): {2}' + .format(area_of_circle, points, estimate(radius, points))) -Let's say we want to write the above statement over multiple lines. Here is how we can use parentheses to do so: +Note the extra beginning and the ending parenthesis. + +Here is another example: .. code:: - s2 = (x + x**2/2 + x**3/3 + x**4/4 + x**5/5 - + x**6/6 + x**7/7 + x**8/8) - -You can even do this: + s1 = x + x**2/2 + x**3/3 + x**4/4 + x**5/5 + x**6/6 + x**7/7 + x**8/8 + +Here is how we can use split the above statment into multiple lines using parentheses: .. code:: @@ -63,6 +59,30 @@ You can even do this: **Use the implicit continuation operator** +The implicit continuation operator, ``\`` can be used to split long statements over multiple lines. Here is how we could split the above statement using ``\`` instead: + +.. code:: + + s3 = x + x**2/2 + x**3/3 \ + + x**4/4 + x**5/5 \ + + x**6/6 + x**7/7 \ + + x**8/8 + + +At the end of every line (except the last), we just add a ``\`` indicating that the next line is also a part of the same statement. + +When calling functions +~~~~~~~~~~~~~~~~~~~~~~ + +By default, when calling functions you can just press enter and without doing anything more keep writing your statement over multiple lines. For example: + +.. code:: + + x = 1 + print(x, + x) + + Dealing with long strings ~~~~~~~~~~~~~~~~~~~~~~~~~ From 5b1e3dd4109ea60d3f8bb6fd392651bf8546bed7 Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Wed, 4 Nov 2015 11:11:31 +1100 Subject: [PATCH 05/11] Update breaking-long-lines.rst --- breaking-long-lines.rst | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/breaking-long-lines.rst b/breaking-long-lines.rst index 07f3d5f..5dd6687 100644 --- a/breaking-long-lines.rst +++ b/breaking-long-lines.rst @@ -82,10 +82,13 @@ By default, when calling functions you can just press enter and without doing an print(x, x) + +Hence, we `could` have broken the first example we saw as: -Dealing with long strings -~~~~~~~~~~~~~~~~~~~~~~~~~ - - +.. code:: + print('Area: {0}, Estimated ({1}): {2}'.format(area_of_circle, + points, + estimate(radius, points))) +When calling ``format()`` we put the arguments over separate lines. From 0432dadfae334c19ff9442238ea782ba634b265a Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Wed, 4 Nov 2015 11:13:22 +1100 Subject: [PATCH 06/11] Update breaking-long-lines.rst --- breaking-long-lines.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/breaking-long-lines.rst b/breaking-long-lines.rst index 5dd6687..056b27d 100644 --- a/breaking-long-lines.rst +++ b/breaking-long-lines.rst @@ -1,3 +1,12 @@ +Breaking long lines in Python +============================= + +:date: 2015-11-04 11:00 +:category: tips +:slug: breaking-long-lines-in-python +:summary: Breaking long lines in Python + + In some of the programs discussed in the book including the sample solutions, you will see statements like: .. code:: From eb274ee6f28ef5ece678ad957784996024e04f41 Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Wed, 4 Nov 2015 11:13:51 +1100 Subject: [PATCH 07/11] Update breaking-long-lines.rst --- breaking-long-lines.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/breaking-long-lines.rst b/breaking-long-lines.rst index 056b27d..5970635 100644 --- a/breaking-long-lines.rst +++ b/breaking-long-lines.rst @@ -2,7 +2,7 @@ Breaking long lines in Python ============================= :date: 2015-11-04 11:00 -:category: tips +:category: articles :slug: breaking-long-lines-in-python :summary: Breaking long lines in Python From bae1bbf47cbe226f50863fe8361d19e598a56365 Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Wed, 4 Nov 2015 11:14:55 +1100 Subject: [PATCH 08/11] Update breaking-long-lines.rst --- breaking-long-lines.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/breaking-long-lines.rst b/breaking-long-lines.rst index 5970635..763f824 100644 --- a/breaking-long-lines.rst +++ b/breaking-long-lines.rst @@ -46,6 +46,7 @@ This is exactly how we break the long statement in the example we started this a This essentially just creates the string ``s``. If we were to split this statement over multiple lines, we would do the following: .. code:: + s = ('Area: {0}, Estimated ({1}): {2}' .format(area_of_circle, points, estimate(radius, points))) From 3122dc74185c49ee3395e8cbc4344c7c68f0648f Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Wed, 4 Nov 2015 11:27:23 +1100 Subject: [PATCH 09/11] Update breaking-long-lines.rst --- breaking-long-lines.rst | 52 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/breaking-long-lines.rst b/breaking-long-lines.rst index 763f824..fad9db9 100644 --- a/breaking-long-lines.rst +++ b/breaking-long-lines.rst @@ -23,7 +23,7 @@ This is really the following single statement: The first code snippet above is an example of breaking a long line into two (or more) lines so that we don't end up with really long lines in our code. How long should a line be when you should think about breaking it? If your statement's length is more than 80 characters, you should think about breaking it up. -In the book, we often had to do so because of layout reasons even though the statement may not have exceeded 80 characters, and in your projects you will want to do it so that your statements are easier to read and on the average all lines have a similar length. This is formalized (among other things) in `PEP 8 `__. You may even find instances where I have not followed a guideline when writing the programs in the book. If you find one, let me know. + + +Getting in touch +================ + +Stay updated or get in touch: + +- `Facebook page `__ +- `G+ Community `__ +- `Twitter `__ + +You can contact me directly via: + +- Twitter: `@mathwithpython `__ +- Email : doingmathwithpython@gmail.com + + + + From 7acc601245f53bd81569ca378bafe5a3ea231fd7 Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Wed, 4 Nov 2015 11:28:22 +1100 Subject: [PATCH 10/11] Update breaking-long-lines.rst --- breaking-long-lines.rst | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/breaking-long-lines.rst b/breaking-long-lines.rst index fad9db9..304af81 100644 --- a/breaking-long-lines.rst +++ b/breaking-long-lines.rst @@ -23,7 +23,7 @@ This is really the following single statement: The first code snippet above is an example of breaking a long line into two (or more) lines so that we don't end up with really long lines in our code. How long should a line be when you should think about breaking it? If your statement's length is more than 80 characters, you should think about breaking it up. -In the book, we often had to do so because of layout reasons even though the statement may not have exceeded 80 characters, and in your projects you will want to do it so that your statements are easier to read and on the average all lines have a similar length. This is formalized (among other things) in `PEP 8 `__. Note that the examples below will for illustrative purposes break lines waaaaay less than 80 characters. @@ -144,7 +144,3 @@ You can contact me directly via: - Twitter: `@mathwithpython `__ - Email : doingmathwithpython@gmail.com - - - - From a3f47f64fbb9567e0ab84908a3ae6192d67cb63e Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Wed, 4 Nov 2015 11:29:14 +1100 Subject: [PATCH 11/11] Update breaking-long-lines.rst --- breaking-long-lines.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/breaking-long-lines.rst b/breaking-long-lines.rst index 304af81..c68e371 100644 --- a/breaking-long-lines.rst +++ b/breaking-long-lines.rst @@ -81,7 +81,7 @@ The line continuation operator, ``\`` can be used to split long statements over At the end of every line (except the last), we just add a ``\`` indicating that the next line is also a part of the same statement. -**Breaking up those long ``if ..`` statements** +**Breaking up those long if statements** Often I have to break long ``if`` statements and is in fact one of the most common cases I face at work where I have to break the statement into multiple lines. Here is an example using both the approaches above: