Polygon art logo of the programming language Python.

.T in NumPy: Meaning?

Here’s what the .T in NumPy means: .T is the returned transposed array with its axes permuted that is used in NumPy. It is the same as calling self.transpose(). When you use .T, you must have at least 2 arrays to work with. Having only 1 array is ineffective. This is a standard matrix transpose for a 2-D array. So if you want to learn all about the meaning of .T in NumPy in Python, then this article is for you. Let’s jump right into it! How Is .Transpose() Used In NumPy? When using .transpose() in NumPy, you’re essentially changing how you look at the array. Remember it needs to be a 2D array or bigger in order for transpose to work. Once called, transpose() will reverse or permute the array on its axes. Take a look at the image below. You have 2 stacks of blocks from 0-7 in front of you. Once you have called transpose(), the blocks have changed! No longer are they standing up but laying down on their side! You still have all 8 blocks(0-7) but their position has changed. It still reads 0-7 in order. Since multidimensional arrays in NumPy are fixed-sized, even transposed, the ndarray has the same number of items. All I did was change the group sizes. Instead of now having 4 groups of 2, I now have 2 groups of 4. If I didn’t have a 2D array, it would return the only ndarray available with no changes. The ndim or the number of array dimensions must be >= 2, else nothing happens. How Is a Transposed Array in NumPy Reshaped? (2 Methods) As you know with Python and so for NumPy, you can stack methods on top of one another to change the outcome of the output. In NumPy, you have 2 methods called .shape() and .reshape() respectively. Not to confuse the 2 of them together. You can use

Polygon art logo of the programming language Python.

Converting List to JSON Array in Python: How To?

This is how to convert a list to a JSON array in Python. To convert a list to a JSON array is called serialization. Learn how to serialize and deserialize a list with this in-depth article. Let’s get started! Learn How to Convert a List to JSON Array in Python You learn the structure of JSON objects and why they’re needed. Then you’ll learn how to convert lists to JSON arrays. This is called serialization.  Next we’ll convert JSON arrays to Python lists. This is called deserialization.  In addition, you’ll figure out how to combine two separate lists with field names and values into a single JSON object. So first things first: What’s JSON?  According to json.org, JSON is a lightweight data exchange format. It’s easy for humans to read and write.  Machines can easily interpret and generate JSON.  Don’t be intimidated by JavaScript in the acronym. JSON was born out of this language but turned out to be a great tool for sending data, for example in HTTP requests. It’s compact, lightweight, and easy to read.  So it is easy to transfer it via the network, it is easy to parse, and it is easy to read as simple text. What Is the Difference Between JSON and XML? If you’re familiar with XML, then JSON is very similar. There are also fields with names and values. Let’s compare JSON and XML for the same object. Here’s the JSON: Here’s the XML: XML looks less readable because of opening and closing tags. Yet, in general, both formats can be read without any extra processing. JSON Objects and JSON Arrays Let’s take a look at the JSON object. In this case, it’s an JSON array. It is enclosed in square brackets and can contain values of different types: Other JSON arrays JSON objects enclosed in curly braces Quoted string values Numeric values ​​(integer and float) Booleans ​​(true or false) Empty values

JSON Object vs. JSON Array Explained With Python

JSON Object vs. JSON Array Explained With Python

Here’s the difference between JSON objects and JSON arrays. Whether you use a JSON object or a JSON array is a matter of taste. So if you want to know what the difference between a JSON object and a JSON array is, then this article is for you. Let’s get started! What is the difference between JSON Object and JSON Array? JSON (JavaScript Object Notation) is a format for storing and exchanging data as keys and values. For example, when retrieving the response from an HTTP request. The difference between a JSON object and a JSON array is just a matter of taste. If you’re in charge of development it’s up to you. In the following, you will learn exactly what JSON objects and JSON arrays are and how they differ from each other explained with Python. In Python, working with JSON is best done by converting them to dictionary objects.  The JSON record format is very concise and does not require duplicate information like XML. What Is a JSON Object in Python? Let’s create a JSON object and dictionary in Python: Here we see almost exactly the same styling on two objects. The first is a JSON object describing the car, and the second is a Python dictionary with the same values.  You can pass JSON objects in plain text, which is good and bad.  The advantage is that it’s easy to read and parse, and the downside is security.  Deserialization is the process of converting JSON into objects.  For the Python json standard library, deserialization generates a dictionary output.  Let’s convert a JSON string to a dictionary: After deserializing, you can access the JSON fields as dictionary fields.  For example, here’s the manufacturer of your car: Here, JSON object is loaded from a string, like a response to an HTTP request.  You can also load a JSON object from a file using the method json.load. To save a dictionary

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.

PIP vs. PIP3: Difference?

Here’s everything about PIP vs. PIP3 and how to use each. You’ll learn: So if you want to understand PIP and PIP3, then you are in the right place. Let’s get started! What Is the Difference Between PIP and PIP3? Sooner or later, anyone who starts learning Python faces the need to install additional modules.  However, this may not happen quickly, as the standard Python library is very extensive and contains modules for such as: Since version 2.7.9 for Python 2 and version 3.4 for Python 3, the standard distribution has included the PIP package manager.  By the way, PIP is a recursive acronym that stands for PIP installs packages. The difference between PIP and PIP3 in short and sweet: PIP is a soft link for a particular installer. Your system will use one of your Python versions depending on what exactly is first in the system PATH variable. When you run PIP3, you can be sure that the module will be installed in Python 3. But to understand truly the difference between PIP and PIP3 you need to understand the big picture and see PIP and PIP3 in action—let’s dive right in: When and How to Use PIP and PIP3? You can find the packages to download and install in the PyPI (Python Package Index) repository. Still, you can set up your repositories and install packages from them using the standard PIP tool.  Moreover, you can create your package, publish it on PyPI, and make it available for installation by all Python users. Typically, in manuals and instructions, package installation is written as: For example: However, you may also see other options, such as pip3 install instead of pip install. So how do you correctly install packages on your system and what is the difference between PIP and PIP3? When you type pip in the console, the system looks for an executable file with that name in the current

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