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

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

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

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

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

## Python Exponent – 4 Operators Every Coder Must Know

Python has four ways to calculate the n-th power (exponent) of x so that xⁿ=x*x*…*x that multiplies the base x with itself, and repeating this n-times. Method 1: Use the double-asterisk operator such as in x**n. Method 2: Use the built-in pow() function such as in pow(x, n). Method 3: Import the math library and … Read more

## Python Integer Division [2-Min Tutorial]

When I started to learn Python 3, I used to be confused about the semantics of dividing two integers. Is the result a float or an integer value? The reason for my confusion was a nasty Java bug that I once found in my code. The code was supposed to perform a simple division of … Read more

## Division in Python

The double-frontslash // operator performs integer division and the single-frontslash / operator performs float division. An example for integer division is 40//11 = 3. An example for float division is 40/11 = 3.6363636363636362. A crucial lesson you need to master as a programmer is “division in Python”. What does it mean to divide in Python? … Read more

## Python Multiplication Operator

Python’s multiplication operator * multiplies two objects. The semantics of the multiplication depends on the operands’ data types. For example, multiplying two integers performs arithmetic multiplication whereas multiplying a list with an integer performs list concatenation. The specific return value of the multiplication operator is defined in a data types’ __mul__() magic method. Have a … Read more

## Python Subtraction Operator

Python provides the subtraction operator – to subtract one object from another. The semantics of the subtraction depends on the operands’ data types. For example, subtracting two integers performs the arithmetic difference operation whereas subtracting two sets performs the set difference operation. The specific return value of the minus operator is defined in a data … Read more

## Python Program to Add Two Numbers

Add Two Integers The most basic Python program to add two integer numbers stored in variables num_1 and num_2 is the expression num_1 + num_2 using the addition operator. The following code shows how to add two numbers 20 and 22 and print the result 42 to the shell: Add Two Integers with User Input … Read more