Python Syntax


Execute Python Syntax

As we learned on the previous page, Python syntax can be done by typing directly into 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 in Command Line:


C:\Users\Your Name>python myfile.py


Python Indentation

Undo refers to the spaces at the beginning of the code line.

Where in some languages ​​the indent editing code is readable, retrieving in Python is very important.

Python uses indentation to display a code block.


Example
if 5 > 2:
  print("Five is greater than two!")

Python will give you an error if you skip over:


Example

Syntax Error:

if 5 > 2:
print("Five is greater than two!")

The number of posts is up to you as an editor, but it should be at least one.


Example
if 5 > 2:
 print("Five is greater than two!"
if 5 > 2:
        print("Five is greater than two!"

You must use the same number of spaces in the same code block, otherwise Python will give you an error:


Example

Syntax Error:

if 5 > 2:
 print("Five is greater than two!")
        print("Five is greater than two!")


Python Variables

In Python, a variable is created when you assign value to it:


Example

Variables in Python:

x = 5
y = "Hello, World!"

Python has no dynamic declaration command.


Comments

Python has the ability to comment for coding purposes.

Comments start with #, and Python will provide the whole line as a comment:


Example

Comments in Python:

#This is a comment.
print("Hello, World!")