Why does Python have to write a colon?

In Python, complex statements such as if, for, def, and class require a colon :.

if True:
    print('Hello, world!')

The header of each section begins with a uniquely identifying keyword and ends with a colon. 8. compound statement-Python language reference

Official documentation

The official documentation explains why you have to write the colon :.

(Delusion) Personally ...

1. ABC language etc.

When writing in one line with a semicolon ; in one line, it is easier to understand. At this time, it would be quite confusing without :.

def f(x): y = x + 1; z = y + 2; return z;

>>> def f(x): y = x + 1; z = y + 2; return z;
>>> f(0)

In the official document, the method of writing in multiple lines is given as an example in the talk of ABC language etc., but I personally think that the one-liner using this semicolon ; was more critical ( I rarely use it, but lol).

It's a simple sentence, but I think it's easier to understand if there is a colon : in the case of a lambda expression.

g = lambda x: x + 1
>>> g = lambda x: x + 1
>>> g(0)

2. Analysis etc.

I've used indentation instead of parentheses to group sentences. Therefore, isn't it somewhat strict to claim that "it is easier to analyze if you use a colon: only at the beginning"? I personally think.

(Delusion) Is it necessary?

Don't you need this colon :? Is often criticized. I don't think I'm honest either. This is because I rarely write compound sentences with one liner.

Python is now quite versatile, but with the spirit of "you don't have to have anything you want," I feel like I was cautious about adding new features. I think that spirit is clearly expressed by the following words of PEP 20.

There should be one-- and preferably only one --obvious way to do it. PEP 20 -- The Zen of Python

It may be more accurate here to say "I don't need the ability to write in one liner with a semicolon; " rather than not needing a colon :.

I thought about the process of writing a colon, but ... I want to be able to write with a semicolon ; even with a one-liner. To do so you need to write a colon :. Writing or not writing a colon : is inconsistent with and without one-liner.

Special cases aren't special enough to break the rules. PEP 20 -- The Zen of Python

So let's unify by adding a colon :. I personally wonder if that was the case.

Recommended Posts

Why does Python have to write a colon?
How to write a Python class
Why Python slicing is represented by a colon (:)
I want to write to a file with Python
I want to write in Python! (2) Let's write a test
How to write a list / dictionary type of Python3
[Python] How to write a docstring that conforms to PEP8
[Python] A memo to write CSV vertically with Pandas
A program to write Lattice Hinge with Rhinoceros with Python
Write code to Unit Test a Python web app
A road to intermediate Python
Write to csv with Python
Write a python program to find the editing distance [python] [Levenshtein distance]
How to write a metaclass that supports both python2 and python3
[Python] Write to csv file with Python
Write A * (A-star) algorithm in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
5 Ways to Create a Python Chatbot
Write a batch script with Python3.5 ~
A special Python codec that seems to know but does not know
[Python] I made a decorator that doesn't seem to have any use.
Qiita (1) How to write a code name
Have Alexa run Python to give you a sense of the future
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
Write the test in a python docstring
A super introduction to Python bit operations
How to write Python document comments (Docstrings)
Send a message from Python to Slack
I want to build a Python environment
[Python] How to invert a character string
[Python] Create a linebot to write a name and age on an image
I have a question! (Python, django) Easy
How to get a stacktrace in python
Write a Caesar cipher program in Python
Write a simple greedy algorithm in Python
Write a TCP client with Python Twisted
How to write a string when there are multiple lines in python
Tips for Python beginners to use Scikit-image examples for themselves 3 Write to a file
Write a simple Vim Plugin in Python 3
[Introduction to Python] How to write a character string with the format function
Compress python data and write to sqlite
Creating a shell script to write a diary
A way to understand Python duck typing
How to write Ruby to_s in Python
Add a Python virtual environment to VSCode
How to run a Maya Python script
[Python] Concatenate a List containing numbers and write it to an output file.
Searching for an efficient way to write a Dockerfile in Python with poetry
I didn't have to write a decorator in the class Thank you contextmanager
Send a message from Slack to a Python server
[Python] List Comprehension Various ways to create a list
Edit Excel from Python to create a PivotTable
How to read a CSV file with Python 2/3
Send a message to LINE with Python (LINE Notify)
Try to calculate a statistical problem in Python
How to create a Python virtual environment (venv)
How to open a web browser from python
[Python] Road to a snake charmer (4) Tweak Numpy