Python syntax could be primarily challege, especially for someone from another langugage.
Thank for the latest editors we have now aday, because the have each language extension that will help for fixing the syntax.
Let's take a VS Code as example, this editor comes with alot features that shows support for each languages and the Python is not an exception.
The major differences that Python syntax has over other languages are:
- Indentation instead of code block for other languages
- Semi-column is not necessary
- Lack of Data-type
- Column is very essential
We will provide some examples to demonstrate the above syntax.
Execute Python Syntax
When running Python in Command Line or Terminal, we don't face much issues with the syntax. like this:
>>> print("Hello, World!")
Hello, World!
Python Indentation
Indentation is the total number of space provided at the beggining of line of code.
In Python Programming language, Indentation is not a style of writing code, is something very important.
While in other programming Indentation is just a style of writing code for readability.
Indentation in Python is used to indicate the level of code block.
Example:
if 10 > 8:
print("Ten is greater than eight!")
You will run into an Error if you neglet (skip) the Indentation
Example:
if 10 > 8:
print("Ten is greater than eight!")
The rules for Indentation is just flexible and customizable, you as a programmer has to decide. But the actual convention is to used single space or fice space.
Or inshort used single Tab for more understandable.
Example:
if 10 > 8:
print("Ten is greater than eight!")
if 10 > 8:
print("Ten is greater than eight!")
Note: all the above are valid ways of Indentation.
In each block of code, you must just give thesame number of Indentation space. Or you will get an error.
Example:
if 10 > 8:
print("Ten is greater than eight!")
print("Ten is greater than eight!")
The code above will produce an error.
Python Variables
To create a variable in Python, you just have to assign a value to it. itsn't that easy.
a = 10
b = "Hello, World!"
print(a)
print(b)
Note: Python has no keyword for creating variables.
Comments in Python
Like other programming language, Python also support comments.
Comments is a way of explaining some part of your code. Or you want to disable some code from running.
Comment start with a
#symbol, then follow with the statement
Anything start with a
#symbol, Python will treat the entire line as
comment
Example:
# this is a sample comment
print("Hello, World!")