image1 image2 image3 image3 image3 image3 image3

HELLO I'M SHASHI|WELCOME TO MY PERSONAL BLOG|I LOVE TO DO CREATIVE THINGS|I'M CYBER SECURITY STUDENT

Python Operators

                             Python Operators are here is

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Python divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators


Python Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

OperatorNameExampleTry it
+Additionx + yTry it »
-Subtractionx - yTry it »
*Multiplicationx * yTry it »
/Divisionx / yTry it »
%Modulusx % yTry it »
**Exponentiationx ** yTry it »
//Floor divisionx // yTry it »

Python Assignment Operators

Assignment operators are used to assign values to variables:

OperatorExampleSame AsTry it
=x = 5x = 5Try it »
+=x += 3x = x + 3Try it »
-=x -= 3x = x - 3Try it »
*=x *= 3x = x * 3Try it »
/=x /= 3x = x / 3Try it »
%=x %= 3x = x % 3Try it »
//=x //= 3x = x // 3Try it »
**=x **= 3x = x ** 3Try it »
&=x &= 3x = x & 3Try it »
|=x |= 3x = x | 3Try it »
^=x ^= 3x = x ^ 3Try it »
>>=x >>= 3x = x >> 3Try it »
<<=x <<= 3x = x << 3Try it »
:=print(x := 3)x = 3
print(x)
Try it »


Python Comparison Operators

Comparison operators are used to compare two values:

OperatorNameExampleTry it
==Equalx == yTry it »
!=Not equalx != yTry it »
>Greater thanx > yTry it »
<Less thanx < yTry it »
>=Greater than or equal tox >= yTry it »
<=Less than or equal tox <= yTry it »

Python Logical Operators

Logical operators are used to combine conditional statements:

OperatorDescriptionExampleTry it
and Returns True if both statements are truex < 5 and  x < 10Try it »
orReturns True if one of the statements is truex < 5 or x < 4Try it »
notReverse the result, returns False if the result is truenot(x < 5 and x < 10)Try it »

Python 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:

OperatorDescriptionExampleTry it
is Returns True if both variables are the same objectx is yTry it »
is notReturns True if both variables are not the same objectx is not yTry it »

Python Membership Operators

Membership operators are used to test if a sequence is presented in an object:

OperatorDescriptionExampleTry it
in Returns True if a sequence with the specified value is present in the objectx in yTry it »
not inReturns True if a sequence with the specified value is not present in the objectx not in yTry it »

Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

OperatorNameDescriptionExampleTry it
ANDSets each bit to 1 if both bits are 1x & yTry it »
|ORSets each bit to 1 if one of two bits is 1x | yTry it »
^XORSets each bit to 1 if only one of two bits is 1x ^ yTry it »
~NOTInverts all the bits~xTry it »
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall offx << 2Try it »
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall offx >> 2Try it »

Operator Precedence

Operator precedence describes the order in which operations are performed.

Example

Parentheses has the highest precedence, meaning that expressions inside parentheses must be evaluated first:

print((6 + 3) - (6 + 3))
Run example »

Example

Multiplication * has higher precedence than addition +, and therefor multiplications are evaluated before additions:

print(100 + 5 * 3)
Run example »

The precedence order is described in the table below, starting with the highest precedence at the top:

OperatorDescriptionTry it
()ParenthesesTry it »
**ExponentiationTry it »
+x  -x  ~xUnary plus, unary minus, and bitwise NOTTry it »
*  /  //  %Multiplication, division, floor division, and modulusTry it »
+  -Addition and subtractionTry it »
<<  >>Bitwise left and right shiftsTry it »
&Bitwise ANDTry it »
^Bitwise XORTry it »
|Bitwise ORTry it »
==  !=  >  >=  <  <=  is  is not  in  not in Comparisons, identity, and membership operatorsTry it »
notLogical NOTTry it »
andANDTry it »
orORTry it »

If two operators have the same precedence, the expression is evaluated from left to right.

Example

Addition + and subtraction - has the same precedence, and therefor we evaluate the expression from left to right:

print(5 + 4 - 7 + 3)
Run example »

Share this:

CONVERSATION

0 Comments:

Post a Comment