Matplotlib: Change Width & Height of a Figure with Subplots
When using Matplotlib to create a figure that contains a grid of subplots, you can set the width and height of the figure by using the set_figwidth() and set_figheight() methods, respectively. Let’s examine the two examples…
Changing dtype of a NumPy array
The examples below show you how to cast a NumPy array from a dtype to another dtype by using the astype method. One important thing to keep in mind here is that the astype doesn’t mutate…
Python: Checking System Default Encoding
In Python, you can see your system default encoding by using the standard sys module. Here’s an example: Output: Further reading: List, Dict, and Set Comprehensions in Python 3 Python: Categorizing Given Words by Their First…
List, Dict, and Set Comprehensions in Python 3
A few examples of using list, dict, and set comprehensions in Python 3. Example 1: List Comprehension This example creates a list that contains numbers that are greater or equal to 10 based on a given…
Python: Categorizing Given Words by Their First Letters
The Challenge Let’s say we have a list of words and we want to put words into groups according to which letter they start with. Hypothetical input: Expected output: The result will be in the form…
Python 3: Simultaneously Iterating over Multiple Sequences
The example below shows you how to iterate through multiple sequences in Python 3 with just a single for loop. The trick here is to use the zip() function to zip up the elements of a…
2 Ways to Reverse a List in Python 3
This concise article shows you 2 ways to reverse a given list in Python 3. The first approach is to use slice notation and the second one is to use the reversed() function. Without any further…
Python 3: Convert Datetime to Timestamp and vice versa
This article shows you a couple of different ways to convert datetime instances into timestamps and vice versa in Python 3. Datetimes to Timestramps Approach 1: Using the timestramp() method Example: Output: Approach 2: Doing some…
Python 3: Formatting a DateTime Object as a String
In Python 3, you can use the strftime method to format a datetime object as a string. Example: Output: Here are all datetime format specifications: %Y: Four-digit year (e.g., 2021) %y: Two-digit year (e.g., 21) %m:…
Converting a String into Datetime Object in Python 3
You can use the datetime.strptime function to convert a string into a datetime object in Python 3. Example: Output: The example above shows you how strings can be parsed into datetime objects. If you’d like to…
How to Check Whether an Object is Iterable in Python 3
In Python, an iterable object is any object capable of returning its members one at a time, permitting it to be iterated over in a loop. For example, string and list are iterable. This short post…
Using the isinstance() function in Python 3
The isinstance() function in Python is used to check whether an object is an instance of a particular type. Syntax: Where: input_object: The object you want to check type type: A type, or a tuple of…