free stats

Python Syntax

Last Update : 27 Jun, 2022 Python, Programming

This is one of the Python fundamentals tutorials. In this tutorial, you will learn about the basic Python syntax. Also, you can get a good start with the Python language quickly.


Python Comments

Comments describe why a piece of code was written. Therefore, comments are a very important thing to any programmer.

When a Python code is executed by a Python interpreter, it ignores the comments.

There are many ways to add comments in the Python programming language.

In Python, a single-line comment started with a hash (#) symbol. After the # symbol, you can write the comment text. An example of a single-line comment is as follows.

# This is a Python single line comment

Also, there are other kinds of comments also supported in Python programming. To learn more about comments, follow the Python Comments tutorial.


Indentation and Whitespace

If you are familiar with other programming language like Java, C#, or C/C++, you know that semicolons (;) is used to separate the statements in these programming languages.

However, Python uses Indentation and Whitespace to make the code structure.

The following shows a snippet of Python code:

# define checkNumber function to print out long number
def checkNumber(num1, num2):
    if(num1 > num2):

# call checkNumber function
checkNumber(10, 10)

Here, The meaning of the above code is not important. Consider the code structure instead.

At the end of each code line, We do not use any semicolons (;) to terminate the statement in Python. Also, Python uses indentation to format the code.

Python code gets the following advantages by using whitespace and indentation to organize the code.

  1. The code is more clear and readable compared with other programming languages.
  2. You will never miss the beginning or ending code of a block.
  3. The coding style is essentially uniform.


Python Identifiers

In Python, Identifiers are the names used to identify variables, functions, modules, classes, and other python objects. Also, you cannot use Python reserved keywords for naming identifiers.

Identifiers in Python are case-sensitive. For example, numbers and Numbers are different identifiers.

Python identifiers do not allow punctuation characters such as @, $, and %. The name of an identifier needs to be a letter (A to Z or a to z) or underscore (_).


Python Keywords

In Python programming, Some words have special meanings. These words are called Python keywords.

The following shows the list of keywords in Python:

False      class      finally    is         return
None       continue   for        lambda     try
True       def        from       nonlocal   while
and        del        global     not        with
as         elif       if         or         yield
assert     else       import     pass
break      except     in         raise

When growing and evolving the Python programming language, Python keywords also will be increasing and changing.

There is a special module in Python to list all keywords called keyword.

To find the keyword list of the current version, you can use the following code

import keyword



Python String Literals

The string literal of Python programming language needs to be surrounded with the same type of quotes. Python uses single quotes ('), triple single quotes ('''), double quotes (") and triple-double quotes (""") to indicate a string literal.

The following examples show the string literals of Python :

a = 'This is a string using single quotes'

b = '''This is a string using 
       triple single quotes'''

c = "This is a string using double quotes"

d = """This is a string using
       triple-double quotes"""


Continuation of Python Statements

Basically, Python places each statement on one line. A long Python statement can be split into multiple lines by using the backslash (\) character.

The following example shows the usage of the backslash (\) character to continue a Python statement into the second line:

if (var1 == True) or (var2 == False) or \
   (var3 == True):
    print("Continuation of Python Statements")



This post discusses basic usage of Python syntaxes such as Python comments, Indentation and Whitespace, Python identifiers, Python keywords, Python string literals, and Continuation of Python statements.

You can get a good start to being a Python developer by following this post.

You found this tutorial / article valuable? Need to show your appreciation? Here are some options:

01. Spread the word! Use following buttons to share this tutorial / article link on your favorite social media sites.

02. Follow us on Twitter, GitHub ,and Facebook.