Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
JSON Parsing Errors in Python
Next article icon

Indentation Error in Python

Last Updated : 13 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will explore the Indentation Error in Python. In programming, we often encounter errors. Indentation Error is one of the most common errors in Python. It can make our code difficult to understand, and difficult to debug. Python is often called a beautiful language in the programming world because we are restricted to code in a formatted manner otherwise it shows an Indentation error. Here we will discuss, the cause of Indentation error and the fixation of it.

What is an Indentation error?

An error is a mistake or issue that prevents the computer program from being run perfectly, Indentation error is one of those. Indentation error occurs in the compilation phase. An Indentation error is a compile-time error that occurs when tabs or spaces in a code do not follow expected patterns. This is typically a syntax error.

Indentation Error is a very common error in Python. Because Python is an interpreted language, its interpreter reads the code line by line. In Python coding, we have to write code that is appropriately formatted with perfect use of gaps to make the code Executable. This perfect use of gaps is termed as indentation. If the user does not write code with proper indentation, it generates an indentation error.

Cause of Indentation error in Python

Indentation error occurs when the number of spaces at the beginning of a block is not equal to the number of spaces assigned at the end. This is the root cause of the Indentation error in Python.

The following are the reasons of the Indentation error in Python code:

  • Misplacing gaps and spaces.
  • Using tabs and spaces during coding.
  • Failing to properly indent compound statements like those used in for loops, while loops, and if statements.
  • Placing some indent wrong.

How to fix Python Indentation Error

To fix indentation errors in Python you have to observe and analyze the code and accurately put the ident, by which it will be able to define the correct scope of various loops. 

  • Follow the correct sequence of code.
  • Use a perfect IDE(Pycharm)
  • Analyze the code and then put spaces as it should define the scopes of loops correctly.
  • Do not use spaces and tabs together stay in one.

Example: You can notice the gaps which is provided at the correct places. This gap makes code properly readable, beautiful and easy to understand. Indentation replaces the curly braces {} in writing code. this indentation describes the scope of a block. If you do not use proper indentation the compiler will return Indentation error. 

Python3
def check_number(a): if a > 2: if a < 7: return "Number is between 2 and 7" return "Number is greater than 2" return "Number is out of the range of 2 and 7"  a = 5 result = check_number(a) print(result) 

Output

Indentation error.

Fix Python Indentation Error

Python3
def check_number(a):     if a > 2:         if a < 7:             return "Number is between 2 and 7"         return "Number is greater than 2"     return "Number is out of the range of 2 and 7"  a = 5 result = check_number(a) print(result) 

Output:

Number is between 2 and 7 

Next Article
JSON Parsing Errors in Python

A

arivashiscqxq
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

  • Indentation in Python
    In Python, indentation is used to define blocks of code. It tells the Python interpreter that a group of statements belongs to a specific block. All statements with the same level of indentation are considered part of the same block. Indentation is achieved using whitespace (spaces or tabs) at the b
    2 min read
  • NZEC error in Python
    While coding on various competitive sites, many people must have encountered NZEC errors. NZEC (non-zero exit code), as the name suggests, occurs when your code fails to return 0. When a code returns 0, it means it is successfully executed otherwise, it will return some other number depending on the
    3 min read
  • JSON Parsing Errors in Python
    JSON is a widely used format for exchanging data between systems and applications. Python provides built-in support for working with JSON data through its JSON module. However, JSON parsing errors can occur due to various reasons such as incorrect formatting, missing data, or data type mismatches. T
    6 min read
  • Floating point error in Python
    Python, a widely used programming language, excels in numerical computing tasks, yet it is not immune to the challenges posed by floating-point arithmetic. Floating-point numbers in Python are approximations of real numbers, leading to rounding errors, loss of precision, and cancellations that can t
    8 min read
  • Handle Memory Error in Python
    One common issue that developers may encounter, especially when working with loops, is a memory error. In this article, we will explore what a memory error is, delve into three common reasons behind memory errors in Python for loops, and discuss approaches to solve them. What is a Memory Error?A mem
    3 min read
  • Statement, Indentation and Comment in Python
    Here, we will discuss Statements in Python, Indentation in Python, and Comments in Python. We will also discuss different rules and examples for Python Statement, Python Indentation, Python Comment, and the Difference Between 'Docstrings' and 'Multi-line Comments. What is Statement in Python A Pytho
    7 min read
  • Handling EOFError Exception in Python
    In Python, an EOFError is raised when one of the built-in functions, such as input() or raw_input() reaches the end-of-file (EOF) condition without reading any data. This commonly occurs in online IDEs or when reading from a file where there is no more data left to read. Example: [GFGTABS] Python n
    4 min read
  • Python IMDbPY - Error Handling
    In this article we will see how we can handle errors related to IMDb module of Python, error like invalid search or data base error network issues that are related to IMDbPY can be caught by checking for the imdb.IMDbErrorexceptionIn order to handle error we have to import the following   from imdb
    2 min read
  • Errors and Exceptions in Python
    Errors are problems in a program that causes the program to stop its execution. On the other hand, exceptions are raised when some internal events change the program's normal flow. Syntax Errors in PythonSyntax error occurs when the code doesn't follow Python's rules, like using incorrect grammar in
    3 min read
  • numpy.index() in Python
    numpy.core.defchararray.index(arr, substring, start=0, end=None): Finds the lowest index of the sub-string in the specified range But if substring is not found, it raises ValueError. Parameters: arr : array-like or string to be searched. substring : substring to search for. start, end : [int, option
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences