syntax - What does _ in Python do? -
this question has reply here:
what purpose of single underscore “_” variable in python? 2 answersi saw somewhere _
character beingness used in python like:
print _
can help me explain does?
in interactive interpreter, _
refers lastly outputed value:
>>> 1 + 1 2 >>> print _ 2 >>> 2 + 2 4 >>> print _ 4 >>>
in normal python1 code however, _
typical name. can assign other:
_ = 3 print _ # output: 3
although wouldn't recommend doing because _
terrible name. also, used convention mean name placeholder. illustration be:
a, _, b = [1, 2, 3]
which uses _
mean not interested in 2
. illustration is:
for _ in range(10): function()
which means not using counter variable within loop. instead, want python phone call function
10 times , need _
have valid syntax.
1by "python", mean cpython, standard flavor of language. other implementations may take things differently. ipython illustration has underscore-only names:
the next global variables exist (so don’t overwrite them!):
[_] (a single underscore) : stores previous output, python’s default interpreter. [__] (two underscores): next previous. [___] (three underscores): next-next previous.
source: http://ipython.org/ipython-doc/rel-0.9.1/html/interactive/reference.html#output-caching-system
python syntax
No comments:
Post a Comment