Python Syntax | The Best Tutorial to Learn Python Syntax 8

1. Python Syntax: Objective

Previously, we saw what is Python and how to set up a Python environment on your computer. Code in any language must follow a set of rules. Today, we will learn about the Python syntax. In doing so, you will see what is Python Syntax and how it is different to Java and C++. After this lesson, you will be able to identify and debug beginner Python syntax.

python syntax

Python Syntax

Read: Python Built-In Functions with Syntax and Examples

2. Introduction to Python Syntax

Let us see various basic python syntax that is used while doing programming in Python.

3. Line Structure

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

The following line causes an error.

How are you?”)

If you face any doubt anywhere in the Python Syntax Tutorial, Please Comment.

Read: Python Function Arguments with Types, Syntax and Examples

4. Multiline Statements

This one is an important Python Syntax

We saw that Python does not mandate semicolons. A new line means a new statement. But sometimes, you may want to split a statement over two or more lines. It may be to aid readability. You can do so in the following ways.

a. Use a backward slash

how are you?”)

You can also use it to distribute a statement without a string across lines.


b. Put the string in triple quotes

how are you?”””)

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


#This causes an error

Read: Python Functions with Syntax and Examples


Python Syntax ‘Comments’ let you store tags at the right places in the code. You can use them to explain complex sections of code. The interpreter ignores comments. Declare a comment using an octothorpe (#).

#This is a comment

Python does not support general multiline comments like Java or C++.

6. Docstrings

A docstring is a documentation string. Like a comment, this Python Syntax is used to explain code. But unlike comments, they are more specific. Also, they are retained at runtime. This way, the programmer can inspect them at runtime. Delimit a docstring using three double quotes. You may put it as a function’s first line to describe it.

def func():

This function prints out a greeting


Any query yet in Python Syntax Tutorial? Please Comment.

Read: Python Namespace and Variable Scope – Local and Global Variables

7. Indentation

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.

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

#This causes a syntax error: “unindent does not match any outer indentation level”

8. 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.


Read: Python Decision Making Statements with Syntax and Examples

9. 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’);

This string is delimited by double quotes.

print(“We need a ‘chaperone’”);

Notice how we used single quotes around the word chaperone in the string? If we used double quotes everywhere, the string would terminate prematurely.

print(“We need a “chaperone””);

10. Blank Lines

If you leave a line with just whitespace, the interpreter will ignore it.

Read: Bitwise Operator in Python with Syntax and Example

Let us see next python syntax of Identifiers.

11. Identifiers

An identifier is a name of a program element, and it is user-defined. This Python Syntax uniquely identifies the element. There are some rules to follow while choosing an identifier:

1) An identifier may only begin with A-Z, a-z, or an underscore(_).

2) This may be followed by letters, digits, and underscores- zero or more.

3) Python is case-sensitive. Name and name are two different identifiers.

4) A reserved keyword may not be used as an identifier. The following is a list of keywords.


Apart from these rules, there are a few naming conventions that you should follow while using this Python syntax:

1) Use uppercase initials for class names, lowercase for all others.

2) Name a private identifier with a leading underscore ( _username)

3) Name a strongly private identifier with two leading underscores ( __password)

4) Special identifiers by Python end with two leading underscores.

Read: Python Operators with Syntax and Examples

12. Variables

In Python, you don’t define the type of the variable. It is assumed on the basis of the value it holds.


Here, we declared a variable x, and assigned it a value of 10. Then we printed its value. Next, we assigned it the value ‘Hello’, and printed it out. So, we see, a variable can hold any type of value at a later instant. Hence, Python is a dynamically-typed language.

Read: Python Strings with String Functions and String Operations

13. String Formatters

Let us see next python syntax String formatters.

a. % Operator

You can use the % operator to format a string to contain text as well as values of identifiers. Use %s where you want a value to appear. After the string, put a % operator, and mention the identifiers in parameters.

x=10;  printer=”HP”
print(“I just printed %s pages to the printer %s” % (x, printer))

b. format method

The format method allows you to format a string in a similar way. At the places you want to put values, put 0,1,2,.. in curly braces. Call the format method on the string and mention the identifiers in parameters.

print(“I just printed {0} pages to the printer {1}”.format(x, printer))

You can also use the method to print out identifiers that match certain values.

print(“I  just printed {x} pages to the printer {printer}”.format(x=7, printer=’HP’))

c. f-strings

If you use an f-string, you just need to mention the identifiers in curly braces. Also, write ‘f’ right before the string, but outside the quotes used.

print(f”I just printed {x} pages to the printer {printer}”)

This was all about the Python Syntax tutorial.

Read: Python Comment, Indentation and Statement

14. Python Syntax – Conclusion

In this Python Syntax tutorial, we learned about the basic Python syntax. We learnt about its line structure, multiline statements, comments and docstrings, indentation, and quotations. We also learnt about blank lines, identifiers, variables, multiple statements in one line, and string formatters. In the next lesson, we will look at different variable types.

If you have any query regarding the Python Syntax Tutorial, please drop a comment.

Leave a comment

Your email address will not be published. Required fields are marked *

8 thoughts on “Python Syntax | The Best Tutorial to Learn Python Syntax