Previously, we learned about the history and features of python and how to set up a Python environment on your computer (Windows). Now it’s time to have a hands-on experience on the syntax of python. Let us see how python is used while doing programming in Python. While doing so, so, you will see what is Python Syntax and how it is different to Java and C++ or any other programming language for that matter.

Python Line Structure

A Python program comprises logical lines. A NEWLINE token follows each of those. The interpreter ignores blank lines.

The following line causes an error.


Python Multiline Statements

Even if Python doesn’t accept newlines in its syntax, still there are going to be times when you might want to move the code to the next line and you can do so by adding backslash just before breaking the line.


You can also use it to distribute a statement without a string across lines, yes there are going to be statements which may not need strings and that’s okay.


String in triple quotes

You can break the line in python by adding triple quotes in the beginning and at the end of the statement.

       how are you?""")

However, you can’t use backslashes inside a docstring for statements that aren’t a string. This causes an error.


Comments in Python

Single-line comments are created simply by beginning a line with the hash (#) character, and they are automatically terminated by the end of line.

#This is a comment

Docstrings in Python

Comments that span multiple lines – used to explain things in more detail – are created by adding a delimiter (“””) on each end of the comment.

def func():
    This function prints out a greeting
    And says Happy Holi


Indentation in Python

Since Python doesn’t use curly braces to delimit blocks of code, this Python Syntax is mandatory. You can indent code under a function, loop, or class.

if 2>1:
      print("2 is the bigger person");
      print("But 1 is worthy too");

You can indent using a number of tabs or spaces, or a combination of those. But remember, indent statements under one block of code with the same amount of tabs and spaces. #This causes a syntax error: “unindent does not match any outer indentation level”

if 2>1:
     print("2 is the bigger person");
   print("But 1 is worthy too");

Python Multiple Statements in One Line

You can also fit in more than one statement on one line. Do this by separating them with a semicolon. But you’d only want to do so if it supplements readability.


Python Quotations

Python supports the single quote and the double quote for string literals. But if you begin a string with a single quote, you must end it with a single quote. The same goes for double quotes.

The following string is delimited by single quotes.

print('We need a chaperone');

Python Syntax – Conclusion

In this Python Syntax tutorial, we learned about the basic Python syntax. We learned about its line structure, multiline statements, comments and docstrings, indentation, and quotations.