No Curly Braces in Python for Blocks

A new way to define the block by Indentation and Colon



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.

Cross-platform compatibility note: because of the nature of text editors on non-UNIX platforms, it is unwise to use a mixture of spaces and tabs for the indentation in a single source file.

We can also see the types of errors in the improper way of indenting and de-denting.

 def user(n):                       # error: first line indented
for i in range(len(n)): # error: not indented
a= n[:i] + n[i+1:]
b= perm(n[:i] + n[i+1:]) # error: unexpected indent
for x in p:
r.append(l[i:i+1] + x)
return r # error: inconsistent dedent

What if we write multiple lines for the same variable. It seems that it needs indentation then it is no to this rather we can use line continuation character (\). For example:

values = 32 + 43 + 74 + \
81 + 31 + 56 + \
19 + 66 + 47

But this is not a way to write good code. In python, we can do line continuation inside parentheses (), brackets[] or braces {}. To do so we can use these as shown below example:

values = (32 + 43 + 74 + \
81 + 31 + 56 + \
19 + 66 + 47)

We can also write multiple statements in a single line with the help of semicolon. For example:

a = 2; name = "Happy" ; code = 23735

The indentation in while loop

val = 1
while(val<= 5):
print(val)
val= val+ 1
#output:
1
2
3
4
5

Comments

There is no need for indentation for comments in python. They are used to describe whats in the code. Comments are very helpful to retain whats the logic of the code if we come back to code after one month. The comments start with a hash symbol (#), and Python will render the rest of the line as a comment. There are two types of comments.

  1. Single line comment

Python single line comment starts with a hashtag symbol with no white spaces and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the next line and continue the comment. Python’s single-line comments are proved useful for supplying short explanations for variables, function declarations, and expressions. See the following code snippet demonstrating single line comment:

#This is a comment.
print("Hello, World!")
# This is a comment
# Print “Hello World” to console
print("Hello, World!")

2. Multiple line comments

Python multi-line comment is a piece of text enclosed in a delimiter (""") on each end of the comment. Again there should be no white space between delimiter ("""). They are useful when the comment text does not fit into one line; therefore needs to span across lines. Multi-line comments or paragraphs serve as documentation for others reading your code. See the following code snippet demonstrating multi-line comment:

"""
Every coder is curious to know the current
date and time with some code.
Python is a great language to do
data analysis and manipulation.
"""

Docstrings

In python, it comes in PEP 257 with Docstring conventions. This PEP aims to standardize the high-level structure of docstrings, what they should contain, and how to say it in code. A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object. All modules should normally have docstrings, and all functions and classes exported by a module should also have docstrings.

Type of Docstrings

  1. One-line Docstrings

Triple quotes are used even though the string fits on one line. This makes it easy to later expand it. The closing quotes are on the same line as the opening quotes. This looks better for one-liners.

Image for post
One line Docstring

2. Multi-line Docstrings

Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line, followed by a more elaborate description. The docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions.

Image for post

Python indentation makes our code beautiful. It also serves the purpose of grouping the statements into a code block. This results in the habit of writing beautiful code all the time because it’s not a Good-To-Have feature but a Must-Have requirement of the code.

If you are reading this means you read all processes above. Be with me for more fun and knowledge. Reach me through mail: design4led@gmail.com

Read more stories

  1. Latest Add On in Python — July 2020
  2. Image Processing — Color Spaces by Python
  3. Robotic Vision in Fruits Harvesting

Towards Data Science

A Medium publication sharing concepts, ideas, and codes.

Comments

Popular posts from this blog

Easy Text-to-Speech with Python

Flutter for Single-Page Scrollable Websites with Navigator 2.0

Better File Storage in Oracle Cloud