Python Fundamentals

 Python Fundamentals 


Generally python fundamentals involves the building blocks of python. The basic building blocks of Python are the essential components that form the foundation of writing any Python program. Understanding these is key to learning and mastering Python. They are 

  • Comments
  • Identifiers
  • Statements
  • Keywords
  • Variables
  • Constants
  • Excape Characters 
  • Data Types
  • Operators
  • Control Flow
  • Functions
  • Data Structures
  • Modules
  • I/O
  • Exception

Whitespaces and Indentaion : 

To write python code in a precise correct and readable way, whitespaces and indentation are very much important. We use indentation and whitespace to organize the python code. 

Python code gains the following advantages:

  • First, you’ll never miss the beginning or ending code of a block like in other programming languages such as Java or C#.
  • Second, the coding style is essentially uniform. If you have to maintain another developer’s code, that code looks the same as yours.
  • Third, the code is more readable and clear in comparison with other programming languages.

As a convention every line should start from same starting line. If we need a block / group of code like if, for or def (function), then we must use one or two whitespaces form the starting line of thses codes but in a same starting line.

Example :


a = 10
b = 20
 c = a + b
print(c)


In the above example, line 1 and 2 are started from same starting line. But line 3 and 4 are started with a whitespace but from  a same starting line.

Output

 c = a + b
    ^
IndentationError: unexpected indent


Unless we have a block of code, the starting line of each line shoould be same.  


Example


a = 10
b = 20
if a>b:
  print("a is greater than b")
else:
  print("b is greater than a")

The above program will run successfully without having any error. Because line 1, 2, 3 and 5 are started from smae starting line where as line no. 4 and 6 are stareted with a whitespace because these two are within a block i.e if block or else block.

Output :

b is greater than a



Previous Topic




Comments