Python Syntax

Summary: in this tutorial, we will show you basic Python syntax to help you get started with the language quickly. This tutorials introduced you to some Python language elements including whitespace, indentation, comments, continuation of statements, identifiers and string literals.

Whitespace and indentation

Different from other programming languages such as C/C++ and Java, Python uses whitespace and indentation to establish code block structure.

Let’s take a look at the code below to see how whitespace and indentation are used:

Please pay attention only to the code structure instead of its meaning. We will learn more about the specific language’s element in the next tutorial.

  • First, we defined a  main() function on line 2.  The whole block below the  main() function from line 2 to 7 is the function body which is indented on level.
  • Next, after the while loop code block is the while loop’s body line 6 and 7. Again it has another level of indentation to indicate that it belongs to the while loop statement.
  • Finally, at the beginning of a new line 9, we call function main() ,which has no indentation.

By using indentation and whitespace to organize the code structure, Python code gain several advantages:

  • You never miss the beginning or ending code of a block like in other programming languages such as C/C++ or Java .
  • Coding style is mostly uniform. If you have to maintain another developer’s code, his code looks as pretty as yours.
  • Code structure is much more readable and clear.


Python comments begin with a pound or hash (#) symbol. All the following characters are ignored by Python interpreter. For more information on how to use comments properly, check it out Python comments tutorial

Continuation of statements

In Python, statements are separated by a new line character. Each statement should be on one line. However, a long statement can span multiple lines using backslash (\) character. You have to place the backslash character before the new line character to continue the current statement onto the next line.

The following example demonstrates how to use backslash (\) character to continue a statement in the second line:

Multiple statements on single line (;)

In Python, each statement always ends with a new line character. However it is possible to put multiple statements on the same line. Each statement is separated by semicolon (;). It is not good practice to put multiple statements on the same line because the code will become less readable.

Take a look at the following example:


Identifiers are names used to identify variables, functions, modules, classes and other objects. The identifier in Python must comply with the following rules:

  • The first character of an identifier must be a letter or underscore ( _ ) and other following characters can be alphanumeric or underscore.
  • Identifier is case-sensitive, meanings that count and Count are different identifiers in Python.

Python Keywords

The following identifiers are keywords or reserved words in Python language, meanings that they cannot be used as ordinary identifiers.

Python is a growing and evolving language therefore its keywords will keep increasing and changing. Python provides a special module for listing its keywords called keyword

The following code demonstrates some of the keyword module functionality:

String literals

Python uses single quote (‘), double quotes (“) and even triple single quotes (”’) and triple double quotes (“””) to denote a string literal. The string literal must begins and ends with the same type of quotes.

In this tutorial, we have introduced you some basic Python syntax so that you can get familiar with the Python language before you can dive into more complex features of Python.

  • Was this tutorial helpful ?
  • YesNo