0% found this document useful (0 votes)
2 views2 pages

CODE-BLOCKS in Python

This lesson explains the importance of code blocks in Python, focusing on indentation and suites. Indentation is crucial for defining blocks of code, with each line needing to be uniformly indented. Suites are collections of statements that form a single code block, typically used in compound statements like if, while, def, and class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

CODE-BLOCKS in Python

This lesson explains the importance of code blocks in Python, focusing on indentation and suites. Indentation is crucial for defining blocks of code, with each line needing to be uniformly indented. Suites are collections of statements that form a single code block, typically used in compound statements like if, while, def, and class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

LESSON 4:

Code Blocks
It is very important to understand how to write code blocks in
Python. Let’s look at two key concepts around code blocks.

Indentation
One of the most unique features of Python is its use of indentation to
mark blocks of code.
Each line of code must be indented by the same amount to denote a
block of code in Python. Indentation is required to indicate which
block of code a code or statement belongs to.

Suits
A collection of individual statements that makes a single code block
are called suites in Python.
A header line followed by a suite are required for compound or
complex statements such as if, while, def, and class (we will
understand each of these in details in the later sections).

Examples:

# Correct indentation
# One TAB (or a continuous four white spaces) is the standard
indentation
print("Python is important for Data Science")
print("Pandas is important for Data Science")

# Correct indentation, note that if statement here is an example of


suites
x=1
if x == 1:
print('x has a value of 1')
print('If x = 1; then this if block will execute')
else:
print ('x does NOT have a value of 1')

print ("I don't know who is x, because I am not in if block")

# incorrect indentation, program will generate a syntax error


# due to the space character inserted at the beginning of second
line
print("Python is important for Data Science")
print("Pandas is important for Data Science")

# Incorrect indentation, program will generate a syntax error


# due to the wrong indentation in the else statement
x=0
if x == 2:
print('x has a value of 2')
else:
print('x does NOT have a value of 2') # press TAB before print
statement

You might also like