Skip to content

Python Operators Explained with Examples sailaja Spark By {Examples}

  • by

Introduction to Python operators

Understanding the different types of operators and their usage is important for writing efficient
and effective Python code. Python operators are special symbols that are used to perform
specific operations on values and variables. These operators operate on values called operands.

Example:

>>> 5-2
3

In the above example, is our operator, 5 and 2 are operands, and 3 is the result of the operation.

Arithmetic operators

These arithmetic operators in Python are used for performing addition, subtraction, multiplication, division operations, etc.

Operator Meaning Example +, additionAdds two operands 3+4=7-, subtractionSubtracts right operand from the left4-1=3*, multiplicationMultiplies two operands2*3=6/, divisionDivides the left operand by the right one15/4=3.75%, modulusReturns the remainder when the first operand is divided by the second6%4=2

Comparison operators

These comparison operators are used to compare values.

OperatorMeaningExample >, greater thanReturns True if the left operand is greater than the right3>1=True<, less thanReturns True if the left operand is less than the right3<1=False==, equal toReturns True if both operands are equal3==1=False!=, not equal toReturns True if both operands are not equal3!=1=True>=, greater than or equal toReturns True if the left operand is greater than or equal to the right3>=1=True<=, less than or equal toReturns True if the left operand is less than or equal to the right3<=1=False

Logical operators

These logical operators perform logical operations

Operator Meaning Example AndReturns True if both operands are trueTrue and True = TrueorReturns True if either one of the operands is trueTrue or False = TruenotReturns True if the operand is false or vice versanot True = False

Assignment operators

These assign operators are used to assign values to variables

OperatorDescriptionExampleEquivalent to=Assigns a value to a variablex=2x=2+=Adds the value on the right to the variable on the left, then assigns the result to the left variablex+=2x=x+2-=Subtracts the value on the right from the variable on the left, then assigns the result to the left variablex-=2x=x-2*=Multiplies the variable on the left by the value on the right, then assigns the result to the left variablex*=2x=x*2/=Divides the variable on the left by the value on the right, then assigns the result to the left variablex/=2x=x/2%=Divides the variable on the left by the value on the right, then assigns the remainder to the left variablex%=2x=x%2//=Divides the variable on the left by the value on the right, then assigns the integer quotient to the left variablex//=2x=x//2**=Raises the variable on the left to the power of the value on the right, then assigns the result to the left variablex**=2x=x**2

Bitwise operators

Bitwise operators are used for performing bitwise operations on integers. The following table lists the bitwise operators in Python.

OperatorNameDescription&Bitwise ANDSets each bit to 1 if both bits are 1|Bitwise ORSets each bit to 1 if at least one of the bits is 1^Bitwise XORSets each bit to 1 if only one of the bits is 1~Bitwise NOTInverts all the bits<<Left shiftShifts the bits of the first operand to the left by the second operand

Membership operators

These Python operators are used to test whether a value is found in a sequence e.g. string, list, tuple, dictionary, set, etc.

Operator Meaning Example inReturns True if the value is found in sequence and vice versa‘h’ in ‘hello’ = True1 in [1, 2] = Truenot inReturns True if the value is not found in the sequence3 not in [1, 2] = True‘H’ not in ‘hello’ = True

Identity operators

OperatorMeaning Example isReturns True if both operands are the same object5 is 5 = Trueis notReturns True if both operands are not of the same object5 is not 5 = False

These operators are used to compare the objects if they are the same and have the same memory location

Conclusion

Understanding operators in Python is a must-have skill for any developer because these operators play a very crucial role as you are creating programs that require data manipulation, mathematical calculations, or logical comparisons. We hope you will keep practicing and experimenting with these concepts to enhance your skills and become a better programmer.

 Introduction to Python operators Understanding the different types of operators and their usage is important for writing efficientand effective Python code. Python operators are special symbols that are used to performspecific operations on values and variables. These operators operate on values called operands. Example: >>> 5-2 3 In the above example, – is our operator,  Read More Python, Python Tutorial 

Leave a Reply

Your email address will not be published. Required fields are marked *