Python - Things to know before proceed - Syntax ( Indentation, Comments ) Tutorial
Python syntax can be executed directly in
- Command-line prompt
- Python IDLE
- Python Script mode
For command prompt
You have to invoke the interpreter without passing the script file, by simply typing python in the prompt, it will open the python command prompt-
After that type –
print(“Hello Fresherbell”)
to print “Hello Fresherbell”
For Python IDLE
You can directly use it, by opening Python IDLE from the start menu
Python Script Mode
You have to invoke the interpreter bypassing the script file.
To create a script file simply create a new file with an extension .py
Eg- create a file on the desktop with the name 1stprogram.py, having a simple script like- print("Hello Fresherbell") in it.
We assume you have a python interpreter set in the Path variable. Now, try to run the following command
i.e
C:\Users\lenovo>python Desktop/1stprogram.py
Python indentation
Most programming languages like C, C++, and Java use braces { } to define blocks of code. However, Python uses indentation to define the block of code.
Indentation is used for code readability. Indentation is very important in python.
Indentation is used to indicate a block of code.
Incorrect indentation will give the below IndentationError-
For indentation, you have to create the same space (tabs) in the same block of code. It should be simple & consistent throughout the block.
Below is a good example of using for under for loop
Output-
Python Comments
Comment in Python is used to give messages in code. It starts with # and then the python will render the rest line of the code.
A comment is used in code for a better understanding of large code. It will not affect the main code.
Single Line comment ( using # )
Output-
Multi-Line Comment ( using """ or ''' )
Output-
Expression and Statement
An expression is a combination of values, variables, and operators. the expression will always return some value.
For example:-
5 + 12
A statement is a unit of code that a python interpreter can execute. the statement will produce the output if there is one example print statement. Otherwise, the assignment statement is another example, which does not produce output.
For example:-
a = 10 #does not produce any output
print(a) # produce the output