What does the ' ; ' symbol mean in python? [duplicate] - python

This question already has answers here:
Why is semicolon allowed in this Python snippet?
(15 answers)
What does a semicolon do?
(5 answers)
Closed 8 years ago.
I sometimes see this ; symbol on tutorials and such, what does it indicate?

Semi-colons are used in Python to separate statements in the same line.
print 1; print 2
Common use example:
import pdb; pdb.set_trace()

The semicolon can be used as a statement separator in Python, called stmt_list in the language reference, but its use is generally discouraged (PEP 8, compound statements). We prefer one statement per line.

Related

Python with statement break line without backslash [duplicate]

This question already has answers here:
Python multi-line with statement
(6 answers)
Closed 5 years ago.
How can I break this line without using "\"?
with mock.patch('six.moves.builtins.open', mock.mock_open()), mock.patch('my_module.yaml.safe_load') as mock_yaml:
#do something
I tried with parenthesis but it complains with SyntaxError about the "as"
with (mock.patch('six.moves.builtins.open', mock.mock_open()),
mock.patch('my_module.yaml.safe_load') as mock_yaml):
#do something
Breaking it inside a set of parentheses, but without the parentheses around mock.patch(....), should work according to the PEP8 Python Style Guide, due to implied line continuation between parentheses:
with mock.patch('six.moves.builtins.open',
mock.mock_open()), mock.patch('my_module.yaml.safe_load') as mock_yaml:
The other option is similar to your second suggestion, but with the closing parentheses moved to before the as:
with (mock.patch('six.moves.builtins.open', mock.mock_open()),
mock.patch('my_module.yaml.safe_load')) as mock_yaml:

How to add an integer into this Regex in Python without hard coding the integer? [duplicate]

This question already has answers here:
How to use a variable inside a regular expression?
(12 answers)
Closed 5 years ago.
I want to eventually be able to increment an integer within my Regex, but the braces are preventing me from doing so.
So far I have:
start = 6
m = re.search(r"(.{{n},}).*?\1".format(n=start), s)
return m.group(1)
However, I get `ValueError: Single '}' encountered in format string
I am using Python 2.7.
What about using a different method of string formatting:
m = re.search(r"({.%s,}).*?\1" % start, s)

Why semi-colon in python after end of print and variable working? [duplicate]

This question already has answers here:
What does a semicolon do?
(5 answers)
Closed 5 years ago.
I am new to python and currently working in it.
I have write one simple program, as i read somewhere that semicolon not allowed or required in python to end statement. but i have use that and till its working fine! anyone explain me
why its possible?
here is code.
a = 10;
if a == 10:
print "value of a is %s"%(a);
else:
print "value of a is not %s"%(a);
semicolon is allowed as statement separator
>>> a=1;b=2;c=3
>>> print(a,b,c)
1 2 3
It is not required if you write each statement in a new line
Python does not require semi-colons to terminate statements. Semi-colons can be used to delimit statements if you wish to put multiple statements on the same line.

%% in sec2time() Python [duplicate]

This question already has answers here:
How can I selectively escape percent (%) in Python strings?
(6 answers)
Closed 6 years ago.
In the sec2time() Python function provided by Lee he uses a syntax I'm struggling to understand:
pattern = '%%02d:%%02d:%%0%d.%df' % (n_msec+3, n_msec)
What is the %%here and how does it affect the outcome?
The % in that string introduces replaceable parts as at the end %d.%df. If you want a % in the output you have to do something special, in this case use %%
After these substitustions the resulting pattern will look like:
'%02d:%02d:%0123.120f'
which, among other things an be used for further substitution.
In the documentation, at the bottem of the second table in that section, it states:
'%' No argument is converted, results in a '%' character in the result.

Why import statements with parentheses? [duplicate]

This question already has answers here:
Is it possible to break a long line to multiple lines in Python? [duplicate]
(7 answers)
Closed 7 years ago.
Lately have seen imports like this
from module import (function, another_function,
another_function)
Seemingly this has been done to be able to stretch the import statement over more than one line. In cases like this I usually just import like so
from module import function, another_function, \
another_function
What exactly are the parentheses doing in this case and are they considered to be bad practice?
As PEP 8 states:
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.

Categories