Execute Python Syntax
There are two ways to run Python syntax: type it straight into the Command Line, or save it on the server in a file with the .py extension and run that file from the Command Line.
Command line execution
>>> print("Hello, World!")
Hello, World!
Run a Python file
C:\Users\Your Name>python myfile.py
Python Indentation
The spaces at the start of a line of code are called indentation.
Other languages use indentation only to make code easier to read. Python is different and depends on it, because indentation is how it marks a block of code.
Example
Output: complete Python program
if 5 > 2:
print("Five is greater than two!")
Syntax Error
Leaving out the indentation makes Python report an error:
Output: complete Python program
if 5 > 2:
print("Five is greater than two!")
Different spaces
You decide how many spaces to use. Four is the most common choice, and one is the minimum.
Output: complete Python program
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
Mixed indentation error
Within a single block of code, the number of spaces has to stay the same. If it changes, you get an error:
Output: complete Python program
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Python Variables
You don't need a special command to declare a variable in Python. It comes into existence as soon as you assign it a value.
Example
Creating and printing variables:
Output: complete Python program
x = 5
y = "Hello, World!"
print(x)
print(y)
Comments
Python lets you document your code with comments. A comment begins with #, and everything after it on that line is ignored.
Example
A comment in Python:
Output: complete Python program
#This is a comment.
print("Hello, World!")