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.
To create variable, Python uses its syntax which is pretty easy and require less code. See the below Example
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.
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:
Or using semicolon to terminate the statement.
Both statements produce the same output to the screen.
Output:
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.
Both print() functions are indented by a tab. See, no curly braces is used.
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.
To avoid this error, use backslash(\). It helps to execute multiline statement without any error. See the below code.
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.
We can import single or multiple modules in the program. See the example below.
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.
Apart of these, there is lot more to discuss. We will include all of them. Till then keep visiting, stay updated.