# Data Structures

## Reverse a String in Python

There are four main ways to reverse a string in Python: Slicing s[::-1] with negative step size traverses the string from right to left. ”.join(reversed(s)) first creates an iterable of characters in reversed order, and then joins these characters to obtain the reversed string. A for loop using the range(len(s)-1, -1, -1) function traverses the …

## What Are Differences Between type() and isinstance()?

The main difference between type() and isinstance() is that type(object) returns the type of an object and isinstance(object, class) returns True if the object argument is an instance of the class argument or in a direct or indirect subclass relationship. To strengthen your understanding, let’s quickly recap the syntactical definitions of both functions: type(object) – …

## Python Bitwise Left-Shift << Operator

The Python bitwise left-shift operator x << n shifts the binary representation of integer x by n positions to the left. For a positive integer, it inserts a 0 bit on the right and shifts all remaining bits by one position to the left. For example, if you left-shift the binary representation 0101 by one …

## Python Bitwise Right-Shift >> Operator

The Python bitwise right-shift operator x >> n shifts the binary representation of integer x by n positions to the right. It inserts a 0 bit on the left and removes the right-most bit. For example, if you right-shift the binary representation 0101 by one position, you’d obtain 0010. Semantically, the bitwise right-shift operator is …

## Python Bitwise NOT Operator

Python’s bitwise NOT operator ~x inverts each bit from the binary representation of integer x so that 0 becomes 1 and 1 becomes 0. This is semantically the same as calculating ~x == -x-1. For example, the bitwise NOT expression ~0 becomes -1, ~9 becomes -10, and ~32 becomes -33. As you go over the …

## How to Convert a Boolean Array to an Integer Array in Python?

Problem Formulation Given a NumPy array consisting of Boolean values. How to convert it to an integer array? Convert each True value to integer 1, and Convet each False value to integer 0. Here’s an example Boolean array: What you want is the following integer array: Let’s examine some methods to accomplish this easily. Method …

## Python Bitwise XOR ^ Operator

Python’s bitwise XOR operator x ^ y performs logical XOR on each bit position on the binary representations of integers x and y. Each output bit evaluates to 1 if and only if exactly one of the two input bits at the same position are 1. For example, the integer expression 4 ^ 3 is …

## Python Bitwise OR | Operator

Python’s bitwise OR operator x | y performs logical OR on each bit position on the binary representations of integers x and y. Each output bit evaluates to 1 if and only if at least one of the two input bits at the same position are 1. For example, the integer expression 4 | 3 …

## Python Bitwise AND Operator &

Python’s bitwise AND operator x & y performs logical AND on each bit position on the binary representations of integers x and y. Thus, each output bit is 1 if both input bits at the same position are 1, otherwise, it’s 0. For example, the integer expression 4 & 3 is translated to binaries 0100 …

## How to Count the Number of Occurrences of a Character in a Python String?

Problem Formulation Given a string s, and Given a character c. How many times does character c occur in the string s? Consider the following examples: INPUT s = ‘hello world’ c = ‘l’ OUTPUT 3 INPUT s = ‘aaaa’ c = ‘a’ OUTPUT 4 INPUT s = ‘finxter’ c = ‘z’ OUTPUT 0 Method …

## Python Not Operator

Python’s not operator returns True if the single operand evaluates to False, and returns False if it evaluates to True. Thus, it logically negates the implicit or explicit Boolean value of the operand. As you read through the article, you can also watch my video for supporting explanations: Python Not Operator on Boolean You can …

## Python Or Operator

Python’s or operator performs the logical OR operation that returns True if at least one of the operands evaluates to True. The operator performs an optimization called short-circuiting, so if the first operand evaluates to True, it returns the first right away without further evaluating the second, and if the first operand evaluates to False, …