Hi friends! In this post, we will be discussing about the “IndentationError: expected an indented block” in Python and how to easily resolve it, as well as, how to resolve the “IndentationError: unexpected indent” error.
What is Indentation in Python?
Generally, in programming, indentation is when you leave empty spaces in the beginning of a code line.
In many programming languages, indentation in code only contributes to a more readable code.
However, in Python, indentation is very important since it actually indicates a block of code.
Example of Code Indentation in Python
Now let’s see some examples of how indentation affects a Python program’s behaviour.
Below, we will see some code examples in Python, that include an if…else statement.
Code Indentation Example 1 – Gives an Error
Normally, in the below code, we should have used indentation for the if…else code blocks, but we didn’t, in order to replicate the error.
So, let’s see the code:
# Indentation Example in Python # Variable x=10 # if...else statement if x > 5: print ("\n x is greater than 5") else: print ("\n x is less than 5")
If we run the above code, we will get an error message similar to the below:
print ("\n x is greater than 5") ^ IndentationError: expected an indented block
The reason we get the above error message, is the lack of indentation in the code, that is the if…else code blocks are not correctly formed.
As you can see, there’s not indentation in the two “print” code lines.
Code Indentation Example 2: Works OK
Now let’s try to fix our Python code, based on the above observations, that is inserting a space in the two “print” lines.
# Indentation Example in Python # Variable x=10 # if...else statement if x > 5: print ("\n x is greater than 5") else: print ("\n x is less than 5")
As you can see in the above code example, we have added an empty space in the two code blocks of the if…else statement (lines 8 and 10).
Therefore, now, if we execute the above code we get the output:
x is greater than 5
Where is Code Indentation Required in Python?
Code indentation is required, every time you need to write a code block in Python.
Some examples include:
- In if…else statements
- In exception handling code constructs
- When defining methods
- In “While” loops
- …
Important Consideration about Code Indentation in Python
You need to use the same number of spaces in the same block of code.
In a different case, Python will give another indentation-related error, that is: IndentationError: unexpected indent
Let’s see a relevant example.
Python code example with correct indentation – Runs OK:
# Indentation Example in Python # Variable x=10 # if...else statement if x > 5: print ("\n x is greater than 5") print ("\n the value of x is: ",x) else: print ("\n x is less than 5") print ("\n the value of x is: ",x)
Python code example with wrong indentation – Gives an Error:
# Indentation Example in Python # Variable x=10 # if...else statement if x > 5: print ("\n x is greater than 5") print ("\n the value of x is: ",x) else: print ("\n x is less than 5") print ("\n the value of x is: ",x)
Learn more about SQL Server Data Access from Python – Enroll to our Course!
Enroll to our online course “Working with Python on Windows and SQL Server Databases” with an exclusive discount, and get started with Python data access programming for SQL Server databases, fast and easy!
Featured Online Courses:
- Working with Python on Windows and SQL Server Databases
- SQL Server 2022: What’s New – New and Enhanced Features
- Introduction to Azure Database for MySQL
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- Data Management for Beginners – Main Principles
- A Guide on How to Start and Monetize a Successful Blog
Read Also:
- Python Data Access Fundamentals
- How to Connect to SQL Server Databases from a Python Program
- How to Run the SQL Server BULK INSERT Command from Within a Python Program
- How to Resolve: [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)
- Useful Python Programming Tips
- Main Data Structures in Python
- How to Read a Text File in Python – Live Demo
- Working with Python on Windows and SQL Server Databases (Course Preview)
- How to Write to a Text File from a C++ Program
- How to Establish a Simple Connection from a C# Program to SQL Server
- The timeout period elapsed prior to obtaining a connection from the pool
- Closing a C# Application (including hidden forms)
- Changing the startup form in a C# project
- Using the C# SqlParameter Object for Writing More Secure Code
- Cannot implicitly convert type ‘string’ to ‘System.Windows.Forms.DataGridViewTextBoxColumn
- Missing parentheses in call to ‘print’. did you mean print(…) – How to Resolve in Python
Check our online courses!
Check our eBooks!
Subscribe to our YouTube channel!
Subscribe to our newsletter and stay up to date!
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.