# 2 [python3] Separation and comment out

Introduction

This article is written by a fledgling engineer who has been studying programming for about two months for the purpose of output. After biting ruby and js, I became interested in the trend python, so I started learning. In this article, I'll write about delimiters and comment outs. It will be a short article, but I would appreciate it if you could point out any points you are interested in! This article is based on the assumption that python3 and anaconda are installed on macOS.

1 Statement (instruction statement) delimiter

The program code also needs to be separated for each punctuation mark "." In Japanese and the period "." In English. In the case of Japanese or English, it is possible to read even if there is no delimiter in the sentence, but in the case of program code, an error will occur if the delimiter is not inserted correctly.

** Error if not separated correctly **

test.py


hige = 1 * 5 hoge = 2 * 2 print(hige) print(hoge)

↑ in the terminal

$ python test.py
  File "test.py", line 1
    hige = 1 * 5 hoge = 2 * 2 print(hige) print(hoge)
                 ^
SyntaxError: invalid syntax

SyntaxError: invalid syntax means syntax error in Japanese. This error is easy to resolve. There are two main methods, so I will introduce them.

** ①; How to separate with ** The first is to separate them with a semicolon. I will actually try it.

test.py


hige = 1 * 5 ; hoge = 2 * 2 ; print(hige) ; print(hoge)

↑ in the terminal

$ python test.py
5
4

I was able to resolve the error by separating the statements with; in this way.

** ② How to separate with line breaks ** The second method is to separate them with line breaks. I will actually try this as well.

test.py


hige = 1 * 5
hoge = 2 * 2 
print(hige)
print(hoge)

↑ in the terminal

$ python test.py
5
4

I was able to execute this without causing an error. Then, which one should I separate? I think some people think that, but if you develop it individually, you can decide it completely according to your preference. If the rules are set by the company or team you belong to, I think you will follow them.

2 Comment out

The comment-out function is indispensable when annotating the code and checking the operation, so it is a place to remember. I will introduce two this time.

** ① How to use # ** You can comment out by adding # at the beginning of the sentence. Let's actually annotate the code.

test.py


hige = 1 * 5 #This is 1 to hige*Substituting 5

print(hige)

I was able to annotate the code. The statement after the # does not affect the execution of the code.

** How to use ② "** "You can comment out all at once by enclosing the sentence with three.

test.py


hige = 1 * 5 
"""
↑ is 1 for hige*Substituting 5.
Inside double quotes does not affect code execution.
This is useful when you want to comment out over multiple lines.
"""
print(hige)

At the end

This is the end of this article. I want to make good use of delimiters and comment outs and try to write code that is easy to read! Next article → https://qiita.com/shin12032123/items/89ecbff9257833eceff3

Recommended Posts

# 2 [python3] Separation and comment out
python comment out
Comment out
2015-03-25 python> Multi-line comment> Watch out for indent
[python] Compress and decompress
Python and numpy tips
[Python] pip and wheel
Batch design and python
Python iterators and generators
Ruby, Python and map
python input and output
Python and Ruby split
Python3, venv and Ansible
Python asyncio and ContextVar
I checked out the versions of Blender and Python
[Python] Find out about pip
Programming with Python and Tkinter
Encryption and decryption with Python
Python: Class and instance variables
3-3, Python strings and character codes
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Python indentation and string format
Python real division (/) and integer division (//)
Install Python and Flask (Windows 10)
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
Python shallow copy and deep copy
Python and ruby slice memo
Python installation and basic grammar
I compared Java and Python!
Python shallow and deep copy
About Python, len () and randint ()
About Python datetime and timezone
Install Python 3.7 and Django 3.0 (CentOS)
Python environment construction and TensorFlow
Python class variables and instance variables
Ruby and Python syntax ~ branch ~
[Python] Python and security-① What is Python?
Stack and Queue in Python
python metaclass and sqlalchemy declareative
Fibonacci and prime implementations (python)
Python basics: conditions and iterations
Python bitwise operator and OR
Python debug and test module
Python list and tuples and commas
Python variables and object IDs
Python list comprehensions and generators
About Python and regular expressions
python with pyenv and venv
Unittest and CI in Python
Maxout description and implementation (Python)
[python] Get quotient and remainder
Python 3 sorted and comparison functions
[Python] Depth-first search and breadth-first search
Identity and equivalence Python is and ==
Source installation and installation of Python
Python or and and operator trap