Sober trivia of python3

There are some points that I don't seem to know when I look at other people's articles.

Only one sentence can be written after the colon

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

If the continuation of the expression is obvious, you can start a new line at any place.

For example, comprehension

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.

Tuple ... inclusion notation ...?

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".

Tuples are syntaxes that consist of commas (in parentheses) in the first place.

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

Sober trivia of python3
Introduction of Python
Basics of Python ①
Basics of python ①
Copy of python
Introduction of Python
[Python] Operation of enumerate
List of python modules
Unification of Python environment
[python] behavior of argmax
Usage of Python locals ()
Installation of Python 3.3 rc1
# 4 [python] Basics of functions
Basic knowledge of Python
Summary of Python arguments
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Various processing of Python
[Python] Correct usage of map
Towards the retirement of Python2
Summary of python file operations
Recommendation of binpacking library of python
[python] Value of function object (?)
Automatic update of Python module
[Python] Etymology of python function names
About the ease of Python
Static analysis of Python programs
About various encodings of Python 3
Equivalence of objects in Python
Introduction of activities applying Python
python> Handling of 2D arrays
Install multiple versions of Python
Version upgrade of python Anaconda
2.x, 3.x character code of python
Comparison of 4 Python web frameworks
Simple FPS measurement of python
Check OpenSSL version of python 2.6
Python implementation of particle filters
Post processing of python (NG)
[Python] Copy of multidimensional list
Faster loading of Python images
Sample usage of Python pickle
Basic usage of Python f-string
Implementation of quicksort in Python
[Python] Correct usage of join
About the features of Python
Source installation and installation of Python
Full understanding of Python debugging
Environment construction of python2 & 3 (OSX)
Basics of Python × GIS (Part 1)
The Power of Pandas: Python
I'm thinking of studying Python
[Python] Tuple version of prefecture pull-down
A record of patching a python package
Environment construction of python and opencv
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
The story of Python and the story of NaN
A good description of Python decorators
Image capture of firefox using python