#3 Python Syntax / Python Lessons for Beginners

by Alex
#3 Python Syntax / Python Lessons for Beginners

Running Python code

As we learned in the previous lesson, Python code is executed by writing directly on the command line:


>>> print("Hello, World!")
Hello, World!

Or, by creating a python file on the server, using the .py extension and running it on the command line:

C:\Users\YourName> python myfile.py

Indentation in Python

While in other programming languages indentation in code is for reading only, in Python indentation is very important. Python uses indentation to specify a block of code.


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

Python will report an error if you miss an indentation:


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

Result:

  File "demo_indentation_test.py", line 2
print("Five is greater than two!")
^
IndentationError: expected an indented block

Comments

Python provides the ability to comment on documentation within code. Comments should start with the # character and the interpreter will display the rest of the line as a comment:


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

Documentation Strings

Python also has advanced documentation features called docstrings. Documentation strings can be single-line or multi-line. Python uses triple quotes at the beginning and end of docstring.


"""This is a multiline
documentation string"""
print("Hello, World!")

Related Posts

LEAVE A COMMENT