Python Operators

Python Operators

Here’s a comprehensive table of Python operators, along with examples and explanations for each type:

Arithmetic Operators

OperatorNameExampleExplanation
+Addition5 + 3Adds two numbers
-Subtraction5 - 3Subtracts second number from first
*Multiplication5 * 3Multiplies two numbers
/Division5 / 3Divides first number by second
//Floor Division5 // 3Divides and returns the integer part
%Modulus5 % 3Returns the remainder of division
**Exponentiation5 ** 3Raises first number to the power of second

Assignment Operators

OperatorNameExampleExplanation
=Assignmentx = 5Assigns value to variable
+=Addition Assignmentx += 3Adds and assigns
-=Subtraction Assignmentx -= 3Subtracts and assigns
*=Multiplication Assignmentx *= 3Multiplies and assigns
/=Division Assignmentx /= 3Divides and assigns
//=Floor Division Assignmentx //= 3Floor divides and assigns
%=Modulus Assignmentx %= 3Modulus and assigns
**=Exponentiation Assignmentx **= 3Exponentiates and assigns

Comparison Operators

OperatorNameExampleExplanation
==Equal to5 == 3Checks if two values are equal
!=Not equal to5 != 3Checks if two values are not equal
>Greater than5 > 3Checks if first value is greater
<Less than5 < 3Checks if first value is lesser
>=Greater than or equal to5 >= 3Checks if first value is greater or equal
<=Less than or equal to5 <= 3Checks if first value is lesser or equal

Logical Operators

OperatorNameExampleExplanation
andLogical ANDTrue and FalseReturns True if both are True
orLogical ORTrue or FalseReturns True if one is True
notLogical NOTnot TrueReturns the opposite boolean value

Bitwise Operators

OperatorNameExampleExplanation
&AND5 & 3Bitwise AND
``OR`5
^XOR5 ^ 3Bitwise XOR
~NOT~5Bitwise NOT
<<Left Shift5 << 1Shifts bits left
>>Right Shift5 >> 1Shifts bits right

Identity Operators

OperatorNameExampleExplanation
isIdentityx is yChecks if both variables are same object
is notNot Identityx is not yChecks if both variables are not same object

Membership Operators

OperatorNameExampleExplanation
inMembership'a' in 'apple'Checks if value is in sequence
not inNot Membership'b' not in 'apple'Checks if value is not in sequence


Comments

Popular posts from this blog

Keyword , Identifier, Indentation, Comments & Documentation

DSA Lab 8 program

DSA Lab 7 Program