What is the note in Python? How to write Python command? Let's find out together!
Python is one of the most popular programming languages today. You can easily find it in the most popular programming products today. Using Python is not difficult, basically, you need to understand the most common concepts and features.
When writing code in Python, it is important to make sure your code can be easily understood for others. Name clear variables, define your clear functions and arrange your code are great ways to do this.
Another great and easy way to increase the ability to read your code is to use the caption!
In this tutorial, you will learn some basic things about how to write notes in Python. You will learn how to write clear and concise captures, and when you may not need to write any comments at all, how to indent and more.
Write commands in Python
Python syntax can be deployed by writing directly into the Command Line:
>>> print("Hello, World!")
Hello, World!
In there, print
A function in Python used to print a string to the screen. In quotes (“”) is the content that you want to display on the screen.
Or by creating a python file on the server, using the extended .po file tail and running it in Command Line:
C:\Users\Your Name>python myfile.py
To run a Python program, you can use an online Python compiler or Python running on your computer.
Write Python statements on many lines
In Python, a statement will end by downstream. However, sometimes you need to write a longer statement in Python and want to go down to make it easier to read.
To write a statement on many lines in Python, you can use the parentheses ()
square brackets []
or braces {}
. The parentheses here implicitly continue the command line remains until the brackets are closed.
For example, to write a statement print
On many lines, you can use the following wrapped characters:
mau_sac = ['vàng',
'xanh',
'cam']
Note that when writing a command on multiple lines, you need to make sure that the next lines are backed (indent) to let Python know that it is part of the previous command.
b = (1 + 3 + 5 +
7 + 9 + 11 +
13 + 15 + 17)
However, we do not always need to use brackets to the line for the command, to be flexible, you can also expand the command on multiple lines with the order to continue the command line (\
). The above example can be rewritten as follows:
b = 1 + 3 + 5 + \
7 + 9 + 11 + \
13 + 15 + 17
In addition, you can also put multiple commands on the same line, separated by a set ;
like this:
a = 1; b = 2; c = 3
Back in the margin Python
If you have learned C, C ++ or Java, you will know that these programming languages use { }
To determine the code blocks. In Python, different, the blocks will be identified through indent. That is why the indent in Python is extremely important, if you indent or mistakenly will be reported immediately.
A code block (usually the command block of the jaw, loop, …) begins with an indented and ends with the first line without indentation. How much indent is depending on you but they must be consistent throughout the code block, that is, the commands in the same block must have equal induction.
Indulating indicating the distance starting the code line. In other programming languages, the head indent in the code is only for easy reading, but the head indent in Python is very important.
Python uses a line to indicate a block of code. For example:
if 5 > 2:
print("Five is greater than two!")
Python will report an error if you skip the head indent:
File "", line 2
print("Five is greater than two!")
^
IndentationError: expected an indented block
>
The number of backward distance depends on the habit of you programming web or app. The most common is 4 but the smallest number must be a distance.
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
Usually, will use 4 times to indent. The most favorite programmer is the tab key, like the example below:
for i in range(1,11):
print(i)
if i == 5:
break
Command block of for
include print(i)
and if
written equally, break
is the inner command if
should be again indented again. You do not need to understand what the above code says, just remember in your mind about Python's indent, gradually we will learn details, one by one.
If the above command is written into:
for i in range(1,11):
print(i)
if i == 5:
break
You will receive an error notice immediately, and the error will appear before the command print(i)
.
Thanks to the head of the line, the code in Python looks neat and clearer. You can skip the headquarters with commands written on many lines, but experienced programmers recommend that: Always indent, which makes the code easier to read.
For example:
if True:
print('Xin chào!')
q = 10
And:
if True: print('Xin chào!'); q = 10
Both of the above writing are correct and do the same work, but do you see how to write the head more clearly?
Note, comments in Python
NOTE The way the code writer communicates with the code reader, it can be said that this is an indispensable component in the programming code section. It helps describe what is happening in the program so that the code reader does not take too much time to learn and speculate.
More practically, you can write many programs at the same time, interwoven, confuse or forget the important details of the code that you have written earlier for a whole month is normal. That is the time when notes and comments become the end. Practice the habit of writing these notes now.
In Python, we use characters #
To start a caption. NOTE BEFORE after the sign #
Until started a new line. When interpreted, Python will ignore these notes.
#Đây là chú thích
#In dòng chữ Quantrimang.com
print('Quantrimang.com')
You can write notes on the same line with commands or expressions without any problems, like this:
print('Quantrimang.com') #Đây là chú thích in dòng chữ Quantrimang.com
If you want to write notes on many lines, very simple, just add # in front of each line, like this:
#Đây là chú thích
#trên nhiều dòng
#In dòng chữ Quantrimang.com
#trong Python
print('Quantrimang.com')
There is another way to write notes is to use 3 single clicks ' ' '
or dual blink " " "
. These clicks are often used for multi -line string. But they can also be used to write notes on many lines. As long as it is not docstring, it will not create any other code.
"""Đây là chú thích
trên nhiều dòng
In dòng chữ Quantrimang.com
trong Python"""
How to use comments in python
Making code easier to understand
If you write a comment in the code when programming, you can easily refer them more in the future. In addition, your code is easier to understand with other programmers.
Use comments to remove
If you encounter an error while running the program, you can leave a comment on the code line that causes error instead of removing it. For example:
print('Python')
# print('Error Line )
print('Django')
Here, Print ('Error Line) It is causing an error, so it has been changed to a comment. Now that program runs normally without errors.
This is a comment that can become a valuable debugging tool.
Note: Always use a comment to explain why the data in the code is changed instead of how to do something. Do not use comments to explain code quality.
Notes in Python is not difficult? The following article will guide you in detail how to write the headquarters and captions in Python.
How to take notes in Python properly
Comments or notes are an important part of a program. Therefore, you need to learn how to write appropriate notes. Here are some characteristics that determine the comments and notes that are written well:
- Make sure they are accurate.
- Do not write general comments, write clearly and specifically (a = 10 #attached to 10 to avoid writing general comments).
- Write a comment describing the overview of a function or method, not a specific detail.
- Good comments and notes are always clear.
- Do not write redundant notes.
Docstring in python
Docstring Is the abbreviation of Documentation String – Documentary series, occurs as the first command in the module, function, definition method. You will have to write what the function, the class will do in the docstring.
Three double clicks are used to write docstring as the example below:
def double(num):
"""Ham nhan doi gia tri"""
return 2*num
Docstring will appear as attributes __doc__
of the function. To see the function of the function you can use the command print()
As below:
def double(num):
"""Ham nhan doi gia tri"""
return 2*num
print(double.__doc__)
Result:
Ham nhan doi gia tri
Wait for the user to take action
The following command line will display the prompt, the statement says “Press the Enter key to escape!“And wait for the user's actions.
input("\n\nNhấn phím Enter để thoát!")
Here, “\n\n
“Used to create two new lines before displaying the actual line. Once the user presses Enter the program will end. This is a good trick to keep the program window display until the user is complete socks with the application.
Test lesson knowledge
Above are things you need to know about how to write commands, notes, indent in Python. Just master the rules and how to write good notes in Python, the Institute programming can read and understand the code easily. It is one of the many basic concepts in Python that you have to learn to master this programming language.