Introduction to Python Operators
Python is a high level, powerful, general purpose programming language created by Guido van Rossum in 1991. Python is initially programmed in C and thus many of the syntax followed finds its origin from C based syntax. Python is an interpreted language which makes it different from Compiled language like C and C++. In this article, we learn more about the Python Operators.
It has a program code inbuilt called interpreter that runs the code, thus focusing on to the “what to do” rather than “how to do” part of the problem.
Operators in python are constructs in python which instructs the interpreter to perform a certain function, however, these are traditionally not defined as a function 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 used to perform 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 real value and ignores 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 bit i.e. they treat the operand as a string of bit for example in bitwise operations 5 will be considered as 0101.
4.8 (2,585 ratings)
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||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 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 memory locations of the operands, 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 condition becomes true.|
|<>||(a <> b)||If values of a and b are not equal, then condition becomes true.|
|>||(a > b)||If the value of a is greater than the value of b, then condition becomes true.|
|<||(a < b)||If the value of a is less than the value of b, then condition becomes true.|
|>=||(a >= b)||If the value of a is greater than or equal to the value of b, then condition becomes true.|
|<=||(a <= b)||If the value of b is less than or equal to the value of b, then condition becomes true.|
6. Assignment Operators
Refer as the name suggests is used to declare assignments to the operands, 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 a 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: