6 Pillars of Python Syntax

Hello Pythonistas, if you have started from here, you might not yet understand Python programs.

For this, you need to understand the Python syntax. It’s like the grammar of Python.

After reading this, you will be able to understand Python codes.

We will cover the major 6 pillars or I can say components of any Python program. So, let’s get started.

Importance of Whitespace and Indentation in Python Syntax

This is the most important pillar of Python. In other programming, languages such as Java, C#, or C/C++ statements are separated by a semicolon (;).

But, Python uses whitespace and indentation for constructing the structure of the code.

The following shows a snippet of Python code:

# define main do_something to print out something
def do_something(): 
    i = 1
    limit = 10
    while (i < limit):
        print(i)
        i = i + 1 
        # call function main main()

What this code does is not important to you right now, just see for the syntax(structure).

At the end of each line, you don’t see any semicolon to terminate the statement. The code uses indentation to format the code.

There are multiple advantages of using indentation and whitespace. See for yourself(same code in C+):

//define do_something function to print out something
#include<stdio.h>
void do_something()
{int i=1, limit = 10;
while (i < limit)
{printf(i);
i=i+1;}}
//Note you can write this whole in a single line too

Python code gains the following advantages:

  • First, you’ll never miss the beginning or ending code of a block like in other programming languages such as Java or C#.
  • Second, the coding style is essentially uniform. If you have to maintain another developer’s code, that code looks the same as yours.
  • Third, the code is more readable and clearer than other programming languages.

Comments

The comments are as important as the code. As they describe why a piece of code was written.

For example:

#This code prints 1 to 10
for i in range(1,11):
    print(i)

Not just Python in any programming language good comments save a lot of time, effort, and money, and make it easy to work in a team.

When the Python interpreter executes the code, it ignores the comments.

In Python, a single-line comment begins with a hash (#) symbol followed by the comment. For example:

# This is a single line comment in Python

And Python also supports other kinds of comments.

Continuation of statements

Newline characters are used to separate two statements.

For a long statement, backslash (\) character can be used.

The following example illustrates how to use the backslash (\) character to continue a statement in the second line:

if (a == True) and (b == False) and \ 
(c == True):
    print("This 👆 is how a long statement can be written")

You will find the backslash feature useful when writing long lists and dictionaries.

Identifiers

Identifiers are the names that you give to your created objects like variables, functions, modules, classes, etc.

This name can begin with a letter(cap or small) or an underscore(_). It can have a number but, cannot start with a number.

Python identifiers are case-sensitive.

For example, the my_var and My_var are different identifiers.

In addition, you cannot use Python keywords for naming identifiers.

Keywords

Keywords are special keywords that provide some special functionality. Like if keyword provides the functionality to evaluate a condition.

Some words have special meanings in Python. They are called keywords.

The following shows the list of keywords in Python:

Falseforwhileifpass
classlambdaandelifbreak
finallytrydelorexcept
returnTrueglobalyieldin
isdefnotassertraise
Nonefromwithelse
continuelocalasimport

Python is a growing and evolving language. So its keywords will keep increasing and changing.

Python provides a special module for listing its keywords called keyword

To find the current keyword list, you use the following code:

import keyword
print(keyword.kwlist)

Remember never to use a keyword as an identifier. In that case, you will be redefining the keyword. And you will not be able to use the existing feature/facility it provides. Also, there can be problems related to ambiguity.

String literals

Python uses single quotes ('), double quotes ("), triple single quotes (''') and triple-double quotes (""") to denote a string literal. (sequence of character)

The string literal needs to be surrounded with the same type of quotes.

For example, if you use a single quote to start a string, you need to use the same single quote to end it.

The following shows some examples of string literals:

s = 'This is a string enclosed in single quotes'
print(s)
s = "This is a string enclosed in double quotes"
print(s)
s = '''This is a string enclosed in single triple quotes'''
print(s)
s = """This is a string enclosed in double triple quotes"""
print(s)

You can use any of these it will be the same string.

Conclusion

  • There are 6 major pillars of Python syntax.
  • Whitespace and indentation are the most important ones. They provide clarity and readability to Python.
  • Comments save time, effort, and money. They are the lines of code that the interpreter ignores.
  • Statements are separated by a new line. Backslash helps to continue when statements are long.
  • Identifiers are names that identify variables, functions, modules, classes, etc. in Python.
  • Keywords are reserved words in Python.
  • Use the single quote, double-quotes, triple-quotes, or triple double-quotes to denote a string.

With all these pillars in mind, you can easily understand any piece of Python code. Also, with a continuous set of practice, you can even start writing them.

Here we come to an end of python’s syntax introduction.

Solve this quiz to test your understanding of Python syntax.

Leave a Reply