Home >>Python Tutorial >Learn Python Syntax

Learn Python Syntax

Python Syntax Exercise

In this part of the Python tutorial, you will get to learn Python syntax. As we have mentioned before, in the getting started section of this Python tutorial we have learned that the entire Python Syntax can be written in the command line directly to execute any Python file. For example

>>> print (“Hello, World!”)
Hello, World!

You as a Python developer can also choose to execute a Python program by directly creating a Python file on your server. This python file is created with the help of a .py extension. This file with the .py extension can then be run in the command line in the below-mentioned way.

C: \Users\Your Name>python myfile.py

 

The Python Indentations

It is important for any learner of this programming language to remember that unlike many other programming languages where the indentations are only for the readability purposes, in Python the indentations actually plays a rather important role.

This important role is that Python uses the indentations to indicate any particular block of codes. The below-mentioned example can be treated as a sample Python script.

if 7 > 2:

print (“Seven is greater than two!”)

And now, if you skip the indentations then Python will give you an error

if 7 > 2:

print (“Seven is greater than two!”)

 

The Comments

For the purpose of in-code documentation Python further has a type of commenting capability within itself. And if you wish to render any particular sentence as a comment then you just need to start that particular sentence with a #.

This can be treated as a sample Python script. For example

>#This is a comment.

print (“Hello, World!”)

 

The Docstrings

The entire Python programming language also comes with an extended documentation capability. And those extended documentation capabilities are known as docstrings. The docstrings can be of a single line or of multiple lines.

To use this function of docstring in Python you just need to place triple quotes at the beginning and at the ending of the sentence that you want to serve as a docstring.

For example

“““ This is a

multiline docstring.”””

print (“Hello, World!”)

With this, we round up our tutorial for Python programming Syntax.


No Sidebar ads