Python Operators

By using operators we can update or compare values or identities of variables.
OperationOperator SampleOutput
Addition+a=4
b=5
print(a+b)
9
Subtraction-print(a-b)-1
Multiplication*print(a*b)20
Division/print(a/b)0.8
Modulus%a=40
b=9
print(a%b)
4
Exponentiation**a=3
b=4
print(a**b)
81
Floor division//a=29
b=5
print(a//b)
5
More on Operators Precedence

Assignment Operators

We can assign or update value of a variable by using assignment operators
OperatorSyntaxEquivalent SampleOutput
=x=5x=5 x=5
print(x)
5
+=x += 5x = x + 5x=4
x += 5
print(x)
9
-=x -= 5x = x - 5x=4
x -= 5
print(x)
-1
*=x *= 5x = x * 5x=4
x *= 5
print(x)
20
/=x /= 5x = x / 5x=4
x /= 5
print(x)
0.8
%=x %= 5x = x % 5x=29
x %= 5
print(x)
4
//=x //= 5x = x // 5x=29
x //= 5
print(x)
5
**=x **= 3x = x ** 3x=5
x **= 3
print(x)
125
Read more on Bitwise operators to understand the codes below
&=x &= 5x = x & 5x=68
x &=5
print(x)
4
|=x |= 5x = x | 5x=68
x |=5
print(x)
69
^=x ^= 5x = x ^ 5x=68
x ^=5
print(x)
65
>>=x >>= 5x = x >> 5x=68
x >>= 5
print(x)
2
<<=x <<= 5x = x << 5x=68
x <<= 5
print(x)
2

Comparison Operators

We can compare two variable values by using comparison operators. Output of these comparison operator is always Boolean ( True or False )
OperationOperator SampleOutput
Equal==a=4
b=5
print(a == b)
False
Not Equal!=a=4
b=5
print(a != b)
True
Greater than>a=4
b=5
print(a > b)
False
Less than<a=4
b=5
print(a < b)
True
Greater than or equal to>=a=4
b=5
print(a>=b)
False
Less than or equal to<=a=4
b=5
print(a<=b)
True

Logical Operators

OperatorDescription SampleOutput
andTrue if both left and right side are Truea=5
b=15
print(a>10 and b<25)
False
orTrue if any one of left or right side are Truea=5
b=15
print(a>10 or b<25)
True
notReverse the output.
True if output is False , vice versa
a=5
print( not(a>10))
True

Identity Operators

is & not is Identity Operators

Membership Operators

in & not in Membership Operators

Bitwise Operators

Bitwise Operators
All Built in Functions in Python dir() ord()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer