In this tutorial you will learn operators that supported by Python. The following are Python operators.
4.1 Arithmetic Operator
Arithmetic operators are used to perform simple calculator operation.
Operator |
Usage |
+ |
Add two operands |
- |
Subtract the left operand with the right operand |
* |
Multiply two operands |
/ |
Divide left operand by the right one (always return float type) |
% |
Modulus - remainder of the division |
// |
Floor division - division that results integer |
** |
Exponent - left operand is raised with the power of right operand |
Example
x = 18
y = 5
print('x + y =', x + y)
print('x - y =', x - y)
print('x * y =', x * y)
print('x / y =', x / y)
print('x // y =', x // y)
print('x ** y =',x ** y)
4.2 Comparison Operator
These operators are used to compare values wether it returns True or False from condition. Here are comparison operators in Python.
Operator |
Usage |
> |
Greater than - True if left operand is greater than the right |
< |
Less than - True if left operand is less than the right |
== |
Equal to - True if both operands are equal |
!= |
Not equal to - True if operands are not equal |
>= |
Greater than or equal to - True if left operand is greater than or equal to the right |
<= |
Less than or equal to - True if left operand is less than or equal to the right |
Example
x = 18
y = 31
print('x > y is', x > y)
print('x < y is', x < y)
print('x == y is', x == y)
print('x != y is', x != y)
print('x >= y is', x >= y)
print('x <= y is', x <= y)
4.3 Logical Operator
Operator |
Usage |
and |
True if both the operands are true |
or |
True if either of the operands is true |
not |
True if operand is false (complement) |
Example
x = False
y = True
print("x and y is", x and y)
print("x or y is", x or y)
print("not x is", not x)
4.4 Bitwise Operator
Bitwise operator is to perform bit operation. Example 4 and 9, now in binary
100 and 1001 respectively. Let's say
x = 5 in binary 0000 0101
y = 9 in binary 0000 1001
Operator |
Usage |
Example |
& |
AND |
x & y = 1 (0000 0001) |
| |
OR |
x | y = 13 (0000 1101) |
~ |
NOT |
~x = -6 (1111 1010) |
^ |
XOR |
x ^ y = 12 (0000 1100) |
>> |
Right shift |
x >> 2 = 1 (0000 0001) |
<< |
Left shift |
x << 2 = 20 (0010 0100) |
x = 5
y = 9
print(x & y)
print(x | y)
print(~x)
print(x ^ y)
print(x >> 2)
print(x << 2)
4.5 Assignment Operator
Operator |
Example |
Is same with |
= |
x = 7 |
x = 7 |
+= |
x += 7 |
x = x + 7 |
-= |
x -= 7 |
x = x - 7 |
*= |
x *= 7 |
x = x * 7 |
/= |
x /= 7 |
x = x / 7 |
%= |
x %= 7 |
x = x % 7 |
//= |
x //= 7 |
x = x // 7 |
**= |
x **= 7 |
x = x ** 7 |
&= |
x &= 7 |
x = x & 7 |
|= |
x |= 7 |
x = x | 7 |
^= |
x ^= 7 |
x = x ^ 7 |
>>= |
x >>= 7 |
x = x >> 7 |
<<= |
x <<= 7 |
x = x << 7 |
Program example for some operators assignment.
x = 5
y = 9
z = x + y
print("z = ", z)
z += x
print("z = ", z)
z -= y
print("z = ", z)
z *= x
print("z = ", z)
z %= y
print("z = ", z)
z **= x
print("z = ", z)
z //= x
print("z = ", z)
z /= y
print("z = ", z)
We haven't add the assignment operators for bit operation in above example, as an exercise how you perform bit operation.
4.6 Membership Operator
Membership operator is to test whether the value is exist in a sequence like string, list, tuple, set, and dictionary.
Operator |
Meaning |
in |
True if value/variable is exist in the sequence |
not in |
True if value/variable is not exist in the sequence |
Program example for membership operators.
s = "hello world"
print("Is 'hello' in s?:", 'hello' in s)
my_list = [1, 2, 3, 4]
print("Is 5 in my_list?:", 5 in my_list)
my_tuple = (1, 2, 3, 4)
print("Is 4 in my_tuple?:", 4 in my_tuple)
my_set = {1, 3, 5, 7}
print("Is 3 in my_set?:", 3 in my_set)
my_dict = {'a':1, 'b':2}
print("Is 'a' in my_dict?", 'a' in my_dict)
print("Is 1 in my_dict?", 1 in my_dict)
4.7 Identity Operator
Python identity operators are used to check if two values or variables are located in the same memory.
Operator |
Meaning |
is |
True if the operands are identical (the object is same) |
is not |
True if the operands are not identical (the object is different) |
Example
x = 30
y = 30
l1 = [1, 2, 3]
l2 = [1, 2, 3]
print("a and b have some identity?", x is y)
y = 31
print("a and b have some identity?", x is y)
print("l1 is not l2?", l1 is not l2)
Notice that l1 is not l2 will be true since l1 and l2 are located in the different memory even their values are equal.
4.8 Type Conversion
If you need to perform conversion between the built-in types, you only need to use type-names as a function. The following are Python type conversion example
print('1. int(a, base): convert data type to ingeter')
b = '10111'
i = int(b, 2)
print(i)
print('2. float(): convert data type to float')
f = float(i)
print(f)
print('3. ord(): convert a character to integer')
c = '7'
i = ord(c)
print(i)
print('4. hex(): convert integer to hexadecimal string')
h = hex(37)
print(h)
print('5. oct(): convert integer to octal string')
o = oct(37)
print(o)
print('6. tuple(): used to convert to a tuple')
c = 'codestuff'
t = tuple(c)
print(t)
print('7. set(): used to convert to set')
s = set(c)
print(s)
print('8. list(): used to convert to list')
l = list(c)
print(l)
print('9. dict(): used to convert a tuple of (key, value) to dictionary')
t = (('one', 1), ('two', 2), ('three', 3))
d = dict(t)
print(d)
print('10. str(): convert to string')
s = str(23.4)
print(s)
print('11. complex(real, imag) to convert real number to complex number')
c = complex(2.3, 4)
print(c)
0 Response to "4. Python Operators"
Post a Comment