Polygon art logo of the programming language Python.

IndentationError: Unexpected Unindent in Python

Python indentation is a part of the syntax. It’s not just for decoration. You’ll learn what these errors mean and how to solve them: IndentationError: unexpected indent IndentationError: expected an indented block IndentationError: unindent does not match any outer indentation level IndentationError: unexpected unindent So if you want to learn how to solve those errors, then you’re in the right place. Let’s kick things off with error #1! How to Solve IndentationError: unexpected indent in Python Python is a beautiful language. One of the key features of this beauty is the lack of curly braces and other symbols that mark the beginning and end of each block.  Even in C it is considered a good practice to indent, denoting different levels in the code. Compare the same C ++ code with and without indentation. First with the indentation: And the same code without indentation: Both codes will compile and run, but the indented code is a lot easier to read. In the second case, it isn’t clear which parenthesis goes with which.  In Python, parentheses aren’t needed, but indentation is. This is what the C++ program would look like in Python: However, there is a downside to this beauty. If you make a mistake in the indentation, the program will be inconsistent, which will lead to errors when it’s running.  Perhaps, this is a better option than changing the indentation and not getting the error, but changing the meaning of the program.  The error IndentationError: unexpected indent is one that results from wrong indentation. It happens when there are no keywords in front of the indentation. Here’s an example: Python expects a keyword line to come before an indented line. List of keywords followed by an indented line: class: class definition def: function definition for: a loop with a parameter while: a loop with a condition if, elif, else: conditional operator try, except, finally: exception handling with: a context operator

Polygon art logo of the programming language Python.

How to Solve ImportError: Attempted Relative Import With No Known Parent Package (Python)

Here’s everything about ImportError: attempted relative import with not known parent package in Python. You’ll learn: The meaning of the error ImportError: attempted relative import with not known parent package How to solve the error ImportError: attempted relative import with not known parent package Lots more So if you want to understand this error in Python and how to solve it, then you’re in the right place. Let’s get started! How to Solve ImportError: Attempted Relative Import With No Known Parent Package (ModuleNotFoundError: No module named ‘name’) For example, you get the error when running a file inside a package as a script. To get rid of the error ImportError: attempted relative import with no known parent package you have two ways to test package functions: Run a script with the -m switch. Use global import inside the package files that you plan to run as scripts. To get rid of the former version of this error ModuleNotFoundError: No module named ‘name’, the path to the package folder must be in the PATH system variable. You can do this in different ways, for example: Move the package folder to a directory that is already in PATH. Add the folder where the package is located to PATH on your own through the console or system settings. Install the package to the system using the setuptools module. Add the address of the package folder to PATH using the sys and pathlib modules in those package files that you plan to run as a separate script. Let’s dive right in: Understand the Error ImportError: Attempted Relative Import With No Known Parent Package One of the key advantages of the Python language is its infrastructure. You can find a ready-made module for almost any of your tasks.  The meme from xkcd below is a good illustration of this statement: In this comic, one man says that flying is a straightforward task; you just need

Polygon art logo of the programming language Python.

SyntaxError: Invalid Character in Identifier: How to Solve? (Python)

Here’s everything about SyntaxError: invalid character in identifier in Python. You’ll learn: The meaning of the error SyntaxError: invalid character in identifier How to solve the error SyntaxError: invalid character in identifier Lots more So if you want to understand this error in Python and how to solve it, then you’re in the right place. Let’s get started! Understand SyntaxError: Invalid Character in Identifier in Python The error SyntaxError: invalid character in identifier occurs when invalid characters somehow appear in the code. Following is how such a symbol can appear in the code: Copying the code from the site such as stackoverflow.com Copying from a PDF file such as one generated by Latex Typing text in national encoding or not in US English encoding Problematic characters can be arithmetic signs, parentheses, various non-printable characters, quotes, colons, and more. You can find non-printable characters using the repr() function or special text editors like Vim. Also, you can determine the real codes of other characters using the ord() function. However, you should copy the program text through the buffer as little as possible. This habit will not only help to avoid this error but will also improve your skills in programming and typing. In most cases, retyping will be faster than looking for the problematic character in other ways. Let’s dive right in: First, What Is an Identifier in Python? The identifier in Python is any name of an entity, including the name of a function, variable, class, method, and so on.  PEP8 recommends using only ASCII identifiers in the standard library. However, PEP3131 allowed the use of Unicode characters in identifiers to support national alphabets.  The decision is rather controversial, as PEP3131 itself writes about. Also, it recommends not using national alphabets anywhere other than in the authors’ names. Nevertheless, you can use such variable names, and it will not cause errors: Don’t Blindly Copy and Paste Python Code Most often,

Polygon art logo of the programming language Python.

How to Solve ‘Tuple’ Object Does Not Support Item Assignment (Python)

Here’s everything about TypeError: ‘Tuple’ Object Does Not Support Item Assignment in Python. You’ll learn: The specifics of the tuple data type The difference between immutable and mutable data types How to change immutable data types Lots more So if you want to understand this error in Python and how to solve it, then you’re in the right place. Let’s jump right in! Mutable, or Immutable? That Is the Question Data types in Python are mutable or immutable. All data types that are numeric, for example, are immutable.  You can write something like this: And: Have you changed the variable a?  Not really: When you write a = 1, you put the object 1 in memory and told the name a to refer to this literal.  Next, when you write a = a + 1, Python evaluates the expression on the right: Python takes the object referred by a (the 1) and then adds 1 to it.  You get a new object, a 2. This object goes right into the memory and a references instead of object 1.  The value of object 1 has not changed—it would be weird if 1 would out of a sudden a 2, for example, wouldn’t it? So instead of overwriting an object (1), a new object (2) is created and assigned to the variable (a). Mutable Data Types More complex data types in Python are sequences such as:  Strings Tuples Bytes Lists Byte Arrays Sequences contain several values, which can be accessed by index. However, some sequences are mutable (byte arrays, lists), while others are immutable (tuples).  You can create a tuple and access its elements like this: Yet if you try to change one of the elements, you get an error: Notice that the item in the tuple at index 2 is a list. You can change the list without changing the tuple: The object stored in the tuple remains the same, but

Polygon art logo of the programming language Python.

9 Examples of Unexpected Character After Line Continuation Character (Python)

Here’s everything about the Python syntax error unexpected character after line continuation character: This error occurs when the backslash character \ is used incorrectly. So if you want to learn all about this Python error and how to solve it, then you’re in the right place. Keep reading! SyntaxError: Unexpected Character After Line Continuation Character in Python So you got SyntaxError: unexpected character after line continuation character?—don’t be afraid this article is here for your rescue and this error is easy to fix: Syntax errors are usually the easiest to solve because they appear immediately after the program starts and you can see them without thinking about it much. It’s not like some logical rocket science error. However, when you see the error SyntaxError: unexpected character after line continuation character for the first time, you might be confused: What Is a Line Continuation Character in Python? A line continuation character is just a backslash \—place a backlash \ at the end of a line, and it is considered that the line is continued, ignoring subsequent newlines. You can use it for explicit line joining, for example. You find more information about explicit line joining in the official documentation of Python. Another use of the backslash \ is to escape sequences—more about that further below. However, here is an example of explicit line joining: So as you can see the output is: This is a huge line. It is very large, but it needs to be printed on the screen in one line. For this, the backslash character is used. No line breaks. The backslash \ acts like glue and connects the strings to one string even when they are on different lines of code. When to Use a Line Continuation Character in Python? You can break lines of code with the backslash \ for the convenience of code readability and maintainability: The PEP 8 specify a maximum line length of 79 characters—PEP is short