Why is a double semicolon a SyntaxError in Python? - python

I know that semicolons are unnecessary in Python, but they can be used to cram multiple statements onto a single line, e.g.
>>> x = 42; y = 54
I always thought that a semicolon was equivalent to a line break. So I was a bit surprised to learn (h/t Ned Batchelder on Twitter) that a double semicolon is a SyntaxError:
>>> x = 42
>>> x = 42;
>>> x = 42;;
File "<stdin>", line 1
x = 42;;
^
SyntaxError: invalid syntax
I assumed the last program was equivalent to x = 42\n\n. I’d have thought the statement between the semicolons was treated as an empty line, a no-op. Apparently not.
Why is this an error?

From the Python grammar, we can see that ; is not defined as \n. The parser expects another statement after a ;, except if there's a newline after it:
Semicolon w/ statement Maybe a semicolon Newline
\/ \/ \/ \/
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
That's why x=42;; doesn't work; because there isn't a statement between the two semicolons, as "nothing" isn't a statement. If there was any complete statement between them, like a pass or even just a 0, the code would work.
x = 42;0; # Fine
x = 42;pass; # Fine
x = 42;; # Syntax error
if x == 42:; print("Yes") # Syntax error - "if x == 42:" isn't a complete statement

An empty statement still needs pass, even if you have a semicolon.
>>> x = 42;pass;
>>> x
42

Related

exec() for multi-line string

This works:
s = "for x in range(5):" \
"print(x)"
exec(s)
How to add if statement to that dynamic code, something like:
s = "for x in range(5):" \
"if x>2:" \
"print(x)"
exec(s)
Above gives error:
...
for x in range(5):if x > 2:print(x)
^
SyntaxError: invalid syntax
The problem is the lack of proper indentation of the code in the string. An easy way to get it right is to enclose the properly formatted code in a triple-quoted string.
s = """
for x in range(5):
if x>2:
print(x)
"""
exec(s)
note that \ is only python's way of allowing visually continuing the same declaration at a "new line". so in practice,
s = "for x in range(5):" \
"if x>2:" \
"print(x)"
is equal to
s = "for x in range(5):if x>2:print(x)"
You will need to add actual line breaks and tabs to make the syntax valid, as such:
s = "for x in range(5):\n\tif x>2:\n\t\tprint(x)"
note that you can make it more readable if you combine \ with the above, and swapping \t with spaces, resulting in:
s = "for x in range(5):" \
"\n if x>2:" \
"\n print(x)"
(Note that to confirm with PEP-8, you should use 4 spaces per identation)
s = "for x in range(5):\n\t" \
"if x > 2:\n\t\t" \
"print(x)"
exec(s)
This will solve your problem.
The reason for getting error is ignoring the python indentation rule, so you need to handle them with \t.

What is the meaning of tuple under function definition [duplicate]

Given:
e = 'a' + 'b' + 'c' + 'd'
How do I write the above in two lines?
e = 'a' + 'b' +
'c' + 'd'
What is the line? You can just have arguments on the next line without any problems:
a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5,
blahblah6, blahblah7)
Otherwise you can do something like this:
if (a == True and
b == False):
or with explicit line break:
if a == True and \
b == False:
Check the style guide for more information.
Using parentheses, your example can be written over multiple lines:
a = ('1' + '2' + '3' +
'4' + '5')
The same effect can be obtained using explicit line break:
a = '1' + '2' + '3' + \
'4' + '5'
Note that the style guide says that using the implicit continuation with parentheses is preferred, but in this particular case just adding parentheses around your expression is probably the wrong way to go.
From PEP 8 -- Style Guide for Python Code:
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
Backslashes may still be appropriate at times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable:
with open('/path/to/some/file/you/want/to/read') as file_1, \
open('/path/to/some/file/being/written', 'w') as file_2:
file_2.write(file_1.read())
Another such case is with assert statements.
Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it. Some examples:
class Rectangle(Blob):
def __init__(self, width, height,
color='black', emphasis=None, highlight=0):
if (width == 0 and height == 0 and
color == 'red' and emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
Blob.__init__(self, width, height,
color, emphasis, highlight)file_2.write(file_1.read())
PEP8 now recommends the opposite convention (for breaking at binary operations) used by mathematicians and their publishers to improve readability.
Donald Knuth's style of breaking before a binary operator aligns operators vertically, thus reducing the eye's workload when determining which items are added and subtracted.
From PEP8: Should a line break before or after a binary operator?:
Donald Knuth explains the traditional rule in his Computers and Typesetting series: "Although formulas within a paragraph always break after binary operations and relations, displayed formulas always break before binary operations"[3].
Following the tradition from mathematics usually results in more readable code:
# Yes: easy to match operators with operands
income = (gross_wages
+ taxable_interest
+ (dividends - qualified_dividends)
- ira_deduction
- student_loan_interest)
In Python code, it is permissible to break before or after a binary operator, as long as the convention is consistent locally. For new code Knuth's style is suggested.
[3]: Donald Knuth's The TeXBook, pages 195 and 196
The danger in using a backslash to end a line is that if whitespace is added after the backslash (which, of course, is very hard to see), the backslash is no longer doing what you thought it was.
See Python Idioms and Anti-Idioms (for Python 2 or Python 3) for more.
Put a \ at the end of your line or enclose the statement in parens ( .. ). From IBM:
b = ((i1 < 20) and
(i2 < 30) and
(i3 < 40))
or
b = (i1 < 20) and \
(i2 < 30) and \
(i3 < 40)
You can break lines in between parenthesises and braces. Additionally, you can append the backslash character \ to a line to explicitly break it:
x = (tuples_first_value,
second_value)
y = 1 + \
2
From the horse's mouth: Explicit line
joining
Two or more physical lines may be
joined into logical lines using
backslash characters (\), as follows:
when a physical line ends in a
backslash that is not part of a string
literal or comment, it is joined with
the following forming a single logical
line, deleting the backslash and the
following end-of-line character. For
example:
if 1900 < year < 2100 and 1 <= month <= 12 \
and 1 <= day <= 31 and 0 <= hour < 24 \
and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date
return 1
A line ending in a backslash cannot
carry a comment. A backslash does not
continue a comment. A backslash does
not continue a token except for string
literals (i.e., tokens other than
string literals cannot be split across
physical lines using a backslash). A
backslash is illegal elsewhere on a
line outside a string literal.
If you want to break your line because of a long literal string, you can break that string into pieces:
long_string = "a very long string"
print("a very long string")
will be replaced by
long_string = (
"a "
"very "
"long "
"string"
)
print(
"a "
"very "
"long "
"string"
)
Output for both print statements:
a very long string
Notice the parenthesis in the affectation.
Notice also that breaking literal strings into pieces allows to use the literal prefix only on parts of the string and mix the delimiters:
s = (
'''2+2='''
f"{2+2}"
)
One can also break the call of methods (obj.method()) in multiple lines.
Enclose the command in parenthesis "()" and span multiple lines:
> res = (some_object
.apply(args)
.filter()
.values)
For instance, I find it useful on chain calling Pandas/Holoviews objects methods.
It may not be the Pythonic way, but I generally use a list with the join function for writing a long string, like SQL queries:
query = " ".join([
'SELECT * FROM "TableName"',
'WHERE "SomeColumn1"=VALUE',
'ORDER BY "SomeColumn2"',
'LIMIT 5;'
])
Taken from The Hitchhiker's Guide to Python (Line Continuation):
When a logical line of code is longer than the accepted limit, you need to split it over multiple physical lines. The Python interpreter will join consecutive lines if the last character of the line is a backslash. This is helpful in some cases, but should usually be avoided because of its fragility: a white space added to the end of the line, after the backslash, will break the code and may have unexpected results.
A better solution is to use parentheses around your elements. Left with an unclosed parenthesis on an end-of-line the Python interpreter will join the next line until the parentheses are closed. The same behaviour holds for curly and square braces.
However, more often than not, having to split a long logical line is a sign that you are trying to do too many things at the same time, which may hinder readability.
Having that said, here's an example considering multiple imports (when exceeding line limits, defined on PEP-8), also applied to strings in general:
from app import (
app, abort, make_response, redirect, render_template, request, session
)

What is the difference between '\' and '\n' escape sequence in python

I am coming from C language
In book of Python it is given
among escape sequence
\ - New line in a multi-line string
\n - Line break
I am confused and unable to differentiate between the two.
You have completely misread the book.
\ is not an escape sequence, and is not used on its own in strings. It is used in multi-line code.
\n is a newline character in strings.
The book is confusing you by mixing two entirely different concepts.
\n is an escape sequence in a string literal. Like other \single-character and \xhh or \uhhhh escape sequences these work exactly like those in C; they define a character in the string that would otherwise be difficult to spell out when writing code.
\ at the end of a physical line of code extends the logical line. That is, Python will see text on the next line as part of the current line, making it one long line of code. This applies anywhere in Python code.
You can trivially see the difference when you print the results of strings that use either technique:
escape_sequence = "This is a line.\nThis is another line"
logical_line_extended = "This is a logical line. \
This is still the same logical line."
print(escape_sequence)
print(logical_line_extended)
This outputs
This is a line.
This is another line
This is a logical line. This is still the same logical line.
Note that the line breaks have swapped! The \n escape sequence in the string value caused the output to be broken across two lines (the terminal or console or whatever is displaying the printed data, knows how to interpret a newline character), while the newline in the logical_line_extended string literal definition is gone; it was never part of the string value being defined, it was a newline in the source code only.
Python lets you extend a line of code like this because Python defines how you delimit logical lines very differently from C. In C, you end statements with ;, and group blocks of lines with {...} curly braces. Newlines are not part of how C reads your code.
So, the following C code:
if (a) { foo = 'bar'; spam = 'ham'; }
is the same thing as
if (a) {
foo = 'bar';
spam = 'ham';
}
C knows where each statement starts and ends because the programmer has to use ; and {...} to delimit lines and blocks, the language doesn't care about indentation or newlines at all here. In Python however, you explicitly use newlines and indentation to define the same structure. So Python uses whitespace instead of {, } and ;.
This means you could end up with long lines of code to hold a complex expression:
# deliberately convoluted long expression to illustrate a point
expr = 18 ** (1 / 3) / (6 * (3 + sqrt(3) * I) ** (1 / 3)) + 12 ** (1 / 3) * (3 + sqrt(3) * I) ** (1 / 3) / 12
The point of \ is to allow you to break up such a long expression across multiple logical lines by extending the current line with \ at the end:
# deliberately convoluted long expression to illustrate a point
expr = 18 ** (1 / 3) / (6 * (3 + sqrt(3) * I) ** (1 / 3)) + \
12 ** (1 / 3) * (3 + sqrt(3) * I) ** (1 / 3) / 12
So the \ as the last character on a line, tells Python to ignore the newline that's there and continue treating the following line as part of the same logical line.
Python also extends the logical line when it has seen an opening (, [ or { brace, until the matching }, ] or ) brace is found to close the expression. This is the preferred method of extending lines. So the above expression could be broken up across multiple logical lines with:
expr = (18 ** (1 / 3) / (6 * (3 + sqrt(3) * I) ** (1 / 3)) +
12 ** (1 / 3) * (3 + sqrt(3) * I) ** (1 / 3) / 12)
You can do the same with strings:
long_string = (
"This is a longer string that does not contain any newline "
"*characters*, but is defined in the source code with "
"multiple strings across multiple logical lines."
)
This uses another C string literal trick Python borrowed: multiple consecutive string literals form one long string object once parsed and compiled.
See the Lexical analysis reference documentation:
2.1.5. Explicit line joining
Two or more physical lines may be joined into logical lines using backslash characters (\)[.]
[...]
2.1.6. Implicit line joining
Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes.
The same documentation lists all the permitted Python string escape sequences.
\ - New line in a multi-line string
It is used for splitting a string which has a vast number of characters into multi lines as it is inconvenient to write in a single line.
This is something that has effect in the code only.
\n - Line break
This one on the other hand is a typical line break statement for printing something in a new line. The same thing we use in C and C++ languages.
this is something that has effect in the output.
Here is the response to your question:
Purpose of \n is basically used to give a line break as you mention too.
Example:
print("Hello\n")
print("Hi")
The output of the above would be like:
Hello
Hi
Purpose of \ is basically used to escape characters which have special meaning
Example: I have to print Hello\ in the output then the code will be like
print("Hello\\")
The output of the above code will be like:
Hello\
So bascially in order to print Hello\ in your output, you have to put two "\\" and this is the purpose of \ character (to escape special characters).
I hope this helps.
With "\" you can change line as you write your code. What I mean is that if you write a long line of code and you want to change line to see what you type.
For example :
print("This is a demonstration of backslash.")
is the same as writing :
print("This is a demonstration \
of backslash")
On the other hand with "\n" you can change line in what you want to print. For example, when you write:print("this is an \nexample"), it will print "this is an"(changes line) "example".
Use \n to have your output go to the next line.
print('Hello \nworld!')
Hello
world!
Use the back slash with a character that has a meaning to Python when you want that character to appear in the printed output.
print('It\'s cold outside')
It's cold outside
I hope this helps. 😀
As excellently answered by #Jimmy I further give the following examples to make the matter more clear.
Case 1:
>>> var1 = "Adolf Hitler was a German dictator. He started the second world war."
>>> print(var1)
Adolf Hitler was a German dictator. He started the second world war.
>>>
Case 2:
>>> var2 = "Adolf Hitler\
... was a German dictator.\
... He started the\
... second world war."\
...
>>> print(var2)
Adolf Hitler was a German dictator. He started the second world war.
>>>
Case 3:
>>> var3 = "Adolf Hitler\nwas a German dictator.\nHe started the\nsecond world war."
>>> print(var3)
Adolf Hitler
was a German dictator.
He started the
second world war.
>>>
Case 4:
>>> var4 = "Adolf Hitler\
... \nwas a German dictator.\
... \nhe started the\
... \nsecond world war."\
...
>>> print(var4)
Adolf Hitler
was a German dictator.
he started the
second world war.
>>>
There is also another point which #Jimmy has not mentioned. I have illustrated it by the following two examples -
Example 1:
>>> var5 = """
... This multi-line string
... has a space at the top
... and a space at the bottom
... when it prints.
... """
>>> print(var5)
This multi-line string
has a space at the top
and a space at the bottom
when it prints.
>>>
Example 2:
>>> var6 = """\
... This multi-line string
... has no space at the
... top or the bottom
... when it prints.\
... """
>>> print(var6)
This multi-line string
has no space at the
top or the bottom
when it prints.

use translate method with one parameter [duplicate]

Let's say I have the string '2okjser823ab'. How can I remove all the numbers from the string using .translate()?
I see that in Python 2.x you can do something like .translate(None, '0123456789') but if I try this in Python 3 it tells me that the method only takes one argument.
It looks like it's a bit harder in Python 3; not sure why.
Here's what you can do:
>>> import string
>>> translation = str.maketrans(string.ascii_letters, string.ascii_letters, string.digits)
>>> "2okjser823ab".translate(translation)
'okjserab'
You may need to expand string.ascii_letters with whatever else you expect as input (string.ascii_letters + string.punctuation + string.whitespace, for example).
Edit: I can't find it clearly in the documentation for str.maketrans, but if you use an empty string for the first two arguments, everything will be mapped 1 to 1, but the deletion part (the third argument) still happens:
>>> translation = str.maketrans("", "", string.digits)
>>> "2eeŷýéeokjser823 ab.;\t".translate(translation)
'eeŷýéeokjser ab.;\t'
Here is the documentation for maketrans in Python 3, and for string functions.
In maketrans(x, y, z), each character in x is mapped to the character at the same position in y - setting up a substitution of x for y. Characters in z are mapped to None. Consequently, they will be removed.
Adding to user707650's answer, here is a more compact implementation, where we pass empty strings '' as the first two arguments. Passing string.digits as the third argument strips away digits 0-9.
import string
line = '# qu1ck br0wn f0x jump3 0ver the l#zy d0g'
line = line.translate(line.maketrans('', '', string.digits))
print(line)
# Result: # quck brwn fx jump ver the l#zy dg
If you would also like to remove punctuation, then you can add
line = line.translate(line.maketrans('', '', string.punctuation))
# Result: quck brwn fx jump ver the lzy dg
You may use the strip function :
strings="tycoon0123456789999"
strings.strip("0123456789")

Using .translate() on a string to strip digits [Python 3]

Let's say I have the string '2okjser823ab'. How can I remove all the numbers from the string using .translate()?
I see that in Python 2.x you can do something like .translate(None, '0123456789') but if I try this in Python 3 it tells me that the method only takes one argument.
It looks like it's a bit harder in Python 3; not sure why.
Here's what you can do:
>>> import string
>>> translation = str.maketrans(string.ascii_letters, string.ascii_letters, string.digits)
>>> "2okjser823ab".translate(translation)
'okjserab'
You may need to expand string.ascii_letters with whatever else you expect as input (string.ascii_letters + string.punctuation + string.whitespace, for example).
Edit: I can't find it clearly in the documentation for str.maketrans, but if you use an empty string for the first two arguments, everything will be mapped 1 to 1, but the deletion part (the third argument) still happens:
>>> translation = str.maketrans("", "", string.digits)
>>> "2eeŷýéeokjser823 ab.;\t".translate(translation)
'eeŷýéeokjser ab.;\t'
Here is the documentation for maketrans in Python 3, and for string functions.
In maketrans(x, y, z), each character in x is mapped to the character at the same position in y - setting up a substitution of x for y. Characters in z are mapped to None. Consequently, they will be removed.
Adding to user707650's answer, here is a more compact implementation, where we pass empty strings '' as the first two arguments. Passing string.digits as the third argument strips away digits 0-9.
import string
line = '# qu1ck br0wn f0x jump3 0ver the l#zy d0g'
line = line.translate(line.maketrans('', '', string.digits))
print(line)
# Result: # quck brwn fx jump ver the l#zy dg
If you would also like to remove punctuation, then you can add
line = line.translate(line.maketrans('', '', string.punctuation))
# Result: quck brwn fx jump ver the lzy dg
You may use the strip function :
strings="tycoon0123456789999"
strings.strip("0123456789")

Categories