Python Basic Syntax and Rules

Like other programming languages, Python also use programming syntax and rules to write clean and readable code. Syntax are basics rules that are require to follow during coding.

Python syntax are clean, easy to read and understand, and in some cases similar to the C and Java. Lets see the syntax to declare the variable, comment, function, class, indentation etc.

Python Variable Syntax

To create variable, Python uses its syntax which is pretty easy and require less code. See the below Example

# Creating variable
a = 10

Notice, we did not mentioned type of variable during declaration. It is because Python is type inference language that automaticaly infers type of variable based on the assigned value. We will discuss it later in our tutorial.

Optional Semicolon

Semicolon is a symbol which is used to terminate program statement in many languages like C and Java. Python does not require semicolon and even it is optional. You either can use it or leave it completely. See an example below.

What if you write a print statement as:

# No Semicolon

Or using semicolon to terminate the statement.

# Using Semicolon

Both statements produce the same output to the screen.



Indentation : No Curly Braces

Curly braces are used to enclose the code statements into a unit. Many languages use curly braces for function and class declaration but Python does not require to use curly braces. Instead of braces python use tab or spaces to indent code.

Indentation is a style of code writing to structure program. It is used to structure the code of function, loop or blocks. It increase code readability and represents the flow of the program. See an example given below, indented by tabs.

# Indentation
a = 10
if a<20:
    print("a is less than 20")
    print("a is greater than 20")

Both print() functions are indented by a tab. See, no curly braces is used.

Multiline Statements

A statement which is using multiple lines in the program called multiline statement. Python uses a new style of syntax to write multiline statement. It is useful if we want to break a single line statement for readability. Python does not allow to break statement normally by pressing enter. If we do that, we get the error. See an example below.

# Multiline Statement
SyntaxError: EOL while scanning string literal

To avoid this error, use backslash(\). It helps to execute multiline statement without any error. See the below code.

# Multiline Statement

Python Import

import is a keyword in python which is used to link module in the program. We use import to access built-in functions and API. The syntax used by the python to import package is given below.

import module_name

We can import single or multiple modules in the program. See the example below.

# Single Import
import sys
# Multiple Import
import sys,os

Python Function

To define a function in Python, Python provides def keyword. It is required to use def each time while creating a function. See the example below, here we are creating a display function.

# Function syntax
def display(msg):

Apart of these, there is lot more to discuss. We will include all of them. Till then keep visiting, stay updated.