Bitwise operators
ARITHMETIC OPERATORS :
Operator | Name | Example |
---|
+ | Addition | x + y |
- | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Modulus | x % y |
** | Exponentiation | x ** y |
// | Floor division | x // y |
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> X=4
>>> Y=8
>>> X+Y (Addition)
12
>>> X-Y (Subtraction)
-4
>>> X//Y (Floor/truncate division - In this it removes digits after decimal and reviews the result)
0
>>> X*Y (Multiplication)
32
>>> X%Y (Modulus - In this division remainder is displayed as a result)
4
>>> X/Y (division)
0.5
>>> X**Y (exponential)
65536
Assignment Operators :
Operator | Description |
---|
= | It assigns the the value of the right expression to the left operand |
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x=5
>>> x
5
>>> x=66.5
>>> print(x)
66.5
Operator | Description |
---|
+= | It increases the value of the left operand by the value of the right operand and assign the modified value back to left operand |
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a=25
>>> b=25
>>> a+=b
>>> print(a)
50
>>> a=a+b
>>> print(a)
75
Operator | Description |
---|
-= | It decreases the value of the left operand by the value of the right operand and assign the modified value back to left operand |
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a=20
>>> b=10
>>> a-=b
>>> print(a)
10
>>> a=a-b
>>> print(a)
0
Operator | Description |
---|
*= | It multiplies the value of the left operand by the value of the right operand and assign the modified value back to left operand. |
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a=10
>>> b=20
>>> a*=b
>>> print(a)
200
>>> a=a*b
>>> print(a)
4000
Operator | Description |
---|
%= | It divides the value of the left operand by the value of the right operand and assign the reminder back to left operand. |
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a=20
>>> b=10
>>> a%=b
>>> print(a)
0
>>> a=a%b
>>> print(a)
0
Operator | Description |
---|
**= | a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 = 16 to a. |
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a=4
>>> b=2
>>> a**=b
>>> print(a)
16
>>> a=a**b
>>> print(a)
256
Operator | Description |
---|
//= | A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3 = 1 to a. |
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a=4
>>> b=3
>>> a//=b
>>> print(a)
1
>>> a=a//b
>>> print(a)
0
RELATIONAL/COMPARISONAL OPERATORS :
Comparison operators are used to comparing the value of the two operands and returns boolean true or false accordingly.
Operator | Description |
---|
== | If the value of two operands is equal, then the condition becomes true. |
!= | If the value of two operands is not equal then the condition becomes true. |
<= | If the first operand is less than or equal to the second operand, then the condition becomes true. |
>= | If the first operand is greater than or equal to the second operand, then the condition becomes true. |
<> | If the value of two operands is not equal, then the condition becomes true. |
> | If the first operand is greater than the second operand, then the condition becomes true. |
< | If the first operand is less than the second operand, then the condition becomes true. |
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(Equal condition example):
>>> a=5
>>> b=5
>>> a==b
True
>>> a=6
>>> b=7
>>> a==b
False
(Not equal condition):
>>> a=2
>>> b=3
>>> a!=b
True
(Less than or equals to):
>>> a=2
>>> b=4
>>> a<=b
True
>>> a=5
>>> b=5
>>> a<=b
True
>>> a=8
>>> b=2
>>> a<=b
False
(Greater than or equals to):
>>> a=8
>>> b=9
>>> a>=b
False
>>> a=78
>>> b=58
>>> a>=b
True
>>> a=88
>>> b=88
>>> a>=b
True
(Greater than condition):
>>> a=28
>>> b=11
>>> a>b
True
(Less than condition):
>>> a=5
>>> b=6
>>> a<b
True
LOGICAL OPERATORS :
These operators are used to combine the conditional statements.
Operator | Description |
---|
and | If both the expression are true, then the condition will be true. If a and b are the two expressions, a → true, b → true => a and b → true. |
or | If one of the expressions is true, then the condition will be true. If a and b are the two expressions, a → true, b → false => a or b → true. |
not | If an expression a is true then not (a) will be false and vice versa. |
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 5>6 and 4==4 ('and' condition)
False
>>> 5<6 and 5==5
True
>>> 2==2 or 5>6 ('or' condition)
True
>>> 1.5>1 or 2==2
True
>>> 5>9 or 2==3
False
>>> not 2==2 ('not' condition)
False
>>> not(2>1)
False
IDENTITY OPERATORS :
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.
Operator | Description |
---|
is | Returns true if both variables are the same object. |
is not | Returns true if both variables are not the same object. |
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x={1,2,3,4}
>>> y={1,2,3,4}
>>> z = x
>>> print(x is y)
False
>>> print(z is x)
True
>>> print(x is z)
True
>>> print(x is not z)
False
>>> print(x is not y)
True
>>> print(z is not x)
False
MEMBERSHIP OPERATORS :
Membership operators are used to test the membership of value inside a data structure. If the value is present in the data structure, then the resulting value is true otherwise it returns false.
Operator | Description |
---|
in | It is evaluated to be true if the first operand is found in the second operand. |
not in | It is evaluated to be true if the first operand is not found in the second operand. |
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> X={"apple","cherry","kiwi"}
>>> print("banana" in X)
False
>>> print("kiwi"in X)
True
>>> print("dragon fruit"not in X)
True
>>> print("orange"not in X)
True
>>> print("orange"not in X)
BITWISE OPERATORS :
Bitwise operators are used to compare binary numbers
Operator | Name | Description |
---|
& | AND | Sets each bit to 1 if both bits are 1 |
| OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
OPERATOR PRECEDENCE :
The following table lists all operators from highest precedence to lowest.
Operator | Description |
---|
** | The exponent operator is given priority over all the others used in the expression. |
~,-,+ | The negation, unary plus and minus. |
* / % // | The multiplication, divide, modules, reminder, and floor division. |
+ - | Binary plus and minus |
>> << | Left shift and right shift |
& | Binary and. |
^ | |
<=, <, >, >= | Comparison operators (less then, less then equal to, greater then, greater then equal to). |
<>, ==, != | Equality operators. |
=, %=, /=, //=, -=, +=,*=,**= | Assignment operators |
is is not | Identity operators |
in not in | Membership operators |
not or and | Logical operators |
Python - RegEx SETS
posted on 2019-11-12 23:06:24 - Python Tutorials
Python - RegEx_Functions
posted on 2019-11-09 06:07:29 - Python Tutorials
Python - RegEx_Sets
posted on 2019-11-09 05:30:54 - Python Tutorials
ChatGPT Prompt Examples
posted on 2023-06-21 22:37:19 - ChatGPT Tutorials
Chat GPT Key Use Cases
posted on 2023-06-21 21:03:17 - ChatGPT Tutorials
Prompt Frameworks
posted on 2023-06-21 19:33:06 - ChatGPT Tutorials