Python Syntax | Basic Introduction with Examples

Every programming language starts with Syntax. Python syntax is much easier than other languages. The syntax of the Python programming language is the set of instruction that defines, how a Python program will be written and Execute.

Basic definition: A syntax simply means “dear interpreter/compiler, please read the next line as if it was on this one”: Lexical analysis

Python Syntax Basic Introduction with Examples 3

In this tutorial, you will learn basic of Basic Syntax used in Python with examples.

Like an any Human Language we need to learn about grammar and set of rule it. The same for other programming languages, if you want to learn it then you must know the syntax of it.

Python Syntax – Print

Here is the syntax for print in the console. You can see it’s only using “Print()” syntax

print("Hello world")
Output

Hello world

Python Indentations

Where in other programming languages the indentation in code is for readability only, in Python the indentation is very important.

Python uses indentation to indicate a block of code.

if 7 > 2:
    print("Seven is greater than two!")
Output

Seven is greater than two!

Python will give you an error if you skip the indentation:

if 7 > 2:
print("Seven is greater than two!")
Output

File “/Users/user/PycharmProjects/Hello/Hello.py”, line 2
print(“Seven is greater than two!”)
^
IndentationError: expected an indented block

Comments Syntax in python

Every programming language has comment syntax. it’s required to you and other developers to understand the code.

Python comments start with a #, and Python will render the rest of the line as a comment:

# Here is output
print("Hello world ")

Docstrings Syntax in python

Python also has extended documentation capability, called docstrings.

"""This is a 
example of docstring."""
print("Hello, World!")

This is a basic Python Syntax. Every programing language has own syntax, you have to always learn a syntax first.

Do comment if you have any doubt and suggestion.

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

The Basic Python Syntax explanation in tutorial base on Python 3, there is a chance other versions don’t have the same syntax.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.