Python BasicsIt is important to understand the basics of any programming language like syntax, identifiers and how to use them. In this tutorial, we will discuss some of it’s basics.
Identifiers in PythonAn identifier in any programming language is used to represent a variable, keyword, class, function etc. Some of them are reserved by the programming language itself and some of them are created by the user.
In Python, we can create an identifier whose first character starts with letter from a – z or A – Z or (_) underscore followed by mix of alphabets or numbers from 0 to 9 or underscore.
Python does not allow any special characters in the identifiers like %, @ or $ in any case. An identifier in Python is case sensitive – Var and var are two different identifiers.
Some of reserved words or keywords in PythonKeywords are reserved words. Python holds some of the words for it’s own purpose and processing.
In Python, there are no braces to represent a block of code in class, function or flow control. Python uses indentation to maintain the code block, so we need to understand it very carefully otherwise it will generate error if we don’t maintain proper indentation in our code.
Indentation in Python
Look at the below code which will generate error:
if True: print("Inside if block") print("Inside if block") else: print("Inside else block") print("Neither if block nor else block, indentation not maintained") print("Not in any block.")
The above program will generate the following error – “IndentationError: unindent does not match any outer indentation level”.
So if we correct our code with the proper indentation then it will look as below :
if True: print("Inside if block") print("Inside if block") else: print("Inside else block") print("Now inside else block") print("Not in any block.")
Comments in PythonComments are useful for documentation which help developers to understand the purpose of the code. In Python, we can create a comment by simply using hashtag (#) sign. Anything at the end of the line will be treated as comment and interpreter will simply ignore it.
#This is the comment print("Welcome to the python.)