Python Syntax



The Python language was designed to be a highly readable language. The syntax of Python is a list of rules which defines how a Python program will be written.


Python Line Structure

A Python program consists of several logical lines which every one of them is terminated by a newline character. A logical line is formed from one or more physical lines.

Python interpreter ignores blank lines that contain only spaces, tabs, comments.

A physical line is a series of characters terminated by an end-of-line sequence.


Multi-line statement

Python marks the end of a statement by a newline character. But you can make a statement extend over multiple lines with the line continuation character \ as follows:

x = line_one + \
      line_two + \
      line_three         

In Python, statements contained within the (), [], or {} brackets do not need to use the line continuation character. For instance, we can implement seasons of years as follows:

seasons = [ 'Spring',  'Summer',
             'Fall',  'Winter' ]

Python Indentation

Python uses indentation to define a block of code, comparing to the other programming languages like Java, C, C++ that use braces {}.

In Python, a code block starts with indentation and ends with the first unindented line. You can use the amount of indentation you like, but it must be consistent throughout that block.

The best practice is to use four whitespaces for indentation. The whitespaces are preferred over tabs.

The following example uses the four whitespaces to define a different block of code:

for x in range (0, 6):
    print(x)
    if x == 4: 
        break

The enforcement of indentation in Python makes the code clean and easy to read. This results in Python programs looking similar and consistent.


Python Variables

Python has no command for declaring a variable. Variables in Python are created when you assign a value to them.

The following example creates two variables, a and b, by assigning value to them:

a = 10
b = 'Hello World!'

You can learn more about variables in the Python Variables chapter.


Python Comments

Comments are important while writing code. Comments describe what your program or your code is doing, making it very easy for others to understand what the code is doing.

Python uses the hash (#) symbol to start writing a comment.

Python interpreter ignores comments. Comments are for programmers to understand code better.

# This is the first comment 
# This is the second comment
print('Hello Wolrd!')

Multi-line comments

Python offers the possibility to have comments that extend up to multiple lines.

The first way to write multi-line comments is to use the hash (#) symbol at the beginning of each line as follows:

# This is a long comment 
# and it extends 
# to multiple lines 

The second way to write multi-line comments is to use the triple quotes, either ``` or """.

The triple quotes are generally used for multi-line strings, but they can be used as a multi-line comment. Unless they are not docstrings, they do not generate any extra code.

"""This is another 
example of 
multi-line comments"""

Docstrings in Python

A docstring is short for documentation string.

Python docstrings are the string that appears right after defining a function, method, class, module.

Triple quotes are used to write docstrings. The following example defines docstrings for the function hello():

def hello():
    """Function that shows Hello World"""
    print("Hello World!");

Note: Always docstrings appear right after the definition of a function, class, or module. This is what separates them from multi-line comments.

The docstrings are associated with the object as their __doc__ attribute.

The following example access the docstrings of the above function:

def hello():
    """Function that shows Hello World"""
    print("Hello World!");

print(hello.__doc__)

The output of the above code:

Function that shows Hello World 


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.