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 … Read more

How to Print Subscript in Python?

Problem Formulation Given two strings x and y. Create a new string xy and print it to the shell. Consider the following examples: INPUT x = ‘hi’ y = ‘finxter’ OUTPUT: hifinxter INPUT x = ‘2’ y = ‘4’ OUTPUT: 24 INPUT x = ‘abc’ y = ‘[1, 2, 3]’ OUTPUT: abc[1, 2, 3] Solution … Read more

How to Print Superscript in Python?

Problem Formulation Given two strings x and y. Create a new string xy and print it to the shell. Consider the following examples: INPUT x = ‘hi’ y = ‘finxter’ OUTPUT: hifinxter INPUT x = ‘2’ y = ‘4’ OUTPUT: 24 INPUT x = ‘abc’ y = ‘[1, 2, 3]’ OUTPUT: abc[1, 2, 3] Solution … Read more

Python Bitwise Operators [Full Guide + Videos]

Bitwise operators perform operations on the binary (bit) representation of integers. Background: Each integer is first written as a binary number that is a sequence of digits 0 or 1. For example: 0 is written as “0” 1 is written as “1” 2 is written as “10” 3 is written as “11” 4 is written … Read more

NumPy Broadcasting – A Simple Tutorial

Broadcasting describes how NumPy automatically brings two arrays with different shapes to a compatible shape during arithmetic operations. Generally, the smaller array is “repeated” multiple times until both arrays have the same shape. Broadcasting is memory-efficient as it doesn’t actually copy the smaller array multiple times. Here’s a minimal example: Let’s have a more gentle … Read more

Python Logical Operators [Blog + Video]

Logical operators work on Boolean values but can be used on integers and other objects as well. Python has three logical operators: and, or, and not. The following table provides a quick overview of Python logical operators: Operator Description Example and Returns True if both operands are True, and False otherwise. (True and True) == … Read more

Python Arithmetic Operators

Arithmetic operators are syntactical shortcuts to perform basic mathematical operations on numbers. Operator Name Description Example + Addition Calculating the sum of the two operands 3 + 4 == 7 – Subtraction Subtracting the second operand from the first operand 4 – 3 == 1 * Multiplication Multiplying the first with the second operand 3 … Read more

Python Comparison Operators [Blog + Videos]

Comparison operators are applied to comparable objects and they return a Boolean value (True or False). Operator Name Description Example > Greater Than Returns True if the left operand is greater than the right operand 3 > 2 == True < Less Than Returns True if the left operand is smaller than the right operand … Read more