English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans cet article, vous découvrirez les instructions Python, l'importance de l'indentation et l'utilisation des commentaires dans la programmation.
Les instructions exécutables par l'interpréteur Python sont appelées instructions. Par exemple, a = 1 sont des instructions d'affectation. Les instructions if, for, while, etc. sont d'autres types d'instructions qui seront discutés plus tard.
En Python, la fin des instructions est marquée par un retour chariot. Mais nous pouvons étendre une instruction sur plusieurs lignes avec des caractères continus (\). Par exemple :
a = 1 + 2 + 3 + \ 4 + 5 + 6 + \ 7 + 8 + 9
C'est une continuation de ligne explicite. En Python, les parenthèses () et les crochets [] et {} contiennent implicitement des retours chariots.
a = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9)
Ici, les parenthèses () autour sont implicitement utilisées pour la continuité des lignes. C'est aussi le cas pour [] et {} . Par exemple :
colors = ['red', 'blue', 'green'
Nous pouvons également utiliser des points-virgules pour placer plusieurs instructions sur une ligne, comme suit
a = 1; b = 2; c = 3
La plupart des langages de programmation (par exemple C, C ++,Java) utilisent des accolades {} pour définir les blocs de code.Tandis que Python utilise l'indentation).
Les blocs de code (Les fonctionsLe corpsLes bouclesLe corps (par exemple) commence par une indentation et se termine par la première ligne non indentée. La quantité d'indentation dépend de vous, mais elle doit rester constante tout au long du bloc.
Généralement, quatre espaces sont utilisés pour l'indentation, et ont la priorité sur les tabulations. Voici un exemple.
L'implémentation de l'indentation en Python rend le code propre et bien organisé. Cela conduit à des programmes Python semblables et cohérents.
L'indentation peut être ignorée sur des lignes consécutives. L'indentation constante est une bonne habitude. Elle rend le code plus lisible. Par exemple :
if True: print('Hello') a = 5
and
if True: print('Hello'); a = 5
Both are valid and do the same thing. But the former style is clearer.
Incorrect indentation will cause IndentationError.
Comments are very important when writing programs. They describe what is happening inside the program, so that the person viewing the source code will not be confused. You might forget the key details of a program you wrote a month ago. Therefore, it is always meaningful to spend time explaining these concepts in the form of comments.
In Python, we use the hash (#) to start writing comments.
It extends to the newline character. Comments are for programmers to better understand the program. Python interpreter ignores comments.
# This is a comment # Print output Hello print('Hello')
If we have extended multi-line comments, one way is to use hash (#) at the beginning of each line. For example:
# This is a long comment # It extends # To multiline
Another way to do this is to use triple quotes, ''' or """.
These triple quotes are usually used for multi-line strings. But they can also be used as multi-line comments. Unless they are not documentation strings, they will not generate any additional code.
"""This is also an A perfect example Multi-line comment """
Docstring is the abbreviation of documentation string.
It is astring, as the first statement in the module, function, class, or method definition. We must write the function/The role of the class.
Use triple quotes when writing documentation strings. For example:
def double(num): """The function doubles the value""" return 2*num
Docstring as the __doc__ attribute of the function is available for us to use. After running the above program, issue the following code in the shell.
def double(num): """The function doubles the value""" return 2*num print(double.__doc__)
Output:
The function doubles the value