A new way to define the block by Indentation and Colon

Image for post

Photo by Connor Pope on Unsplash

The meaning of both curly braces ( {} ) and colon ( : ) to define a block of a group of statements called a suite. Curly braces use by most of the languages like C, C++, Java and etc. Python is famous for its simple user-friendly syntax. Python is the programming language that uses white space as an indentation in their codes for functions, for loops, if-else and etc. Different people use a different length of spaces before a statement so, its a little bit mixed way for writing the codes without maintaining the same length of spaces for all block statements. Let’s see what is this indentation and colon in python programming language.

Image for post

No spaces in normal statements

The above photos show no indentation spaces for normal statements. On the other side, the statements come in block or suite need to be indented to keep them as a suite that belongs to one particular functions or loops. For beginners, it is very much confusing to them because they used to for curly braces in their blocks. Sometimes, people think why python needs to tell me how to write my code, but we should not argue on the syntax. It’s a very technical way of coding. The white spaces given for indentation can be 1 or other numbers, but the white spaces given to one line should be the same throughout the block.

Image for post

Indentation comes in Block statements

What if we give one white space indentation. Will it give the error?. The answer is NO. If we maintain the same indentation for all the statements belongs to one suite it will execute with no error.

Image for post

With one indentation in block

#For Loop 
n = int(input("Enter the Number:- "))
for i in range(1, n+1): 
 num=1
 for j in range(1,i+1):        
  print(num,end=" ")
  num=num+1
 print("\r")
#output:
Enter the Number:- 3
1 
1 2 
1 2 3

As compare to other languages the ending of the block in python should be carefully defined with proper indentation, as in other languages the curly braces show the concrete block-ending character and have a low chance of getting any error. If it is confusing to press the space-bar many times then press the tab button once and it has done. One tab equals to 4 spaces.

#python #programming #data-science #analytics #python3

No Curly Braces in Python for Blocks
2.05 GEEK