Operators in Python

I am Developer, Artist and trying my luck on blogging as well. Well I am Ambitious, Passionate towards Learning, The Night Owl, And I Like challenges...
Search for a command to run...

I am Developer, Artist and trying my luck on blogging as well. Well I am Ambitious, Passionate towards Learning, The Night Owl, And I Like challenges...
No comments yet. Be the first to comment.
In this series you will get the fundamentals of python programming, and it will help you write a clean and effective scripts in future.
A loop statement allows us to execute a statement or group of statements multiple times. The following diagram illustrates a loop statement − while loop : Repeats a statement or group of statements while a given condition is TRUE. It tests the cond...
Post #2 in the Complete Prompt Engineering Series Welcome back! In What is Prompt Engineering? A Complete Introduction, you learned what prompt engineering is and why it matters. Now we're going deeper: understanding the engine under the hood. You do...

Welcome to the future of human-AI collaboration. If you're reading this in 2024-2025, you're witnessing a fundamental shift in how humans interact with machines—and prompt engineering is your passport to this new world. The Definition: What Exactly I...
In this post, we’ll walk through the anatomy of a great prompt, illustrate every step with vivid examples, and even peek under the hood to see what happens technically when you hit “send.” By the end, you’ll be able to craft prompts that unlock the f...

What is GenAI and Why Does Prompting Matter? If you’ve ever wondered how people interact with AI tools, or why some folks seem to get exactly what they want from tools like ChatGPT while others receive confusing or generic responses, this article is ...

Let’s be honest—being a developer isn’t just about writing code. It’s about solving problems, dealing with burnout, handling meetings, chasing deadlines, and finding time to learn, debug, and ship. But in the middle of this chaos, one simple habit ca...

Python Operators in general are used to perform operations on values and variables. These are standard symbols used for the purpose of logical and arithmetic operations. In this article, we will look into different types of Python operators.
Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, and division.

# Examples of Arithmetic Operator
a = 9
b = 4
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div1 = a / b
# Division(floor) of number
div2 = a // b
# Modulo of both number
mod = a % b
# Power
p = a ** b
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
13
5
36
2.25
2
1
6561
Bitwise operators act on bits and perform the bit-by-bit operations. These are used to operate on binary numbers.

# Examples of Bitwise operators
a = 10
b = 4
# Print bitwise AND operation
"""
a = 00001010
b = 00000100
------------
00000000 = 0
"""
print(a & b)
# Print bitwise OR operation
"""
a = 00001010
b = 00000100
------------
00001110 = 14
"""
print(a | b)
# Print bitwise NOT operation
"""
a = 00001010 =10
~a = 11110101 = -11
or -10-1=-11
"""
print(~a)
# print bitwise XOR operation
"""
a= 00001010
b= 00000100
------------------
xor= 00001110 = 14
"""
print(a ^ b)
# print bitwise right shift operation
print(a >> 2) #00001010 >>2 => 00000101 => 00000010 =2
# print bitwise left shift operation
print(a << 2) #00001010 <<2 => 00010100 => 00101000 =40
0
14
-11
14
2
40
Till now you would have come across many operators like addition, multiplication, etc. But what will you do when you have multiple operators in one expression?
This is the situation where we use the precedence of the operators, to get the correct result.

Operator precedence Table in Python:

PEMDAS Rule in Python: We would have learned of the BODMAS rule in mathematics while giving preference to the operators. We have a similar rule in Python and that is PEMDAS. Where,
a = 20
b = 10
c = 15
d = 5
e = 0
e = (a + b) * c / d #( 30 * 15 ) / 5
print ("Value of (a + b) * c / d is ", e)
e = ((a + b) * c) / d # (30 * 15 ) / 5
print ("Value of ((a + b) * c) / d is ", e)
e = (a + b) * (c / d); # (30) * (15/5)
print ("Value of (a + b) * (c / d) is ", e)
e = a + (b * c) / d; # 20 + (150/5)
print ("Value of a + (b * c) / d is ", e)
Value of (a + b) * c / d is 90.0
Value of ((a + b) * c) / d is 90.0
Value of (a + b) * (c / d) is 90.0
Value of a + (b * c) / d is 50.0