Introduction to Python
Component of Python
Operator are used to perform operations on variables and value.
It is used with numeric values to performs mathematical Operations.
Operator | Name | Example |
---|---|---|
+ | Addition | X+Y |
- | Subtracation | X-Y |
* | Multiplication | X*Y |
/ | Division | X/Y |
% | Modulus | X%Y |
// | Floor Division | X//Y |
** | Exponentiation | X**Y |
Assignment operators are used to assign values to variables:
Operator | Name | Example |
---|---|---|
= | x=5 | x=5 |
+= | x+=6 | x=x+6 |
-= | x-=7 | x=x-7 |
*= | x*=4 | x=x*4 |
/= | x/=3 | x=x/3 |
%= | x%=3 | x=x%3 |
//= | x//=6 | x=x//6 |
**= | x**=6 | x=x**6 |
&= | x&=7 | x=x&7 |
I= | xI=7 | x=xI7 |
^= | x ^=5 | x=x^5 |
>>= | x>>=6 | x=x>>6 |
<<= | x<<=5 | x=x<<5 |
Comparison operators are used to compare two values:
Operator | Name | Example |
---|---|---|
== | Equal | X==Y |
!= | Not Equal | X!=Y |
> | Greater than | X>Y |
< | Less than | X |
>= | Greater than or equal to | X>=Y |
<= | Less than or equal to | X<=y |
Logical operators are used to combine conditional statements:
Operator | Description | Example |
---|---|---|
and | Return true if both statement true | X<5 and X<10 |
or | Return true if one of the statement is true | X<5 or X<3 |
not | Reverse the result, returns False if the result is true | Not(x < 6 and x < 10) |
It is used to compare the object,not if they are equal ,but they are same object and allocated with the same memory location:-
Operator | Description | Example |
---|---|---|
is | Return true if both variables are the same object | X is Y |
is not | Returns true if both variables are not the same object | X is not Y |
Python membership operators test for membership in a sequence, such as strings, lists, or tuples.
Operator | Description | Example |
---|---|---|
in | Return true if finds a variable in the specified sequence. | X in Y |
not in | Return true if does not finds a variable in the specified sequence | X not in Y |
Bitwise operator works on bits and performs bit by bit operation:-
Operator | Name | Description | Example |
---|---|---|---|
& | And | Operator copies a bit to the result if it exists in both operands | (a & b) (means 0000 1100) |
| | OR | It copies a bit if it exists in either operand. | (a | b) = 61 (means 0011 1101) |
^ | XOR | It copies the bit if it is set in one operand but not both. | (a ^ b) = 49 (means 0011 0001) |
~ | NOT | Inverts all the bits | (~a ) = -61 (means 1100 0011 in 2's complement form due to a signed binary number. |
>> | Binary Right Shift | The left operands value is moved right by the number of bits specified by the right operand. | a << 2 = 240 (means 1111 0000) |
<< | Binary Left Shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off | a >> 2 = 15 (means 0000 1111) |
python training insitute| Best IT Training classes in Gurgaon | python training