## How to Index Elements in NumPy Arrays?

NumPy is a popular Python library for data science for array, vector, and matrix computations. This puzzle introduces basic indexing of elements in NumPy arrays. Problem Formulation: How to index elements in NumPy arrays? Indexing 1D Arrays with Positive Indices The most simple use of indexing is with the square bracket notation and positive integers: … Read more

## How to Calculate the Standard Deviation in NumPy?

Problem Formulation: How to calculate the standard deviation in NumPy? Differentiations: There are many different variants of this problem: Calculate the standard deviation of a 1D array Calculate the standard deviation of a 2D array Calculate the standard deviation of a 3D array Then you can also calculate the standard deviation along an axis: Calculate … Read more

## Python issubclass()

Python’s built-in issubclass(X, Y) function takes a class X and a class Y and returns True if the X is an instance of Y and otherwise False. The argument Y can also be a tuple in which case it checks whether X is a subclass of any class in the tuple—such as in issubclass(X, (class_1, … Read more

## PyPubSub – Creating Your First Publish Subscribe App in Python

Did you check the news today, or receive an email newsletter from a company? Both modes of communication follow the publish-subscribe communication pattern. This article will show you how to implement your own pubsub system in Python using the flexible PyPubSub library. If you already know about the PubSub concept, feel free to move ahead … Read more

## How to Modify a Text File in Python?

You can modify a text file in Python using one of the following methods:

1. Using The seek() Method.
2. Using The fileinput Module.
3. Using The splitlines() Method.
4. Using the regex module and the
split() and insert() methods.

## Python reversed() — A Simple Guide with Video

Python’s built-in reversed(sequence) function returns a reverse iterator over the values of the given sequence such as a list, a tuple, or a string. Usage Learn by example! Here are some examples of how to use the reversed() built-in function. The most basic use is on a Python list: You can see that the return … Read more

## How to Stop a Python Script (Keyboard and Programmatically)

Why Stop a Python Script? As a programming language,Python is designed to read code line by line and stop at the end of the script by default  –  so why would we need to stop it?  Whilst there are a number of reasons this may be necessary, they basically fall into two distinct categories: You … Read more

## Python HTML Get Parameter

Problem Formulation: How to perform an HTTP get call in Python? Solution: Use Python’s requests library. This is semantically equivalent to issuing an HTTP get call: http://example.com?param_1=value_1&param_2=value_2 In fact, you can obtain this exact URL by using the r.url attribute on the request object: You can find the text response by using the r.text attribute … Read more

## Python isinstance()

Python’s built-in isinstance(object, class) function takes an object and a class as input arguments. It returns True if the object is an instance of the class. Otherwise, it returns False. Instead of a class, you can also pass a tuple of classes to check if the object is an instance of any of the classes … Read more

## How to Switch Keys and Values in a Python Dictionary?

Problem: Given a dictionary in Python; how to switch Keys and Values?  Method 1: Using a for loop   Method 2: Using the zip function Method 3: Using the map and reverse functions Method 4: Using a dictionary comprehension Summary: Use one of the following methods to switch between the keys and values in a dictionary with unique values. … Read more

## How to Check if an Object is of Type List in Python?

Problem Formulation: Given a Python object. How to check if the given Python object is of type list? Example: Given a dictionary for which you don’t know the values associated to the keys. For example, suppose you have entry dict[key] for which you don’t know the type. How to check if it’s a list? Given: … Read more

## How to Split a Byte String into Lines?

Problem Formulation: Given a byte string that contains new-line characters ‘\n’. How to split the byte string into a list of lines? Example: You want to transform the byte string b’your\nbyte\nstring’ into the list of byte strings [b’your’, b’byte’, b’string’] using b’\n’ as a newline separator. Given: b’your\nbyte\nstring’ Goal: [b’your’, b’byte’, b’string’] Solution: To split … Read more