Operators are the constructs that can manipulate the value of operands. For example: Given an expression 3+2 = 5; here 3 and 2 are operands and + is the operator. Python supports the following operators which we will look at in-depth:
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Arithmetic Operators
Python has 7 arithmetic operations that perform mathematical operations. They include addition(+), subtraction(-) , division( /), modulo/remainder (% ) and exponents (**) Here are some examples you should try out in your interactive prompt:
print "5 + 2 = ", 5+2 # addition print "5 - 2 = ", 5-2 # subtraction print "5 * 2 = ", 5*2 # multiplication print "5 / 2.0 = ", 5/2.0 # division print "5 // 2 = ", 5//2 # floor division which truncates results print "5 ** 2 = ", 5**2 # power of a numer /exponents print "5 % 2 = ", 5%2 # modulus/remainder
Note: Division (/) in Python2 returns a truncated decimal place and returns integer value hence need to add decimal point .0 to get the real division. This has been fixed in Python3.
Assignment Operators
Comparison (Relational) Operators These operators compare the values on either side of them and decide the relation among them. They are also called Relational operators. Here are some examples you should try out:
print "5 == 2 is ", 5 == 2 # returns True if the two values are equal, false otherwise print "5 != 2 is ", 5 != 2 # returns True if the two value are not equal, false otherwise print "5 > 2 is ", 5 > 2 # returns True if left value is greater than right value, false otherwise print "5 < 2 is ", 5 < 2 # returns True if left value is less than right value, false otherwise print "5<= 2 is ", 5 <= 2 # returns True if left value is less than or equal to right value, false otherwise print "5 >= 2 is ", 5 >= 2 # returns True if left value is greater than or equal to right value, false otherwise
Logical Operators
The following are the logical operators in Python. AND – returns true if both values are true OR – returns True if either of the values are true NOT – Used to reverse the logical state of its operand Example of logical operations
# Assign variable a to True and variable b to false a = True b = True print a and b # Returns False print a or b # Returns True print not(a) # Retuns False
Assignment Operators
Assignment operators are used to assign values to the variables.
Operator | Description | Example |
---|---|---|
= | Assigns values from right side operands to left side operand | c = a + b assigns value of a + b into c |
+= Add AND | It adds right operand to the left operand and assign the result to left operand | c += a is equivalent to c = c + a |
-= Subtract AND | It subtracts right operand from the left operand and assign the result to left operand | c -= a is equivalent to c = c – a |
*= Multiply AND | It multiplies right operand with the left operand and assign the result to left operand | c *= a is equivalent to c = c * a |
/= Divide AND | It divides left operand with the right operand and assign the result to left operand | c /= a is equivalent to c = c / ac /= a is equivalent to c = c / a |
%= Modulus AND | It takes modulus using two operands and assign the result to left operand | c %= a is equivalent to c = c % a |
**= Exponent AND | Performs exponential (power) calculation on operators and assign value to the left operand | c **= a is equivalent to c = c ** a |
//= Floor Division | It performs floor division on operators and assign value to the left operand | c //= a is equivalent to c = c // a |
Membership Operators
in and not in are the membership operators; used to test whether a value or variable is in a sequence.
in True if value is found in the sequence not in True if value is not found in the sequence
Identity Operators
is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal does not imply that they are identical. For example:
a = "goat" b = "Goat" c = "goat" print a is not b # returns True, remember python is case sensitive hence G is not equal to g print a is c # returns True