There are some points that I don't seem to know when I look at other people's articles.
So
exec("for i in range(10):\n\tprint(i)")
this is
for i in range(10):print(i)
Originally it can be written in one line. You don't need exec.
This applies to all syntaxes that use a colon. def, class, while, try / catch, if .... Is there anything else?
If you're exploring the sources of frameworks elsewhere, don't you see this often?
class OreOreException(Exception): pass
xx = ["even" if i % 2 == 0 else "odd" \
for i in range(10)]
this is
xx = [
"even"
if i % 2 == 0
else "odd"
for i in range(10)
]
You can write like this.
For the same reason, you can force a normal expression into multiple lines by enclosing it in parentheses.
If you use a comma even for a moment, only one part of it will be transformed into a tuple notation, so be careful.
I was scared when I saw the articles related to the inclusion notation, but sometimes I sometimes see the Japanese word "tuple inclusion notation".
Pitfalls of Python tuple comprehension -done is better than perfect
Python tuple comprehension and generator expression -Qiita
Starting with python2.5, parenthesized list comprehensions seem to work as generators.
Functional Programming HOWTO — Python 2 \ .7 \ .18 Documentation
Up to 2.4, it may have been "tuple comprehension".
5 . Data Structures — Python 3 \ .8 \ .5 Documentation
Tuples consist of several values separated by commas. For example, write:
It seems that you don't need parentheses .... but commas are required.
On the contrary, how do you write when there is one element? The question comes up.
python - How to create a tuple with only one element - Stack Overflow
>>> type( ('a') )
<type 'str'>
>>> type( ('a',) )
<type 'tuple'>
The point is the comma at the end.
By the way, it is a specification that a tuple is completed by plunging into tuple (). list (), tuple (), dict (), etc. are for accepting generators / iterators directly.
Built-in — Python 3 \ .8 \ .5 documentation
It says tuple ([iterable]), right?
Recommended Posts