1.3. Operations in Python#

In Python, operations are actions performed on data. These actions can be arithmetic, logical, bitwise, or involve other types of operators. Understanding operations is fundamental to programming, as they allow you to manipulate data and perform various calculations and logical decisions [Python Software Foundation, 2024].

Types of Operations:

  1. Arithmetic Operations: Perform mathematical calculations.

  2. Logical Operations: Evaluate expressions and return Boolean values.

  3. Bitwise Operations: Operate on binary representations of numbers.

  4. Comparison Operations: Compare values and return Boolean results.

  5. Assignment Operations: Assign values to variables.

  6. Membership Operations: Check for membership in sequences.

  7. Identity Operations: Compare the memory locations of objects.

1.3.1. Arithmetic Operations#

Arithmetic operations are used to perform mathematical calculations. The primary arithmetic operators in Python are [Python Software Foundation, 2024]:

  • Addition (+): Adds two numbers.

  • Subtraction (-): Subtracts the second number from the first.

  • Multiplication (*): Multiplies two numbers.

  • Division (/): Divides the first number by the second.

  • Floor Division (//): Divides the first number by the second and returns the largest integer less than or equal to the result.

  • Modulus (%): Returns the remainder of the division of the first number by the second.

  • Exponentiation (**): Raises the first number to the power of the second.

Examples:

# Addition
result = 2 + 3
print(result)  # Output: 5

# Subtraction
result = 5 - 2
print(result)  # Output: 3

# Multiplication
result = 3 * 4
print(result)  # Output: 12

# Division
result = 10 / 2
print(result)  # Output: 5.0

# Floor Division
result = 10 // 3
print(result)  # Output: 3

# Modulus
result = 10 % 3
print(result)  # Output: 1

# Exponentiation
result = 2 ** 3
print(result)  # Output: 8
5
3
12
5.0
3
1
8

1.3.2. Logical Operations#

Logical operations are used to combine Boolean expressions. The primary logical operators in Python are [Python Software Foundation, 2024]:

  • AND (and): Returns True if both expressions are True.

  • OR (or): Returns True if at least one expression is True.

  • NOT (not): Returns the negation of the expression.

Examples:

a = True
b = False

# AND
result = a and b
print(result)  # Output: False

# OR
result = a or b
print(result)  # Output: True

# NOT
result = not a
print(result)  # Output: False
False
True
False

1.3.3. Bitwise Operations#

Bitwise operations operate on the binary representations of numbers. The primary bitwise operators in Python are [Python Software Foundation, 2024]:

  • AND (&): Performs a bitwise AND operation.

  • OR (|): Performs a bitwise OR operation.

  • XOR (^): Performs a bitwise XOR operation.

  • NOT (~): Performs a bitwise NOT operation.

  • Left Shift (<<): Shifts the bits to the left.

  • Right Shift (>>): Shifts the bits to the right.

Examples:

x = 5  # Binary: 0101
y = 3  # Binary: 0011

# Bitwise AND
result = x & y
print(result)  # Output: 1 (Binary: 0001)

# Bitwise OR
result = x | y
print(result)  # Output: 7 (Binary: 0111)

# Bitwise XOR
result = x ^ y
print(result)  # Output: 6 (Binary: 0110)

# Bitwise NOT
result = ~x
print(result)  # Output: -6 (Binary: ...11111010)

# Left Shift
result = x << 1
print(result)  # Output: 10 (Binary: 1010)

# Right Shift
result = x >> 1
print(result)  # Output: 2 (Binary: 0010)
1
7
6
-6
10
2

1.3.4. Comparison Operations#

Comparison operations are used to compare values and return Boolean results. The primary comparison operators in Python are [Python Software Foundation, 2024]:

  • Equal (==): Returns True if the values are equal.

  • Not Equal (!=): Returns True if the values are not equal.

  • Greater Than (>): Returns True if the first value is greater than the second.

  • Less Than (<): Returns True if the first value is less than the second.

  • Greater Than or Equal To (>=): Returns True if the first value is greater than or equal to the second.

  • Less Than or Equal To (<=): Returns True if the first value is less than or equal to the second.

Examples:

x = 5
y = 10

# Equal
result = x == y
print(result)  # Output: False

# Not Equal
result = x != y
print(result)  # Output: True

# Greater Than
result = x > y
print(result)  # Output: False

# Less Than
result = x < y
print(result)  # Output: True

# Greater Than or Equal To
result = x >= y
print(result)  # Output: False

# Less Than or Equal To
result = x <= y
print(result)  # Output: True
False
True
False
True
False
True

1.3.5. Assignment Operations#

Assignment operations are used to assign values to variables. The primary assignment operators in Python are [Python Software Foundation, 2024]:

  • Assignment (=): Assigns the value on the right to the variable on the left.

  • Add and Assign (+=): Adds the value on the right to the variable on the left and assigns the result to the variable on the left.

  • Subtract and Assign (-=): Subtracts the value on the right from the variable on the left and assigns the result to the variable on the left.

  • Multiply and Assign (*=): Multiplies the variable on the left by the value on the right and assigns the result to the variable on the left.

  • Divide and Assign (/=): Divides the variable on the left by the value on the right and assigns the result to the variable on the left.

  • Floor Divide and Assign (//=): Performs floor division on the variable on the left by the value on the right and assigns the result to the variable on the left.

  • Modulus and Assign (%=): Performs the modulus operation on the variable on the left by the value on the right and assigns the result to the variable on the left.

Examples:

x = 5

# Assignment
x = 10
print(x)  # Output: 10

# Add and Assign
x += 5
print(x)  # Output: 15

# Subtract and Assign
x -= 3
print(x)  # Output: 12

# Multiply and Assign
x *= 2
print(x)  # Output: 24

# Divide and Assign
x /= 4
print(x)  # Output: 6.0

# Floor Divide and Assign
x //= 2
print(x)  # Output: 3

# Modulus and Assign
x %= 2
print(x)  # Output: 1
10
15
12
24
6.0
3.0
1.0

1.3.6. Membership Operations#

Membership operations check for membership in sequences (e.g., lists, tuples, strings). The primary membership operators in Python are [Python Software Foundation, 2024]:

  • In (in): Returns True if the value is present in the sequence.

  • Not In (not in): Returns True if the value is not present in the sequence.

Examples:

numbers = [1, 2, 3, 4, 5]

# In
result = 3 in numbers
print(result)  # Output: True

# Not In
result = 6 not in numbers
print(result)  # Output: True
True
True

1.3.7. Identity Operations#

Identity operations compare the memory locations of objects. The primary identity operators in Python are [Python Software Foundation, 2024]:

  • Is (is): Returns True if both variables refer to the same object.

  • Is Not (is not): Returns True if both variables refer to different objects.

Examples:

x = [1, 2, 3]
y = x
z = [1, 2, 3]

# Is
result = x is y
print(result)  # Output: True

# Is Not
result = x is not z
print(result)  # Output: True
True
True

1.3.8. Order of Operations#

In Python, as in most programming languages, certain rules dictate the order in which operators are evaluated in an expression. These rules are known as the “order of operations” or “operator precedence.” Following the correct order of operations is crucial to obtaining the expected results from your expressions. The order of operations in Python is as follows, from highest to lowest precedence:

  1. Parentheses (()): Expressions enclosed in parentheses are evaluated first.

  2. Exponentiation (**): The exponentiation operator is evaluated next.

  3. Multiplication (*), Division (/), Floor Division (//), and Modulus (%): These arithmetic operations are evaluated from left to right.

  4. Addition (+) and Subtraction (-): These arithmetic operations are also evaluated from left to right.

  5. Bitwise NOT (~): This operation is evaluated next.

  6. Bitwise AND (&): This operation is evaluated next.

  7. Bitwise OR (|): This operation is evaluated next.

  8. Bitwise XOR (^): This operation is evaluated next.

  9. Left Shift (<<) and Right Shift (>>): These operations are evaluated next.

  10. Comparison Operators: These include ==, !=, >, <, >=, and <=.

  11. Logical NOT (not): This operation is evaluated next.

  12. Logical AND (and): This operation is evaluated next.

  13. Logical OR (or): This operation is evaluated last.

  14. Assignment Operators: These include =, +=, -=, *=, /=, //=, %=.

For example:

result = 2 + (3 * 4)
print(result)
14

In this example, multiplication has higher precedence than addition, so the expression is evaluated as follows:

result = 2 + (3 * 4)
result = 2 + 12
result = 14

To ensure that addition is performed first, use parentheses:

result = (2 + 3) * 4

Here, the addition is evaluated first:

result = (2 + 3) * 4
result = 5 * 4
result = 20

Summary

The priority order determines how expressions are evaluated. Operators with higher precedence are evaluated first, and if multiple operators with the same precedence are present in the expression, they are evaluated from left to right. Parentheses can be used to override the default precedence and explicitly define the order of operations.

  1. Parentheses: ()

  2. Exponentiation: **

  3. Multiplication: *, Division: /, Floor Division: //, Modulus: %

  4. Addition: +, Subtraction: -

  5. Bitwise NOT: ~

  6. Bitwise AND: &

  7. Bitwise OR: |

  8. Bitwise XOR: ^

  9. Left Shift: <<, Right Shift: >>

  10. Comparison Operators: ==, !=, >, <, >=, <=

  11. Logical NOT: not

  12. Logical AND: and

  13. Logical OR: or

  14. Assignment Operators: =, +=, -=, *=, /=, //=, %=.