English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Funktion in Python

Datentypen in Python

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Anweisungen, Einrückung und Kommentare in Python

In this article, you will learn about Python statements, the importance of indentation, and the use of comments in programming.

Python statements

Instructions that the Python interpreter can execute are called statements. For example, a = 1 is an assignment statement. If statements, for statements, while statements, and so on are other types of statements that will be discussed later.

Multiline statements

In Python, the end of a statement is marked with a newline character. However, we can extend a statement to multiple lines with a continuous character (\). For example:

a = 1 + 2 + 3 + \
    4 + 5 + 6 + \
    7 + 8 + 9

This is an explicit line continuation. In Python, there is an implied newline character in parentheses () and square brackets [] and curly braces {}.

a = (1 + 2 + 3 +
    4 + 5 + 6 +
    7 + 8 + 9)

Here, the surrounding parentheses () implicitly continue the line. The same is true for [] and {}. For example:

colors = ['red',
          'blue',
          'green'

We can also use semicolons to place multiple statements on a single line, as shown below

a = 1; b = 2; c = 3

Python indentation

Most programming languages (such as C, C ++, Java) all use curly braces {} to define code blocks.While Python uses indentation).

Code block (Function'sBody,Loop'sThe body (and so on) starts with indentation and ends with the first line that is not indented. The amount of indentation depends on you, but it must be consistent throughout the block.

Generally, four spaces are used for indentation, and they take precedence over tabs. Here is an example.

The implementation of indentation in Python makes the code look neat and clean. This leads to a consistent and uniform appearance of Python programs.

Indentation can be ignored in consecutive lines. It is always a good habit to indent. It makes the code more readable. For example:

if True:
    print('Hello')
    a = 5

und

if True: print('Hello'); a = 5

Beide sind gültig und tun dasselbe. Aber das erste Stil ist klarer.

Falsche Einrückung führt zu einem IndentationError.

Python-Kommentare

Kommentare sind bei der Programmierung sehr wichtig. Sie beschreiben, was im Programm intern passiert, so dass der Betrachter des Quellcodes nicht so verwirrt wird. Sie könnten die wichtigen Details eines Programms, das Sie vor einem Monat geschrieben haben, vergessen haben. Daher ist es immer sinnvoll, Zeit zu investieren, um diese Konzepte in Form von Kommentaren zu erklären.

In Python verwenden wir das Octothorn (#), um Kommentare zu schreiben.

Er erstreckt sich bis zum Zeilenumbruch. Kommentare sind für Programmierer gedacht, um das Programm besser zu verstehen. Der Python-Interpreter ignoriert Kommentare. 

# Dies ist ein Kommentar
# Drucke ausgeben Hello
print('Hello')

Mehrzeilige Kommentare

Wenn wir erweiterte mehrzeilige Kommentare haben, eine Methode ist, am Anfang jeder Zeile ein Hash (#) zu verwenden. Zum Beispiel:

# Dies ist ein langer Kommentar
# es erstreckt sich
# bis zu mehreren Zeilen

Eine andere Methode, dies zu tun, ist die Verwendung von dreifachen Anführungszeichen, ''' oder """.

Diese dreifachen Anführungszeichen werden normalerweise für mehrzeilige Strings verwendet. Sie können aber auch als mehrzeilige Kommentare dienen. Es wird nichts zusätzliches generiert, es sei denn, es handelt sich nicht um einen Dokumentationsstring.

"""Dies ist auch ein
Perfekter Beispiel
Mehrzeilige Kommentare """

Dokumentationsstrings in Python

Docstring ist die Abkürzung für Dokumentationsstring.

Es ist einZeichenketteerscheint als erste Zeile in der Definition eines Moduls, einer Funktion, einer Klasse oder eines Methoden. Wir müssen die Funktion in der Dokumentationszeichenfolge beschreiben/Die Funktion der Klasse.

Verwenden Sie dreifache Anführungszeichen, um Dokumentationsstrings zu schreiben. Zum Beispiel:

def double(num):
    """Funktion verdoppelt den Wert"""
    return 2*num

Die Docstring als Attribut __doc__ der Funktion kann verwendet werden. Nach dem Ausführen des obigen Programms geben Sie im Shell die folgenden Befehle ein.                                                                                                              

def double(num):
    """Funktion verdoppelt den Wert"""
    return 2*num
print(double.__doc__)

Ausgabe:

Funktion verdoppelt den Wert