Mastering Python Operators: The Ultimate Step-by-Step Guide

Python Operators Tutorial Step-by-Step Guide
Python Operators

Introduction to Python Operators

Python operators are symbols that performs operations on values and variables. Python operators allow you to manipulate data, execute calculations, and perform essential programming tasks efficiently

Types of Operators

Operator TypeSymbolsCharacteristics
Arithmetic Operators+, -, *, /, //, %, **Perform basic mathematical operations
Assignment Operators=, +=, -=, *=, /=, //=, %=Assign values and perform operations on variables.
Relationship Operators==, !=, >, <, >=, <=Compare values and return a boolean result
Logical Operatorsand, or, notConstruct large conditions by combining boolean values
Membership Operatorsin, not inCheck the given value present in sequences
Identity Operatorsis, is notCheck for object identity (memory location address)
Bitwise Operators&, |, ^, ~,<<,>>Perform operations at the bit level
Ternary Operatorx = true_value if cond else false_valueIf the condition is True then true_value will be considered else false_value will be considered

Note: Python doesn’t support the — and ++ operators, unlike in C and Java.

Arithmetic Operators:

  • Arithmetic operators used to perform mathematical operations on numeric values
  • They work with integers and decimal numbers and return numeric results

Example

a = 10
b = 5
print(a + b)  # Addition: 15
print(a - b)  # Subtraction: 5
print(a * b)  # Multiplication: 50
print(a / b)  # Division: 2.0
print(a // b) # Floor Division: 2
print(a % b)  # Modulus: 0
print(2 % 0) # Modulus: (ZeroDivisionError: modulo by zero)
print(a ** b) # Exponentiation: 100000

Assignment Operators:

Assign values and perform operations on variables.

x = 10
x += 5  # Addition assignment: x = x + 5 -> 15
x -= 2  # Subtraction assignment: x = x - 2 -> 13
x *= 3  # Multiplication assignment: x = x * 3 -> 39
x /= 3  # Division assignment: x = x / 3 -> 13.0

Relational operators:

  • These operators are used to compare the relation between two values.
  • These operators return two possible outcomes as a result either True or False when we compare the values.
  • By using these operators, we can construct simple conditions.

Assume that,
            a = 13
            b = 5

OperatorExampleResult
a>bTRUE
>=a>=bTRUE
a<bFALSE
<=a<=bFALSE
==a==bFALSE
!=a!=bTRUE

Example

x = 10
y = 20
print(x == y)  # Equality or equal: False
print(x != y)  # Inequality or not equal: True
print(x > y)   # Greater than: False
print(x < y)   # Less than: True
print(x >= y)  # Greater than or equal to: False
print(x <= y)  # Less than or equal to: True

Logical operators:

  • In Python there are three logical operators those are,
    • and : If both arguments are True, then the only result is True
    • or : If at least one argument is True, then the result is True
    • not : complement (opposite of actual result)
  • Logical operators are useful for building compound conditions.
  • Compound condition is a combination of two or more simple conditions.
  • Each simple condition brings the boolean result finally the total compound condition evaluates either True or False.

Example 1:

a = True
b = False
print(a and b)  # Logical AND: False
print(a or b)   # Logical OR: True
print(not a)    # Logical NOT: False

Example 2:

a = 10
b = 20
c = 30
print((a>b) and (b>c)) # Logical AND: False
print((a<b) and (b<c)) # Logical AND: True
print((a>b) or (b>c)) # Logical OR: True

Membership operators:

  • Membership operators are useful to check whether the given object is available in the collection (sequence) or not. (It may be string, list, set, tuple, range, and dict)
  • There are two membership operators,
    • in
    • not in
  • ‘in’ operator returns True, if the element is found in the sequences or else False
  • ‘not in’ operator returns True, if the element is not found in the sequences or else False

Example

lst = [10, 20, 30, 40, 50]
print(30 in lst)   # Membership: True
print(60 not in lst) # Non-membership: True

Identity operators (is, is not):

  • This operator compares the memory locations (address) of two values.
  • Using the id() function to check the address of every element.

Example

a=26
b=26
print(id(a)) # Output : 1570989024
print(id(b)) # Output : 1570989024

is operator:

  • A is B returns True if both A and B are pointing to the same object.
  • A is B returns False if both A and B are not pointing to the same object.

is not operator:

  • A is not B returns True if both A and B are not pointing to the same object.
  • A is not B returns False if both A and B are pointing to the same object.

Example

a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b)   # Identity: True
print(a is c)   # Identity: False
print(a is not c) # Identity: True

Bitwise Operators:

  • Bitwise operators can be applied to int and boolean data types only.
  • Applying bitwise operators to other data types will result in error.
OperatorDescription
&If both bits are 1 then only the result is 1 otherwise the result is 0
|If at least one bit is 1 then the result is 1 otherwise the result is 0
^If bits are different then only the result is 1 otherwise the result is 0
~bitwise complement operator i.e 1 is 0 and 0 is 1
>> Bitwise Left shift Operator
<< Bitwise Right shift Operator

Example 1:

print(5&6) # valid
print(11.5 & 5.6) # TypeError: unsupported operand type(s) for &: 'float' and 'float'
print(True & True) # valid

Example 2:

a = 10  # 1010 in binary
b = 4   # 0100 in binary
print(a & b)  # Bitwise AND: 0 (0000)
print(a | b)  # Bitwise OR: 14 (1110)
print(a ^ b)  # Bitwise XOR: 14 (1110)
print(~a)     # Bitwise NOT: -11 (inverted bits)

Ternary Operator:

Syntax: x = true_value if cond else false_value

If the condition is True then true_value will be used or else false_value will be considered.

Example 1:

a,b = 100,200
y = 300 if a<b else 400
print(y) #300

Example 2: Program for minimum of 3 numbers

a,b,c=10,20,30
min=(a if a<b and a<c else b) if b<c else c
print(min) # 10

Knowledge Check

Conclusion

Operators are crucial for data processing in Python. Understanding different types of operators and their usage is crucial for effective programming and manipulating data.

Data Analytics with Power Bi and Fabric
Could Data Engineer
Data Analytics With Power Bi Fabic
AWS Data Engineering with Snowflake
Azure Data Engineering
Azure & Fabric for Power bi
Full Stack Power Bi
Subscribe to our channel & Don’t miss any update on trending technologies

Kick Start Your Career With Our Data Job

Master Fullstack Power BI – SQL, Power BI, Azure Cloud & Fabric Tools
Master in Data Science With Generative AI Transform Data into Business Solutions
Master Azure Data Engineering – Build Scalable Solutions for Big Data
Master AWS Data Engineering with Snowflake: Build Scalable Data Solutions
Transform Your Productivity With Low Code Technology: Master the Microsoft Power Platform

Social Media channels

► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizon
► KSR Datavizon Instagram :- https://www.instagram.com/ksr_datavision
► KSR Datavizon Face book :- https://www.facebook.com/KSRConsultingServices
► KSR Datavizon Playstore :-

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *