pythonmyfile.pyPython Indentation Indentation refers to the spaces at the beginning of lines of code. In other programming languages, code is indented just for readability, but indentation in Python is very important. Python uses indentation"/> pythonmyfile.pyPython Indentation Indentation refers to the spaces at the beginning of lines of code. In other programming languages, code is indented just for readability, but indentation in Python is very important. Python uses indentation">
Home > Article > Backend Development > Python syntax example code analysis
As we learned in the previous section, you can write the syntax to execute Python directly on the command line:
>>> print("Hello, World!") Hello, World!
Or by creating a python file on the server , using the .py file extension, and running it from the command line:
C:\Users\Your Name>python myfile.py
Indentation refers to the spaces at the beginning of lines of code.
In other programming languages, code indentation is only for readability, but indentation in Python is very important.
Python uses indentation to indicate blocks of code.
Example
if 5 > 2: print("Five is greater than two!")
Running Example
##If indentation is omitted, Python will error:Example
Syntax error:if 5 > 2: print("Five is greater than two!")Running Example The number of spaces depends on the programmer, but At least one is required.
Example
if 5 > 2: print("Five is greater than two!") if 5 > 2: print("Five is greater than two!")Running Example You must use the same number of spaces in the same code block, Otherwise Python will error:
Example
Syntax error:if 5 > 2: print("Five is greater than two!") print("Five is greater than two!")Run Example Python variablesIn Python, a variable is created when a value is assigned to it:
Example
Variables in Python:
x = 5 y = "Hello, World!"Run Example Python No command to declare variablesIn the Python Variables chapter we learn more about variables. CommentsPython has the function of commenting the code in the document. Comments start with # and Python renders the rest as a comment:
Example
Comments in Python:#This is a comment. print("Hello, World!")Running instance
The above is the detailed content of Python syntax example code analysis. For more information, please follow other related articles on the PHP Chinese website!