infronx-final-logo

Operators in Python 3: A Comprehensive Guide

Python, a versatile and powerful programming language, offers a wide range of operators to handle various operations, from basic arithmetic to logical manipulations. Understanding these operators is fundamental to mastering Python programming. In this blog post, we’ll delve into the different types of operators in Python, providing clear examples to enhance your understanding.

python-operator-img

Table of Contents


Arithmetic Operators

Arithmetic operations like addition, subtraction, multiplication, and division are performed using arithmetic operators. 

Operands: The values or variables that are operated on by an operator. 

Addition (+): Add two numbers. 

Subtraction (-): Finds the difference between two numbers. 

Multiplication (*): Multiplies two numbers. 

Division (/): Divides one number by another. 

Modulus (%): Finds the remainder when one number is divided by another. 

Exponentiation (**): Multiplies a number by itself a specified number of times, also known as raising a number to a power. 

Floor Division (//): Divides and gives only the whole number quotient. 

a = 10
b = 5

# Addition
print(a + b)  # Output: 15

# Subtraction
print(a - b)  # Output: 5

# Multiplication
print(a * b)  # Output: 50

# Division
print(a / b)  # Output: 2.0

# Modulus
print(a % b)  # Output: 0

# Exponentiation
print(a ** b) # Output: 100000

# Floor Division
print(a // b) # Output: 2


Comparison Operators

Comparison operators in Python evaluate the relationship between two operands and return a Boolean value indicating the validity of the comparison (True or False). 

Equal to (==): Verifies if two operands are identical, considering both their values and memory locations.. 

Not equal to (!=): Verifies if two operands differ, considering both their values and memory locations. 

Greater than (>): Verifies if the left operand is greater than the right operand. 

Less than (<): Verifies if the left operand is less than the right operand. 

Greater than or equal to (>=): Verifies if the left operand greater or equals the right operand. 

Less than or equal to (<=): Verifies if the left operand is equal to or less than the right operand. 

x = 10
y = 20

print(x == y)  # Output: False
print(x != y)  # Output: True
print(x &gt; y)   # Output: False
print(x &lt; y)   # Output: True
print(x &gt;= y)  # Output: False
print(x &lt;= y)  # Output: True


Logical Operators

Logical operators are used to combine conditional statements. 

AND (and): Returns True if both operands are True. 

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

NOT (not): Returns True if the operand is False and vice versa. 

p = True
q = False

print(p and q)  # Output: False
print(p or q)   # Output: True
print(not p)    # Output: False


Assignment Operators

Assignment operators are used to assign values to variables. 

Equal (=): Assigns the value of the right operand to the left operand. 

Add and Assign (+=): Adds the right operand to the left operand and assigns the result to the left operand. 

Subtract and Assign (-=): Subtracts the right operand from the left operand and assigns the result to the left operand. 

Multiply and Assign (*=): Multiplies the right operand with the left operand and assigns the result to the left operand. 

Divide and Assign (/=): Divides the left operand by the right operand and assigns the result to the left operand. 

x = 10

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

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

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

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


Bitwise Operators

Bitwise operators are used to perform bitwise operations on integers. 

AND (&): Bitwise AND. 

    1010   (a) 

& 0100   (b) 

—————– 

    0000   (Result) 

—————– 

OR (|): Bitwise OR. 

    1010   (a) 

|   0100   (b) 

—————– 

    1110   (Result) 

—————– 

XOR (^): Bitwise XOR. 

    1010   (a) 

^  0100   (b) 

—————– 

    1110   (Result) 

—————– 

NOT (~): Bitwise NOT. 

~ 1010   (a) 

—————– 

  0101   (Result) 

—————– 

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

The right shift operator (>>) moves the bits of a number to the right. Any empty spaces it creates on the left side get filled with zeros. 

If you’re working with negative numbers, those empty spaces on the left side get filled with ones instead of zeros. It’s like dividing the number by some number that’s a power of two. 

So, in short, the right shift operator helps move bits to the right and does some special filling when it meets negative numbers! 

    1000   (a)   

>>      1   (b)  

—————– 

    0100   (Result) 

—————– 

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

The left shift operator (<<) moves the bits of a number to the left. Any empty spaces it creates on the right side get filled with zeros. 

Just like with the right shift and negative numbers, if you’re working with a negative number and use the left shift, those empty spaces on the right side get filled with ones. This gives a result similar to multiplying the number by some number that’s a power of two. 

    0010   (a)  

<<      1   (b)  

—————– 

    0100   (Result) 

—————– 

a = 10  # Binary: 1010
b = 4   # Binary: 0100

# Bitwise AND
print(a &amp; b)  # Output: 0

# Bitwise OR
print(a | b)  # Output: 14

# Bitwise XOR
print(a ^ b)  # Output: 14

# Bitwise NOT
print(~a)     # Output: -11

# Left Shift
print(a &lt;&lt; 1)  # Output: 20

# Right Shift
print(a &gt;&gt; 1)  # Output: 5


Membership Operators

Membership operators are used to test if a sequence (such as a list, tuple, or string) contains a value or not.  

In: Evaluates to True if a value is present within a sequence. 

Not In: Evaluates to True if a value is absent from a sequence. 

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

print(3 in lst)   # Output: True
print(6 not in lst)  # Output: True


Identity Operators

Identity operators examine the memory addresses of two objects. 

Is: Evaluates to True if both variables reference the same object in memory. 

Is Not: Evaluates to True if both variables reference distinct objects in memory.  

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

print(x is y)     # Output: False
print(x is not y) # Output: True


Ternary Operator

The ternary operator provides a concise way to write conditional statements.  

Syntax:

value_if_true if condition else value_if_false  

x = 10
y = 20
max_value = x if x &gt; y else y
print(max_value)  # Output: 20


Conclusion

Understanding operators in Python is crucial for writing efficient and effective code. Whether you’re performing arithmetic operations, comparing values, combining conditions, or manipulating bits, Python’s diverse range of operators has got you covered. We hope this comprehensive guide has provided you with valuable insights and clear examples to help you master Python’s operators. Happy coding! 

Most Recent Posts

Copyright © 2024 - All Rights Reserved By Infronx