Python Operator: Get Rid Of Paper!

Image credit:python-tricks.com

Why I’m creating this blog?

Many people are using paper in this 21st century. Especially the computer enthusiastic. If you are a computer science student then you know the python language.

Then why waste a long time in the paper for a long calculation? just use Python Operator. Need the least knowledge of the coding.

You don’t need specialized coding knowledge. What you need an enthusiasm for break the traditional rule of calculation.

I am going to show how can you do!

In this section, you are going to see:-

• Arithematic operator
• Assignment operator
• Comparison operator
• logical operator
• Identity operator
• Membership operator
• Bitwise operator

Arithmetic operator

+ Addition
x=5
y=10
print(x+y)

15

- Subtraction
x=6
y=2
print(x-y)

4

* Multiplication
x=8
y=2
print(x*y)

16

/ Division
x=60
y=20
print(x/y)

3.0

% Modules
x=4
y=2
print(x%y)

0

** Exponential
x=4
y=2
print(x**y)

16

// Floor devision
x=8
y=3

2

Python Assignment Operator: Store the value

• = (assign value)
x=5
print(x)

5

• += (assign addition)
x=6
x=x+6
print(x)

12

• -= (assign Substraction)
x=7
x-=3
print(x)

4

• *= (Multiplication)
x=6
x*=2
print(x)

12

• /= (Division)
x=3
x/=3
print(x)

1.0


• %= (assign Modulus)
x=4
x%=3
print(x)

1


•**= (Power assign)
x=5
x**=2
print(x)

25

•&= (And operator)
x=3
x&=2 (if both are other than 0 then 1)
print(x)

1


•|= (OR operator)
x=6
x|=2
(If one of the number true then 1)
print(x)

1


•^= (bitwise xor operator if only one true then 1,else 0 )
x=5
x^=3

print(x)

6

(5= 101 in binary)
(3= 011 in binary)
------------------------
110. 3rd column both are 1 so 0.
------------------------
110 in decimal format is 6.


•>>= (bitwise shift to the right-hand side)

x=5
x>>=2
print(x)

1


•<<= (bitwise shift to the left-hand side.)
x=5
x<<=3
print(x)

40
(5 in binary 101, then move 3 places left then it become 101000)
101000=40



Python comparison Operator: Compare the value

• == (equal to)
x=5
y=3
print(x==y)

False (5 is not equal to 3)

•!= (not equal to)
x=5
y=2
print(x!=y)

True (5 is not equal to 2)

• > (Greater than)
x=3
y=6
print(x>y)

False

• < (less than)
x=6
y=4
print(x<y)

False

• >= (greater than or equal to)
x=8
y=4
print(x>=y)

True

• <= (less than or equal to)
x=5
y=5
print(x<=y)

True

Python logical operator: Combine two conditions

• and (&)
If both conditions are true then the return value is true.
x=5
print(x>3 and x<8)

True
# 5 is greater than 3 and less than 8

• or(|)
If anyone condition is true then true.
x=6
print(x>4 or x<3)

True
# 3 is not greater than 6 but 4 is less than 6.

•not(~)
Reverse the result, if the result is true then false.
x=5
print(not(x>3 and x<9))

False
# Output should be true, but not make it false.

Python Identity Operator: To check if they are the same object, not similar object

• is operator x=["apple","banana"]
y=["apple","banana"]
print(x==y)

True
# Return true because x and y are the same objects.

x=["banana","apple"]
y=["apple","banana"]
print(x is y)

False
# return False because x is not the same as y, even the content is the same.



• is not
Returns true if both objects are not the same.

x=["apple","banana"]
y=["apple","banana"]

print(x is not y)
True
# Return true because they are not equal even their content is the same.

print(x==y)
True
# Both variables are equal.

Python Membership Operator: To check Element is present in the object or not

• in (operator)
Returns true if the element is present in the sequence of the group.
x=["apple","banana"]
print("banana" in x)

True
# Return True because "banana" is present in x.



•not in
Return true if the element is not present in the sequence of the group.
x=["apple","banana"]
print("orange" not in x)

True
# Return true because "orange" not in x

Python Bitwise operator:Compare the two numbers

Bitwise operator are used to compare the (binary) numbers.

Operator

&
|
^
~
<<
>>
Name

And
OR
XOR
NOT
FILL left shift
Signed right shift
Description

1.Both are true than true.
2.One true than true.
Only if one condition true, then true.
3.Inverse to the result.
Shift left by pushing zeros in from the right side.
4.Shift right by pushing zero on the left side.

Conclusion:-

Python operator giving a large sense of the mathematical calculation. you don’t have to use paper to do the calculation. It providing you with the basic calculation to advance calculation. It saves your time and energy.

See Also:

https://vipulkunwar503.code.blog/2021/06/16/python-string/

https://vipulkunwar503.code.blog/2021/06/13/python-syntax/

https://vipulkunwar503.code.blog/2021/06/10/python-numbers/