Python Syntax

This lesson will teach you the fundamentals of Python syntax so you can get started quickly with the language.

Whitespace and indentation

If you’ve worked with other programming languages like Java, C#, or C/C++, you’re aware that semicolons (;) are used to separate statements.

Python, on the other hand, constructs the code structure using whitespace and indentation.

A piece of Python code is shown below:

def main():
    i = 1
    max = 10
    while (i < max):
        i = i + 1


You don’t need to know what the code means right now. Instead, concentrate on the code’s structure.

There is no semicolon at the end of each line to finish the statement. Indentation is also used to format the code.

Python code obtains the following benefits by utilising indentation and whitespace to structure it:

  • First, unlike other programming languages like Java or C#, you’ll never miss the beginning or conclusion of a block’s code.
  • Second, the code style is largely consistent. If you have to maintain the code of another developer, it will look just like yours.
  • Third, in comparison to other programming languages, the code is more understandable and clear.


Because they explain why a piece of code was produced, comments are just as significant as the code.

The comments are ignored by the Python interpreter when it runs the code.

A single line comment in Python starts with the hash (#) symbol and then the comment. For instance:

# Single line comment demo

Multiline Statements

A newline character is used to separate statements in Python. Each statement is placed on a single line.

Using the backslash () character, however, a long sentence can span numerous lines.

The following example shows how to continue a sentence in the second line with the backslash () character:

a= True
b= True
if (a == True) and \
   (b == True):


In Python, identifiers are names that are used to identify variables, functions, modules, classes, and other objects.

An identifier’s name must be a letter or underscore ( ). The characters that follow can be alphanumeric or underscore.

Case matters when using Python identifiers. The counter and Counter, for example, are two distinct identities.

Furthermore, Python keywords cannot be used to name identifiers.


In Python, some words have unique meanings. They’re known as keywords.

Python’s keywords are listed below:

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

Python is a language that is constantly evolving. As a result, its keywords will continue to grow and change.

Python has a special module called keyword for listing its keywords.

The following code is used to retrieve the current keyword list:

import keyword


String literals

To denote a string literal, Python uses single quotes (‘), double quotes (“), triple single quotes (“‘), and triple-double quotes (“””).

The string literal must be surrounded by the same type of quotes. For example, if you start a string literal with a single quote, you must end it with the same single quote.

The following are some string literal examples:

s = 'welcome to'
s = "welcome to"
s = ''' welcome to
        multiple lines '''