Python – Basics

Python - basics

Python Basics

It 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.


Python - basics



Identifiers in Python

An 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 Python

Keywords are reserved words. Python holds some of the words for it’s own purpose and processing. 

False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass  
break except in raise  


Indentation in Python

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.

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 Python

Comments 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.)


How to create multiple statements in a single line

Sometimes it is required to create multiple statements in a single line just to create a short code. Python allows us to do so by (;) semicolon sign. Do not use semicolon at the end of the statement.


Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *