Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Comprehensive Guide to Python Operators: Arithmetic, Logical, Comparison, Assignment, Bitwise, and Precedence

What Are Logical Operators in Python?

Logical operators in Python evaluate Boolean expressions and return True or False. The three primary logical operators are and, or, and not. They are indispensable for controlling flow with if, while, and other conditional statements.

In this tutorial, we’ll explore the full spectrum of Python operators, including:

Arithmetic Operators

Arithmetic operators perform numeric calculations. Python supports the usual operators: + (addition), - (subtraction), * (multiplication), / (true division), // (floor division), % (modulus), and ** (exponentiation). These can be used directly in expressions or via the built‑in eval() function for dynamic evaluation.

Example: Simple Addition

x = 4

y = 5
print(x + y)  # Output: 9

Other operators work similarly; for instance, 4 * 5 yields 20, 4 / 2 gives 2.0, and 4 ** 2 results in 16.

Comparison Operators

Comparison (relational) operators evaluate the relationship between two values and return a Boolean result. They include:

Example: Comparing Two Variables

x = 4
y = 5
print('x > y is', x > y)  # Output: False

Assignment Operators

Assignment operators store values in variables. The basic form is =, while compound operators combine an operation with assignment, such as +=, -=, *=, /=, and **=.

Example: Basic and Compound Assignment

num1 = 4
num2 = 5
print('Line 1 - Value of num1:', num1)
print('Line 2 - Value of num2:', num2)

# Compound addition
res = num1 + num2
res += num1
print('Line 3 - Result after res += num1:', res)  # Output: 13

Logical and Bitwise Operators

Logical operators (and, or, not) evaluate Boolean expressions. Bitwise operators (&, |, ^, ~, <<, >>) perform bit-level manipulation.

Example: Logical Operations

a = True
b = False
print('a and b is', a and b)  # Output: False
print('a or b is', a or b)    # Output: True
print('not a is', not a)      # Output: False

Membership Operators

Membership operators test for presence of a value within a sequence: in and not in.

Example: Checking Membership in a List

x = 4
y = 8
sample_list = [1, 2, 3, 4, 5]

if x in sample_list:
    print('Line 1 - x is available in the list')
else:
    print('Line 1 - x is not available in the list')

if y not in sample_list:
    print('Line 2 - y is not available in the list')
else:
    print('Line 2 - y is available in the list')

Identity Operators

Identity operators compare the memory addresses of objects: is and is not. They are essential when distinguishing between objects that are equal in value but distinct in identity.

Example: Identity Comparison

x = 20
y = 20
if x is y:
    print('x & y have the same identity')

y = 30
if x is not y:
    print('x & y have different identities')

Operator Precedence

Python follows a strict precedence hierarchy to resolve expressions without parentheses. The highest precedence is exponentiation (**), followed by unary operators, multiplication/division/modulus, addition/subtraction, comparison operators, identity operators, membership operators, logical operators, and finally assignment operators.

Using parentheses overrides the default precedence and makes expressions explicit.

Example: Precedence in Action

v = 4
w = 5
x = 8
y = 2
z = (v + w) * x / y
print('Value of (v + w) * x / y is', z)  # Output: 28.0

Python 2 Compatibility

Below are equivalent snippets for Python 2, which uses print statements without parentheses.

# Arithmetic Operators
x = 4
y = 5
print x + y

# Comparison Operators
x = 4
y = 5
print 'x > y is', x > y

# Assignment Operators
num1 = 4
num2 = 5
print 'Line 1 - Value of num1:', num1
print 'Line 2 - Value of num2:', num2

# Compound Assignment
num1 = 4
num2 = 5
res = num1 + num2
res += num1
print 'Line 3 - Result after res += num1:', res

# Logical Operators
a = True
b = False
print 'a and b is', a and b
print 'a or b is', a or b
print 'not a is', not a

# Membership Operators
x = 4
y = 8
sample_list = [1, 2, 3, 4, 5]
if x in sample_list:
    print 'Line 1 - x is available in the list'
else:
    print 'Line 1 - x is not available in the list'
if y not in sample_list:
    print 'Line 2 - y is not available in the list'
else:
    print 'Line 2 - y is available in the list'

# Identity Operators
x = 20
y = 20
if x is y:
    print 'x & y have the same identity'
y = 30
if x is not y:
    print 'x & y have different identities'

# Operator Precedence
v = 4
w = 5
x = 8
y = 2
z = (v + w) * x / y
print 'Value of (v + w) * x / y is', z

Summary

Python

  1. Mastering C# Bitwise & Bit Shift Operators: A Comprehensive Guide
  2. Python Variables, Constants, and Literals – A Comprehensive Guide
  3. Mastering Python Operators: A Comprehensive Guide
  4. Python List Operations: Creation, Access, Modification, and Advanced Techniques
  5. Mastering C# Variables & Operators: Practical Examples & Explanations
  6. Mastering Bitwise Operators in C: AND, OR, XOR, Shifts & Complement
  7. Python Dictionaries: Adding, Updating, and Managing Key/Value Pairs
  8. Python abs() Function: Compute Absolute Values for Numbers & Complex Numbers
  9. Mastering Python's range() Function: From Basics to Advanced Use Cases
  10. Master Python Basic Operators: Arithmetic, Comparison, Logical & More