Introduction to Python Operators
Constructs in Python programming language that instruct the interpreter to perform a certain function involving two or more variables upon which the construct operates are called python operators. Though not actually seen as functions, they have their own identity as concepts because they are different syntactically and semantically from functions. It performs tasks of various nature, including arithmetic, bitwise, membership, identity, comparison, assignment, and logic, etc., with a certain order of execution existing among them.
Operators in python are constructs in Python that instruct the interpreter to perform a certain function; however, these are traditionally not defined as functions; rather, they are syntactically and semantically different from functions. Operators are used to performing operations on variables and values according to their use.
Python language supports the following types of operators.
- Arithmetic Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
- Comparison Operators
- Assignment Operators
- Logical Operators
1. Arithmetic Operator
Arithmetic operators are used to performing mathematical operations
|+||Addition||a+b||Returns sum of the operands|
|–||Subtraction||a-b||Returns Difference of the operands|
|/||Division||a/b||Returns Quotient of the operands|
|*||Multiplication||a*b||Returns product of the operands|
|**||Exponentiation||a**b||returns exponent of a raised to the power b|
|%||Modulus||a%b||returns remainder of the division|
|//||Floor division||a//b||returns a real value and ignores the decimal part|
Let us consider an example program for carrying out the arithmetic operations explained above.
Let us consider two integers Xa=2 and Xb=3
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
add = Xa + Xb
diff = Xa - Xb
mul = Xa * Xb
div = Xa / Xb
floor_div = Xa // Xb
power = Xa ** Xb
modulus = Xa % Xb
print('Sum of the numbers is',Xa ,'and' ,Xb ,'is :',add)
print('Difference of the numbers is ',Xa ,'and' ,Xb ,'is :',diff)
print('Product of the numbers is ' ,Xa ,'and' ,Xb ,'is :',mul)
print('Division of the numbers is ',Xa ,'and' ,Xb ,'is :',div)
print('Floor Division of the numbers is ',Xa ,'and' ,Xb ,'is :',floor_div)
print('Exponent of the numbers is ',Xa ,'and' ,Xb ,'is :',power)
print('Modulus of the numbers is ',Xa ,'and' ,Xb ,'is :',modulus)
2. Bitwise Operators
Refers to the operators working on a bit, i.e. they treat the operand as a string of bits; for example, in bitwise operations, 5 will be considered as 0101.
The box below provides the bitwise operators in python
|&||Binary AND||a&b||copies a bit to the result if it exists in both operands|
||||Binary OR||a|b||copies a bit if it exists in either operand.|
|^||Binary XOR||a^b||copies the bit if it is set in one operand but not both.|
|~||Binary One’s Complement||a~b||Unary operation of flipping bits|
|<<||Binary Left Shift||a<<b||The left operands value is moved left by the number of bits specified by the right operand.|
|>>||Binary Right Shift||a>>b||left operands value is moved right by the number of bits specified by the right operand.|
3. Membership Operators
Refers to the operators used in the validation of membership of operand test in a sequence, such as strings, lists, or tuples. There are two types of membership operators in python
|in||if (a in x):||Evaluates to true if it finds a variable in the specified sequence and false otherwise.|
|not in||If ( b not in x ):||Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.|
4. Identity Operators
Used to compare the operands’ memory locations, they are quite often used to determine if the operand is of a particular type; there are two types of identity operators in python.
|is||x is y||returns True if the type of the value in y points to the same type in the x.|
|is not||x is not y||returns True if the type of the value in y points to a different type than the value in the x|
5. Comparison Operators
Also known as Relational operators, these operators are used in determining the relation between the operand on either side of the operator.
|==||(a == b)||If the values of a and b are equal, then the condition becomes true.|
|!=||(a != b)||If values of a and b are not equal, then the condition becomes true.|
|<>||(a <> b)||If values of a and b are not equal, then the condition becomes true.|
|>||(a > b)||If the value of a is greater than the value of b, then the condition becomes true.|
|<||(a < b)||If the value of a is less than the value of b, then the condition becomes true.|
|>=||(a >= b)||If the value of a is greater than or equal to the value of b, then the condition becomes true.|
|<=||(a <= b)||If the value of b is less than or equal to the value of b, then the condition becomes true.|
6. Assignment Operators
Referred as the name suggests, it is used to declare assignments to the operands; the following are the types of assignment operators in python.
|=||Equal to||c = a + b||assigns a value of a + b into c|
|+=||Add AND||c += a||is equivalent to c = c + a|
|-=||Subtract AND||c -= a||is equivalent to c = c – a|
|*=||Multiply AND||c *= a||is equivalent to c = c * a|
|/=||Divide AND||c /= a||is equivalent to c = c / ac /= a is equivalent to c = c / a|
|%=||Modulus AND||c %= a||is equivalent to c = c % a|
|**=||Exponent AND||c **= a||is equivalent to c = c ** a|
|//=||Floor Division||c //= a||is equivalent to c = c // a|
7. Logical Operators
These operators are used to perform similar operations as that of logical gates; there are 3 types of logical operators in python.
|and||Logical AND||a and b||a condition is true if both a and b are true|
|or||Logical OR||a or b||a condition is true if either a and b are true|
|not||Logical NOT||not a||Complement the operand|
Python Operators are the backbone of any operations and functions in the programming context.
This has been a guide to Python Operators. Here we discuss the various Python Operators like Logical, Comparison, Arithmetic, etc. You may also look at the following articles to learn more: