How to write multiple statements in Python

Many programming languages like C, C++, C#, Java use braces { } to mark a block of code or multiple statements. Python does it via indentation instead of braces. In this tutorial I will show you how to write multiple statements in Python.

A code block which represents the body of a function or a loop begins with the indentation and ends with the first unindented line.

Python style guidelines (PEP 8) states that you should keep indent size of four. On the other hand, Google has its unique style guideline which limits indenting up to two spaces. However, I recommend you to follow the PEP8 which means four spaces.

In most of the programming languages we follow indentation for better formatting of coding but those don’t enforce to have it. However, in Python, it is mandatory to obey the indentation rules. Typically, we indent each line by four spaces (or by the same amount) in a block of code.

Example Code for Indentation:

flag=1
input=12
for i in range(2,input,1):
    if input%i == 0:
        flag=0
        break
if flag==1:
    print(input, " is a prime number")
else:
    print(input, " is not a prime number")

In this tutorial, I have shown how to write multiple statements in Python. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page http://www.facebook.com/freetechtrainer and stay connected.

Add a Comment