Skip to content

Programming Essentials in Python Section 3

Operators in Python

Python categorizes operators into several types, each serving a specific purpose. Below are the main operator groups, each with descriptions, syntax, and examples.

1. Arithmetic Operators

These operators perform mathematical operations.

OperatorNameExample
+Additionx + y (Adds x and y)
-Subtractionx - y (Subtracts y from x)
*Multiplicationx * y (Multiplies x and y)
/Divisionx / y (Divides x by y)
%Modulusx % y (Remainder of x divided by y)
**Exponentiationx ** y (Raises x to the power of y)
//Floor Divisionx // y (Quotient of x divided by y rounded down)

2. Assignment Operators

Assignment operators assign values to variables.

OperatorNameExample
=Assignmentx = 5 (Assigns 5 to x)
+=Addition Assignmentx += 3 (Equivalent to x = x + 3)
-=Subtraction Assignmentx -= 3 (Equivalent to x = x - 3)
*=Multiplication Assignmentx *= 3 (Equivalent to x = x * 3)
/=Division Assignmentx /= 3 (Equivalent to x = x / 3)
%=Modulus Assignmentx %= 3 (Equivalent to x = x % 3)
**=Exponentiation Assignmentx **= 3 (Equivalent to x = x ** 3)
//=Floor Division Assignmentx //= 3 (Equivalent to x = x // 3)

3. Comparison Operators

Comparison operators compare values and return a boolean result.

OperatorNameExample
==Equal tox == y (True if x is equal to y)
!=Not equalx != y (True if x is not equal to y)
>Greater thanx > y (True if x is greater than y)
<Less thanx < y (True if x is less than y)
>=Greater than or equal tox >= y (True if x is greater than or equal to y)
<=Less than or equal tox <= y (True if x is less than or equal to y)

4. Logical Operators

Logical operators combine conditional statements.

OperatorNameExample
andLogical ANDx and y (True if both x and y are True)
orLogical ORx or y (True if at least one of x or y is True)
notLogical NOTnot x (True if x is False)

5. Bitwise Operators

Bitwise operators perform operations at the binary level on integers.

OperatorNameExample
&ANDx & y (x AND y)
|ORx | y (x OR y)
^XORx ^ y (x XOR y)
<<Left Shiftx << 2 (Shifts bits in x to the left by 2 positions)
>>Right Shiftx >> 2 (Shifts bits in x to the right by 2 positions)

Bitwise Operator Truth Tables

  1. Bitwise AND (&)
    Returns 1 only if both bits are 1.
aba & b
000
010
100
111
  1. Bitwise OR (|)
    Returns 1 if at least one bit is 1.
aba | b
000
011
101
111
  1. Bitwise XOR (^)
    Returns 1 only if one of the bits is 1 (similar = False, different = True).
aba ^ b
000
011
101
110

Bitwise Shift Operators

Bitwise shift operators move bits within the binary representation of an integer either to the left or to the right. Shifts can be a powerful way to perform fast calculations by modifying the binary representation directly.

  1. Left Shift (<<)
  • The left shift operator << shifts all bits in the number to the left by a specified number of positions.
  • Each left shift effectively multiplies the number by 2shift count.
  • Example:
    python
    x = 3       # Binary: 0000 0011
    print(x << 2)  # Shifts bits left by 2, resulting in 12 (Binary: 0000 1100)
  • Here, 3 << 2 results in 12 because shifting 3 (binary 0000 0011) left by 2 adds two zeros to the right, changing it to binary 0000 1100 which represents 12 in decimal. Same as multiplying 3 by 2shift count which is 322=12.
  1. Right Shift (>>)
  • The right shift operator >> moves all bits in the number to the right by a specified number of positions.
  • Each right shift effectively divides the number by 2shift count and discards any remainder (similar to floor division).
  • Example:
    python
    x = 8       # Binary: 0000 1000
    print(x >> 2)  # Shifts bits right by 2, yielding 2 (Binary: 0000 0010)
  • Here, 8 >> 2 results in 2 because shifting 8 (binary 0000 1000) right by 2 removes two rightmost bits, changing it to binary 0000 0010, which represents 2 in decimal. Same as dividing 8 by 2shift count which is 8÷22=2.

Floor Division and ceil() Function

  1. Floor Division (//)

    • Floor division divides and returns the integer part of the result; it removes anything after the decimal point.
    python
    print(7 // 2)  # Output: 3 (rounds down from 3.5)
  2. ceil() Function

    • The ceil() function, from Python's math module, rounds up to the nearest integer.
    python
    from math import ceil # Importing the `ceil` function from the `math` module
    print(ceil(4.2))  # Output: 5